@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.
@@ -1,6 +1,6 @@
1
1
  import { NextConfig } from 'next';
2
2
 
3
- type TransformKeys = 'assetPrefix' | 'draftMode' | 'headers' | 'rewrites' | 'serverActions' | 'webpack';
3
+ type TransformKeys = 'assetPrefix' | 'draftMode' | 'redirects' | 'rewrites' | 'serverActions' | 'webpack';
4
4
 
5
5
  interface WithMicrofrontendsOptions {
6
6
  /**
@@ -1588,13 +1588,13 @@ function transform(args) {
1588
1588
  next
1589
1589
  };
1590
1590
  }
1591
- if (next.assetPrefix !== void 0 && next.assetPrefix !== app.getAssetPrefix()) {
1592
- console.log(
1593
- `"assetPrefix" already set. This route must be manually configured in your microfrontend.json file.`
1591
+ const assetPrefix = `/${app.getAssetPrefix()}`;
1592
+ if (next.assetPrefix !== void 0 && next.assetPrefix !== assetPrefix) {
1593
+ throw new Error(
1594
+ `"assetPrefix" already set and does not equal "${assetPrefix}". Either omit the assetPrefix in your next config, or set it to "${assetPrefix}".`
1594
1595
  );
1595
- } else {
1596
- next.assetPrefix = `/${app.getAssetPrefix()}`;
1597
1596
  }
1597
+ next.assetPrefix = assetPrefix;
1598
1598
  return {
1599
1599
  next
1600
1600
  };
@@ -1615,58 +1615,114 @@ function transform2(args) {
1615
1615
  };
1616
1616
  }
1617
1617
 
1618
- // src/next/config/transforms/headers.ts
1618
+ // src/next/config/transforms/redirects.ts
1619
1619
  function transform3(args) {
1620
- const { next, app } = args;
1621
- if (app.isDefault()) {
1622
- return {
1623
- next
1620
+ const { next, microfrontend, opts } = args;
1621
+ const isProduction2 = opts?.isProduction ?? false;
1622
+ const isDevEnv = (process.env.VERCEL_ENV ?? "development") === "development";
1623
+ const requireLocalProxyHeader = !isProduction2 && isDevEnv && Boolean(process.env.TURBO_TASK_HAS_MFE_PROXY) && !process.env.MFE_DISABLE_LOCAL_PROXY_REWRITE;
1624
+ if (requireLocalProxyHeader) {
1625
+ const proxyRedirect = {
1626
+ source: "/:path*",
1627
+ destination: `http://localhost:${microfrontend.getLocalProxyPort()}/:path*`,
1628
+ permanent: false,
1629
+ missing: [{ type: "header", key: "x-vercel-mfe-local-proxy-origin" }]
1624
1630
  };
1631
+ if (next.redirects && typeof next.redirects === "function") {
1632
+ const originalRedirectsFn = next.redirects;
1633
+ next.redirects = async () => {
1634
+ const originalRedirects = await originalRedirectsFn();
1635
+ return [proxyRedirect, ...originalRedirects];
1636
+ };
1637
+ } else {
1638
+ next.redirects = async () => [proxyRedirect];
1639
+ }
1625
1640
  }
1626
- const headersToAdd = [
1627
- {
1628
- source: "/:path*",
1629
- headers: [
1630
- {
1631
- key: "X-Vercel-Zone",
1632
- value: app.name
1633
- },
1634
- {
1635
- key: "X-Vercel-MFE-App",
1636
- value: app.name
1641
+ return { next };
1642
+ }
1643
+
1644
+ // src/next/config/transforms/rewrites.ts
1645
+ function debugRewrites(rewrites) {
1646
+ if (process.env.MFE_DEBUG === "true") {
1647
+ const indent = " ".repeat(4);
1648
+ const header = "rewrites (source \u2192 destination)";
1649
+ const separator = "\u23AF".repeat(header.length);
1650
+ const maxSourceLength = Math.max(
1651
+ ...rewrites.map((key) => key.source.length)
1652
+ );
1653
+ const table = rewrites.map((route, idx) => {
1654
+ const paddedSource = route.source.padEnd(maxSourceLength);
1655
+ return `${indent} ${idx + 1}. ${paddedSource} \u2192 ${route.destination}`;
1656
+ }).join("\n");
1657
+ console.log(`${indent}${header}
1658
+ ${indent}${separator}
1659
+ ${table}
1660
+ `);
1661
+ }
1662
+ }
1663
+ function rewritesMapToArr(rewrites) {
1664
+ return Array.from(rewrites.entries()).flatMap(([source, rewrite]) => {
1665
+ const destination = `${rewrite.destination.domain || ""}${rewrite.destination.pathname}`;
1666
+ if (source === destination)
1667
+ return [];
1668
+ return [
1669
+ {
1670
+ source,
1671
+ destination
1672
+ }
1673
+ ];
1674
+ });
1675
+ }
1676
+ function transform4(args) {
1677
+ const { next, app } = args;
1678
+ const buildBeforeFiles = () => {
1679
+ const rewrites = /* @__PURE__ */ new Map();
1680
+ if (!app.isDefault()) {
1681
+ rewrites.set(`/${app.getAssetPrefix()}/_next/:path+`, {
1682
+ destination: {
1683
+ pathname: `/_next/:path+`
1637
1684
  }
1638
- ]
1639
- },
1640
- {
1641
- source: "/:path*",
1642
- has: [
1643
- {
1644
- type: "host",
1645
- value: "(?<host>.*)"
1685
+ });
1686
+ rewrites.set(`/${app.getAssetPrefix()}/.well-known/vercel/flags`, {
1687
+ destination: {
1688
+ pathname: `/.well-known/vercel/flags`
1646
1689
  }
1647
- ],
1648
- headers: [
1649
- {
1650
- // TODO: we may want to revisit before launch whether we want to expose this
1651
- // value on all responses publicly since users may not want to reveal this host
1652
- // (maybe just in preview? or when the user is logged in to the toolbar?)
1653
- key: "X-Vercel-Host",
1654
- value: ":host"
1690
+ });
1691
+ rewrites.set(`/${app.getAssetPrefix()}/_vercel/:path*`, {
1692
+ destination: {
1693
+ pathname: `/_vercel/:path*`
1655
1694
  }
1656
- ]
1695
+ });
1657
1696
  }
1658
- ];
1659
- if (next.headers && typeof next.headers === "function") {
1660
- const originalHeadersFn = next.headers;
1661
- next.headers = async () => {
1662
- const originalHeaders = await originalHeadersFn();
1663
- return [...originalHeaders, ...headersToAdd];
1697
+ return rewritesMapToArr(rewrites);
1698
+ };
1699
+ const newBeforeFiles = buildBeforeFiles();
1700
+ if (next.rewrites && typeof next.rewrites === "function") {
1701
+ const originalRewritesFn = next.rewrites;
1702
+ next.rewrites = async () => {
1703
+ const originalRewrites = await originalRewritesFn();
1704
+ if (typeof originalRewrites === "object" && !Array.isArray(originalRewrites)) {
1705
+ const { beforeFiles = [] } = originalRewrites;
1706
+ return {
1707
+ beforeFiles: [...newBeforeFiles, ...beforeFiles],
1708
+ afterFiles: originalRewrites.afterFiles,
1709
+ fallback: originalRewrites.fallback
1710
+ };
1711
+ }
1712
+ return {
1713
+ beforeFiles: newBeforeFiles,
1714
+ afterFiles: originalRewrites,
1715
+ fallback: []
1716
+ };
1664
1717
  };
1665
1718
  } else {
1666
- next.headers = () => {
1667
- return Promise.resolve(headersToAdd);
1668
- };
1719
+ next.rewrites = async () => ({
1720
+ beforeFiles: newBeforeFiles,
1721
+ afterFiles: [],
1722
+ fallback: []
1723
+ });
1669
1724
  }
1725
+ debugRewrites(newBeforeFiles);
1670
1726
  return {
1671
1727
  next
1672
1728
  };
@@ -1755,138 +1811,6 @@ function getDomainForCurrentEnvironment(config, appName, opts = {}) {
1755
1811
  }
1756
1812
  }
1757
1813
 
1758
- // src/next/config/transforms/rewrites.ts
1759
- function debugRewrites(rewrites) {
1760
- if (process.env.MFE_DEBUG === "true") {
1761
- const indent = " ".repeat(4);
1762
- const header = "rewrites (source \u2192 destination)";
1763
- const separator = "\u23AF".repeat(header.length);
1764
- const maxSourceLength = Math.max(
1765
- ...rewrites.map((key) => key.source.length)
1766
- );
1767
- const table = rewrites.map((route, idx) => {
1768
- const paddedSource = route.source.padEnd(maxSourceLength);
1769
- return `${indent} ${idx + 1}. ${paddedSource} \u2192 ${route.destination}`;
1770
- }).join("\n");
1771
- console.log(`${indent}${header}
1772
- ${indent}${separator}
1773
- ${table}
1774
- `);
1775
- }
1776
- }
1777
- function pathToRewrites(path5) {
1778
- const regex = /(?<base>^.+)\/:.+\*$/;
1779
- const match = regex.exec(path5);
1780
- const paths = [path5];
1781
- if (match?.groups?.base) {
1782
- paths.unshift(match.groups.base);
1783
- }
1784
- return paths;
1785
- }
1786
- function rewritesMapToArr(rewrites) {
1787
- return Array.from(rewrites.entries()).flatMap(([source, rewrite]) => {
1788
- const destination = `${rewrite.destination.domain || ""}${rewrite.destination.pathname}`;
1789
- if (source === destination)
1790
- return [];
1791
- return [
1792
- {
1793
- source,
1794
- destination,
1795
- missing: [
1796
- // if this header is present, the proxy has performed the rewrite.
1797
- // once the proxy routing is fully rolled out, this package should
1798
- // be updated to not perform any rewrites when deployed.
1799
- {
1800
- type: "header",
1801
- key: "x-vercel-mfe-host"
1802
- }
1803
- ]
1804
- }
1805
- ];
1806
- });
1807
- }
1808
- function transform4(args) {
1809
- const { next, microfrontend, app } = args;
1810
- const buildBeforeFiles = () => {
1811
- const rewrites = /* @__PURE__ */ new Map();
1812
- if (!app.isDefault()) {
1813
- rewrites.set(`/${app.getAssetPrefix()}/_next/:path+`, {
1814
- destination: {
1815
- pathname: `/_next/:path+`
1816
- }
1817
- });
1818
- rewrites.set(`/${app.getAssetPrefix()}/.well-known/vercel/flags`, {
1819
- destination: {
1820
- pathname: `/.well-known/vercel/flags`
1821
- }
1822
- });
1823
- if (process.env.VERCEL_MICROFRONTENDS_CONSOLIDATE_SPEED_INSIGHTS === "1") {
1824
- rewrites.set(`/${app.getAssetPrefix()}/_vercel/:path*`, {
1825
- destination: { pathname: "/_vercel/:path*" }
1826
- });
1827
- }
1828
- } else if (microfrontend instanceof MicrofrontendMainConfig) {
1829
- for (const [_, a] of Object.entries(
1830
- microfrontend.getChildApplications()
1831
- )) {
1832
- const { routing } = a;
1833
- const domain = getDomainForCurrentEnvironment(microfrontend, a.name);
1834
- rewrites.set(`/${a.getAssetPrefix()}/:path+`, {
1835
- destination: {
1836
- domain,
1837
- pathname: `/${a.getAssetPrefix()}/:path+`
1838
- }
1839
- });
1840
- for (const group of routing) {
1841
- if (group.flag) {
1842
- continue;
1843
- } else {
1844
- for (const source of group.paths) {
1845
- const paths = pathToRewrites(source);
1846
- for (const p of paths) {
1847
- rewrites.set(p, {
1848
- destination: { domain, pathname: p }
1849
- });
1850
- }
1851
- }
1852
- }
1853
- }
1854
- }
1855
- }
1856
- return rewritesMapToArr(rewrites);
1857
- };
1858
- const newBeforeFiles = buildBeforeFiles();
1859
- if (next.rewrites && typeof next.rewrites === "function") {
1860
- const originalRewritesFn = next.rewrites;
1861
- next.rewrites = async () => {
1862
- const originalRewrites = await originalRewritesFn();
1863
- if (typeof originalRewrites === "object" && !Array.isArray(originalRewrites)) {
1864
- const { beforeFiles = [] } = originalRewrites;
1865
- return {
1866
- beforeFiles: [...newBeforeFiles, ...beforeFiles],
1867
- afterFiles: originalRewrites.afterFiles,
1868
- fallback: originalRewrites.fallback
1869
- };
1870
- }
1871
- return {
1872
- beforeFiles: newBeforeFiles,
1873
- afterFiles: originalRewrites,
1874
- fallback: []
1875
- };
1876
- };
1877
- } else {
1878
- next.rewrites = async () => ({
1879
- beforeFiles: newBeforeFiles,
1880
- afterFiles: [],
1881
- fallback: []
1882
- });
1883
- }
1884
- debugRewrites(newBeforeFiles);
1885
- return {
1886
- next
1887
- };
1888
- }
1889
-
1890
1814
  // src/next/config/transforms/server-actions.ts
1891
1815
  function debugRewrites2(allowedOrigins) {
1892
1816
  if (process.env.MFE_DEBUG === "true" && allowedOrigins) {
@@ -2008,7 +1932,7 @@ function transform6(args) {
2008
1932
  var transforms = {
2009
1933
  assetPrefix: transform,
2010
1934
  draftMode: transform2,
2011
- headers: transform3,
1935
+ redirects: transform3,
2012
1936
  rewrites: transform4,
2013
1937
  serverActions: transform5,
2014
1938
  webpack: transform6
@@ -2040,9 +1964,9 @@ function setEnvironment({
2040
1964
  NEXT_PUBLIC_MFE_CLIENT_CONFIG: JSON.stringify(
2041
1965
  microfrontends.config.toClientConfig().serialize()
2042
1966
  ),
2043
- ...process.env.ROUTE_OBSERVABILITY_TO_DEFAULT_APP || !app.getAssetPrefix() ? {} : {
1967
+ ...process.env.ROUTE_OBSERVABILITY_TO_THIS_PROJECT && app.getAssetPrefix() ? {
2044
1968
  NEXT_PUBLIC_VERCEL_OBSERVABILITY_BASEPATH: `/${app.getAssetPrefix()}/_vercel`
2045
- }
1969
+ } : {}
2046
1970
  };
2047
1971
  const serverEnvs = {
2048
1972
  MFE_CURRENT_APPLICATION: app.name,