@vercel/microfrontends 0.19.4 → 0.19.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1622,13 +1622,13 @@ function transform(args) {
1622
1622
  next
1623
1623
  };
1624
1624
  }
1625
- if (next.assetPrefix !== void 0 && next.assetPrefix !== app.getAssetPrefix()) {
1626
- console.log(
1627
- `"assetPrefix" already set. This route must be manually configured in your microfrontend.json file.`
1625
+ const assetPrefix = `/${app.getAssetPrefix()}`;
1626
+ if (next.assetPrefix !== void 0 && next.assetPrefix !== assetPrefix) {
1627
+ throw new Error(
1628
+ `"assetPrefix" already set and does not equal "${assetPrefix}". Either omit the assetPrefix in your next config, or set it to "${assetPrefix}".`
1628
1629
  );
1629
- } else {
1630
- next.assetPrefix = `/${app.getAssetPrefix()}`;
1631
1630
  }
1631
+ next.assetPrefix = assetPrefix;
1632
1632
  return {
1633
1633
  next
1634
1634
  };
@@ -1649,58 +1649,114 @@ function transform2(args) {
1649
1649
  };
1650
1650
  }
1651
1651
 
1652
- // src/next/config/transforms/headers.ts
1652
+ // src/next/config/transforms/redirects.ts
1653
1653
  function transform3(args) {
1654
- const { next, app } = args;
1655
- if (app.isDefault()) {
1656
- return {
1657
- next
1654
+ const { next, microfrontend, opts } = args;
1655
+ const isProduction2 = opts?.isProduction ?? false;
1656
+ const isDevEnv = (process.env.VERCEL_ENV ?? "development") === "development";
1657
+ const requireLocalProxyHeader = !isProduction2 && isDevEnv && Boolean(process.env.TURBO_TASK_HAS_MFE_PROXY) && !process.env.MFE_DISABLE_LOCAL_PROXY_REWRITE;
1658
+ if (requireLocalProxyHeader) {
1659
+ const proxyRedirect = {
1660
+ source: "/:path*",
1661
+ destination: `http://localhost:${microfrontend.getLocalProxyPort()}/:path*`,
1662
+ permanent: false,
1663
+ missing: [{ type: "header", key: "x-vercel-mfe-local-proxy-origin" }]
1658
1664
  };
1665
+ if (next.redirects && typeof next.redirects === "function") {
1666
+ const originalRedirectsFn = next.redirects;
1667
+ next.redirects = async () => {
1668
+ const originalRedirects = await originalRedirectsFn();
1669
+ return [proxyRedirect, ...originalRedirects];
1670
+ };
1671
+ } else {
1672
+ next.redirects = async () => [proxyRedirect];
1673
+ }
1659
1674
  }
1660
- const headersToAdd = [
1661
- {
1662
- source: "/:path*",
1663
- headers: [
1664
- {
1665
- key: "X-Vercel-Zone",
1666
- value: app.name
1667
- },
1668
- {
1669
- key: "X-Vercel-MFE-App",
1670
- value: app.name
1675
+ return { next };
1676
+ }
1677
+
1678
+ // src/next/config/transforms/rewrites.ts
1679
+ function debugRewrites(rewrites) {
1680
+ if (process.env.MFE_DEBUG === "true") {
1681
+ const indent = " ".repeat(4);
1682
+ const header = "rewrites (source \u2192 destination)";
1683
+ const separator = "\u23AF".repeat(header.length);
1684
+ const maxSourceLength = Math.max(
1685
+ ...rewrites.map((key) => key.source.length)
1686
+ );
1687
+ const table = rewrites.map((route, idx) => {
1688
+ const paddedSource = route.source.padEnd(maxSourceLength);
1689
+ return `${indent} ${idx + 1}. ${paddedSource} \u2192 ${route.destination}`;
1690
+ }).join("\n");
1691
+ console.log(`${indent}${header}
1692
+ ${indent}${separator}
1693
+ ${table}
1694
+ `);
1695
+ }
1696
+ }
1697
+ function rewritesMapToArr(rewrites) {
1698
+ return Array.from(rewrites.entries()).flatMap(([source, rewrite]) => {
1699
+ const destination = `${rewrite.destination.domain || ""}${rewrite.destination.pathname}`;
1700
+ if (source === destination)
1701
+ return [];
1702
+ return [
1703
+ {
1704
+ source,
1705
+ destination
1706
+ }
1707
+ ];
1708
+ });
1709
+ }
1710
+ function transform4(args) {
1711
+ const { next, app } = args;
1712
+ const buildBeforeFiles = () => {
1713
+ const rewrites = /* @__PURE__ */ new Map();
1714
+ if (!app.isDefault()) {
1715
+ rewrites.set(`/${app.getAssetPrefix()}/_next/:path+`, {
1716
+ destination: {
1717
+ pathname: `/_next/:path+`
1671
1718
  }
1672
- ]
1673
- },
1674
- {
1675
- source: "/:path*",
1676
- has: [
1677
- {
1678
- type: "host",
1679
- value: "(?<host>.*)"
1719
+ });
1720
+ rewrites.set(`/${app.getAssetPrefix()}/.well-known/vercel/flags`, {
1721
+ destination: {
1722
+ pathname: `/.well-known/vercel/flags`
1680
1723
  }
1681
- ],
1682
- headers: [
1683
- {
1684
- // TODO: we may want to revisit before launch whether we want to expose this
1685
- // value on all responses publicly since users may not want to reveal this host
1686
- // (maybe just in preview? or when the user is logged in to the toolbar?)
1687
- key: "X-Vercel-Host",
1688
- value: ":host"
1724
+ });
1725
+ rewrites.set(`/${app.getAssetPrefix()}/_vercel/:path*`, {
1726
+ destination: {
1727
+ pathname: `/_vercel/:path*`
1689
1728
  }
1690
- ]
1729
+ });
1691
1730
  }
1692
- ];
1693
- if (next.headers && typeof next.headers === "function") {
1694
- const originalHeadersFn = next.headers;
1695
- next.headers = async () => {
1696
- const originalHeaders = await originalHeadersFn();
1697
- return [...originalHeaders, ...headersToAdd];
1731
+ return rewritesMapToArr(rewrites);
1732
+ };
1733
+ const newBeforeFiles = buildBeforeFiles();
1734
+ if (next.rewrites && typeof next.rewrites === "function") {
1735
+ const originalRewritesFn = next.rewrites;
1736
+ next.rewrites = async () => {
1737
+ const originalRewrites = await originalRewritesFn();
1738
+ if (typeof originalRewrites === "object" && !Array.isArray(originalRewrites)) {
1739
+ const { beforeFiles = [] } = originalRewrites;
1740
+ return {
1741
+ beforeFiles: [...newBeforeFiles, ...beforeFiles],
1742
+ afterFiles: originalRewrites.afterFiles,
1743
+ fallback: originalRewrites.fallback
1744
+ };
1745
+ }
1746
+ return {
1747
+ beforeFiles: newBeforeFiles,
1748
+ afterFiles: originalRewrites,
1749
+ fallback: []
1750
+ };
1698
1751
  };
1699
1752
  } else {
1700
- next.headers = () => {
1701
- return Promise.resolve(headersToAdd);
1702
- };
1753
+ next.rewrites = async () => ({
1754
+ beforeFiles: newBeforeFiles,
1755
+ afterFiles: [],
1756
+ fallback: []
1757
+ });
1703
1758
  }
1759
+ debugRewrites(newBeforeFiles);
1704
1760
  return {
1705
1761
  next
1706
1762
  };
@@ -1789,138 +1845,6 @@ function getDomainForCurrentEnvironment(config, appName, opts = {}) {
1789
1845
  }
1790
1846
  }
1791
1847
 
1792
- // src/next/config/transforms/rewrites.ts
1793
- function debugRewrites(rewrites) {
1794
- if (process.env.MFE_DEBUG === "true") {
1795
- const indent = " ".repeat(4);
1796
- const header = "rewrites (source \u2192 destination)";
1797
- const separator = "\u23AF".repeat(header.length);
1798
- const maxSourceLength = Math.max(
1799
- ...rewrites.map((key) => key.source.length)
1800
- );
1801
- const table = rewrites.map((route, idx) => {
1802
- const paddedSource = route.source.padEnd(maxSourceLength);
1803
- return `${indent} ${idx + 1}. ${paddedSource} \u2192 ${route.destination}`;
1804
- }).join("\n");
1805
- console.log(`${indent}${header}
1806
- ${indent}${separator}
1807
- ${table}
1808
- `);
1809
- }
1810
- }
1811
- function pathToRewrites(path5) {
1812
- const regex = /(?<base>^.+)\/:.+\*$/;
1813
- const match = regex.exec(path5);
1814
- const paths = [path5];
1815
- if (match?.groups?.base) {
1816
- paths.unshift(match.groups.base);
1817
- }
1818
- return paths;
1819
- }
1820
- function rewritesMapToArr(rewrites) {
1821
- return Array.from(rewrites.entries()).flatMap(([source, rewrite]) => {
1822
- const destination = `${rewrite.destination.domain || ""}${rewrite.destination.pathname}`;
1823
- if (source === destination)
1824
- return [];
1825
- return [
1826
- {
1827
- source,
1828
- destination,
1829
- missing: [
1830
- // if this header is present, the proxy has performed the rewrite.
1831
- // once the proxy routing is fully rolled out, this package should
1832
- // be updated to not perform any rewrites when deployed.
1833
- {
1834
- type: "header",
1835
- key: "x-vercel-mfe-host"
1836
- }
1837
- ]
1838
- }
1839
- ];
1840
- });
1841
- }
1842
- function transform4(args) {
1843
- const { next, microfrontend, app } = args;
1844
- const buildBeforeFiles = () => {
1845
- const rewrites = /* @__PURE__ */ new Map();
1846
- if (!app.isDefault()) {
1847
- rewrites.set(`/${app.getAssetPrefix()}/_next/:path+`, {
1848
- destination: {
1849
- pathname: `/_next/:path+`
1850
- }
1851
- });
1852
- rewrites.set(`/${app.getAssetPrefix()}/.well-known/vercel/flags`, {
1853
- destination: {
1854
- pathname: `/.well-known/vercel/flags`
1855
- }
1856
- });
1857
- if (process.env.VERCEL_MICROFRONTENDS_CONSOLIDATE_SPEED_INSIGHTS === "1") {
1858
- rewrites.set(`/${app.getAssetPrefix()}/_vercel/:path*`, {
1859
- destination: { pathname: "/_vercel/:path*" }
1860
- });
1861
- }
1862
- } else if (microfrontend instanceof MicrofrontendMainConfig) {
1863
- for (const [_, a] of Object.entries(
1864
- microfrontend.getChildApplications()
1865
- )) {
1866
- const { routing } = a;
1867
- const domain = getDomainForCurrentEnvironment(microfrontend, a.name);
1868
- rewrites.set(`/${a.getAssetPrefix()}/:path+`, {
1869
- destination: {
1870
- domain,
1871
- pathname: `/${a.getAssetPrefix()}/:path+`
1872
- }
1873
- });
1874
- for (const group of routing) {
1875
- if (group.flag) {
1876
- continue;
1877
- } else {
1878
- for (const source of group.paths) {
1879
- const paths = pathToRewrites(source);
1880
- for (const p of paths) {
1881
- rewrites.set(p, {
1882
- destination: { domain, pathname: p }
1883
- });
1884
- }
1885
- }
1886
- }
1887
- }
1888
- }
1889
- }
1890
- return rewritesMapToArr(rewrites);
1891
- };
1892
- const newBeforeFiles = buildBeforeFiles();
1893
- if (next.rewrites && typeof next.rewrites === "function") {
1894
- const originalRewritesFn = next.rewrites;
1895
- next.rewrites = async () => {
1896
- const originalRewrites = await originalRewritesFn();
1897
- if (typeof originalRewrites === "object" && !Array.isArray(originalRewrites)) {
1898
- const { beforeFiles = [] } = originalRewrites;
1899
- return {
1900
- beforeFiles: [...newBeforeFiles, ...beforeFiles],
1901
- afterFiles: originalRewrites.afterFiles,
1902
- fallback: originalRewrites.fallback
1903
- };
1904
- }
1905
- return {
1906
- beforeFiles: newBeforeFiles,
1907
- afterFiles: originalRewrites,
1908
- fallback: []
1909
- };
1910
- };
1911
- } else {
1912
- next.rewrites = async () => ({
1913
- beforeFiles: newBeforeFiles,
1914
- afterFiles: [],
1915
- fallback: []
1916
- });
1917
- }
1918
- debugRewrites(newBeforeFiles);
1919
- return {
1920
- next
1921
- };
1922
- }
1923
-
1924
1848
  // src/next/config/transforms/server-actions.ts
1925
1849
  function debugRewrites2(allowedOrigins) {
1926
1850
  if (process.env.MFE_DEBUG === "true" && allowedOrigins) {
@@ -2042,7 +1966,7 @@ function transform6(args) {
2042
1966
  var transforms = {
2043
1967
  assetPrefix: transform,
2044
1968
  draftMode: transform2,
2045
- headers: transform3,
1969
+ redirects: transform3,
2046
1970
  rewrites: transform4,
2047
1971
  serverActions: transform5,
2048
1972
  webpack: transform6
@@ -2074,9 +1998,9 @@ function setEnvironment({
2074
1998
  NEXT_PUBLIC_MFE_CLIENT_CONFIG: JSON.stringify(
2075
1999
  microfrontends.config.toClientConfig().serialize()
2076
2000
  ),
2077
- ...process.env.ROUTE_OBSERVABILITY_TO_DEFAULT_APP || !app.getAssetPrefix() ? {} : {
2001
+ ...process.env.ROUTE_OBSERVABILITY_TO_THIS_PROJECT && app.getAssetPrefix() ? {
2078
2002
  NEXT_PUBLIC_VERCEL_OBSERVABILITY_BASEPATH: `/${app.getAssetPrefix()}/_vercel`
2079
- }
2003
+ } : {}
2080
2004
  };
2081
2005
  const serverEnvs = {
2082
2006
  MFE_CURRENT_APPLICATION: app.name,