@vercel/microfrontends 0.19.5 → 0.20.0

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' | 'redirects' | 'rewrites' | 'serverActions' | 'webpack';
3
+ type TransformKeys = 'assetPrefix' | 'draftMode' | 'redirects' | 'rewrites' | 'serverActions' | 'webpack';
4
4
 
5
5
  interface WithMicrofrontendsOptions {
6
6
  /**
@@ -1615,65 +1615,8 @@ function transform2(args) {
1615
1615
  };
1616
1616
  }
1617
1617
 
1618
- // src/next/config/transforms/headers.ts
1619
- function transform3(args) {
1620
- const { next, app } = args;
1621
- if (app.isDefault()) {
1622
- return {
1623
- next
1624
- };
1625
- }
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
1637
- }
1638
- ]
1639
- },
1640
- {
1641
- source: "/:path*",
1642
- has: [
1643
- {
1644
- type: "host",
1645
- value: "(?<host>.*)"
1646
- }
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"
1655
- }
1656
- ]
1657
- }
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];
1664
- };
1665
- } else {
1666
- next.headers = () => {
1667
- return Promise.resolve(headersToAdd);
1668
- };
1669
- }
1670
- return {
1671
- next
1672
- };
1673
- }
1674
-
1675
1618
  // src/next/config/transforms/redirects.ts
1676
- function transform4(args) {
1619
+ function transform3(args) {
1677
1620
  const { next, microfrontend, opts } = args;
1678
1621
  const isProduction2 = opts?.isProduction ?? false;
1679
1622
  const isDevEnv = (process.env.VERCEL_ENV ?? "development") === "development";
@@ -1698,6 +1641,98 @@ function transform4(args) {
1698
1641
  return { next };
1699
1642
  }
1700
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
+ if (process.env.VERCEL_ENV && process.env.VERCEL_ENV !== "development") {
1679
+ return {
1680
+ next
1681
+ };
1682
+ }
1683
+ const buildBeforeFiles = () => {
1684
+ const rewrites = /* @__PURE__ */ new Map();
1685
+ if (!app.isDefault()) {
1686
+ rewrites.set(`/${app.getAssetPrefix()}/_next/:path+`, {
1687
+ destination: {
1688
+ pathname: "/_next/:path+"
1689
+ }
1690
+ });
1691
+ rewrites.set(`/${app.getAssetPrefix()}/.well-known/vercel/flags`, {
1692
+ destination: {
1693
+ pathname: "/.well-known/vercel/flags"
1694
+ }
1695
+ });
1696
+ rewrites.set(`/${app.getAssetPrefix()}/_vercel/:path*`, {
1697
+ destination: {
1698
+ pathname: "/_vercel/:path*"
1699
+ }
1700
+ });
1701
+ }
1702
+ return rewritesMapToArr(rewrites);
1703
+ };
1704
+ const newBeforeFiles = buildBeforeFiles();
1705
+ if (next.rewrites && typeof next.rewrites === "function") {
1706
+ const originalRewritesFn = next.rewrites;
1707
+ next.rewrites = async () => {
1708
+ const originalRewrites = await originalRewritesFn();
1709
+ if (typeof originalRewrites === "object" && !Array.isArray(originalRewrites)) {
1710
+ const { beforeFiles = [] } = originalRewrites;
1711
+ return {
1712
+ beforeFiles: [...newBeforeFiles, ...beforeFiles],
1713
+ afterFiles: originalRewrites.afterFiles,
1714
+ fallback: originalRewrites.fallback
1715
+ };
1716
+ }
1717
+ return {
1718
+ beforeFiles: newBeforeFiles,
1719
+ afterFiles: originalRewrites,
1720
+ fallback: []
1721
+ };
1722
+ };
1723
+ } else {
1724
+ next.rewrites = async () => ({
1725
+ beforeFiles: newBeforeFiles,
1726
+ afterFiles: [],
1727
+ fallback: []
1728
+ });
1729
+ }
1730
+ debugRewrites(newBeforeFiles);
1731
+ return {
1732
+ next
1733
+ };
1734
+ }
1735
+
1701
1736
  // src/routing/get-domain-from-environment.ts
1702
1737
  function getDomainFromEnvironment({
1703
1738
  app,
@@ -1781,138 +1816,6 @@ function getDomainForCurrentEnvironment(config, appName, opts = {}) {
1781
1816
  }
1782
1817
  }
1783
1818
 
1784
- // src/next/config/transforms/rewrites.ts
1785
- function debugRewrites(rewrites) {
1786
- if (process.env.MFE_DEBUG === "true") {
1787
- const indent = " ".repeat(4);
1788
- const header = "rewrites (source \u2192 destination)";
1789
- const separator = "\u23AF".repeat(header.length);
1790
- const maxSourceLength = Math.max(
1791
- ...rewrites.map((key) => key.source.length)
1792
- );
1793
- const table = rewrites.map((route, idx) => {
1794
- const paddedSource = route.source.padEnd(maxSourceLength);
1795
- return `${indent} ${idx + 1}. ${paddedSource} \u2192 ${route.destination}`;
1796
- }).join("\n");
1797
- console.log(`${indent}${header}
1798
- ${indent}${separator}
1799
- ${table}
1800
- `);
1801
- }
1802
- }
1803
- function pathToRewrites(path5) {
1804
- const regex = /(?<base>^.+)\/:.+\*$/;
1805
- const match = regex.exec(path5);
1806
- const paths = [path5];
1807
- if (match?.groups?.base) {
1808
- paths.unshift(match.groups.base);
1809
- }
1810
- return paths;
1811
- }
1812
- function rewritesMapToArr(rewrites) {
1813
- return Array.from(rewrites.entries()).flatMap(([source, rewrite]) => {
1814
- const destination = `${rewrite.destination.domain || ""}${rewrite.destination.pathname}`;
1815
- if (source === destination)
1816
- return [];
1817
- return [
1818
- {
1819
- source,
1820
- destination,
1821
- missing: [
1822
- // if this header is present, the proxy has performed the rewrite.
1823
- // once the proxy routing is fully rolled out, this package should
1824
- // be updated to not perform any rewrites when deployed.
1825
- {
1826
- type: "header",
1827
- key: "x-vercel-mfe-host"
1828
- }
1829
- ]
1830
- }
1831
- ];
1832
- });
1833
- }
1834
- function transform5(args) {
1835
- const { next, microfrontend, app } = args;
1836
- const buildBeforeFiles = () => {
1837
- const rewrites = /* @__PURE__ */ new Map();
1838
- if (!app.isDefault()) {
1839
- rewrites.set(`/${app.getAssetPrefix()}/_next/:path+`, {
1840
- destination: {
1841
- pathname: `/_next/:path+`
1842
- }
1843
- });
1844
- rewrites.set(`/${app.getAssetPrefix()}/.well-known/vercel/flags`, {
1845
- destination: {
1846
- pathname: `/.well-known/vercel/flags`
1847
- }
1848
- });
1849
- if (process.env.VERCEL_MICROFRONTENDS_CONSOLIDATE_SPEED_INSIGHTS === "1") {
1850
- rewrites.set(`/${app.getAssetPrefix()}/_vercel/:path*`, {
1851
- destination: { pathname: "/_vercel/:path*" }
1852
- });
1853
- }
1854
- } else if (microfrontend instanceof MicrofrontendMainConfig) {
1855
- for (const [_, a] of Object.entries(
1856
- microfrontend.getChildApplications()
1857
- )) {
1858
- const { routing } = a;
1859
- const domain = getDomainForCurrentEnvironment(microfrontend, a.name);
1860
- rewrites.set(`/${a.getAssetPrefix()}/:path+`, {
1861
- destination: {
1862
- domain,
1863
- pathname: `/${a.getAssetPrefix()}/:path+`
1864
- }
1865
- });
1866
- for (const group of routing) {
1867
- if (group.flag) {
1868
- continue;
1869
- } else {
1870
- for (const source of group.paths) {
1871
- const paths = pathToRewrites(source);
1872
- for (const p of paths) {
1873
- rewrites.set(p, {
1874
- destination: { domain, pathname: p }
1875
- });
1876
- }
1877
- }
1878
- }
1879
- }
1880
- }
1881
- }
1882
- return rewritesMapToArr(rewrites);
1883
- };
1884
- const newBeforeFiles = buildBeforeFiles();
1885
- if (next.rewrites && typeof next.rewrites === "function") {
1886
- const originalRewritesFn = next.rewrites;
1887
- next.rewrites = async () => {
1888
- const originalRewrites = await originalRewritesFn();
1889
- if (typeof originalRewrites === "object" && !Array.isArray(originalRewrites)) {
1890
- const { beforeFiles = [] } = originalRewrites;
1891
- return {
1892
- beforeFiles: [...newBeforeFiles, ...beforeFiles],
1893
- afterFiles: originalRewrites.afterFiles,
1894
- fallback: originalRewrites.fallback
1895
- };
1896
- }
1897
- return {
1898
- beforeFiles: newBeforeFiles,
1899
- afterFiles: originalRewrites,
1900
- fallback: []
1901
- };
1902
- };
1903
- } else {
1904
- next.rewrites = async () => ({
1905
- beforeFiles: newBeforeFiles,
1906
- afterFiles: [],
1907
- fallback: []
1908
- });
1909
- }
1910
- debugRewrites(newBeforeFiles);
1911
- return {
1912
- next
1913
- };
1914
- }
1915
-
1916
1819
  // src/next/config/transforms/server-actions.ts
1917
1820
  function debugRewrites2(allowedOrigins) {
1918
1821
  if (process.env.MFE_DEBUG === "true" && allowedOrigins) {
@@ -1933,7 +1836,7 @@ ${table}
1933
1836
  }
1934
1837
  }
1935
1838
  var formatDomainForServerAction = (domain) => domain.replace(/https?:\/\//, "");
1936
- function transform6(args) {
1839
+ function transform5(args) {
1937
1840
  const { next, app, microfrontend } = args;
1938
1841
  if (microfrontend instanceof MicrofrontendChildConfig) {
1939
1842
  console.warn(
@@ -1990,7 +1893,7 @@ function transform6(args) {
1990
1893
  }
1991
1894
 
1992
1895
  // src/next/config/transforms/webpack.ts
1993
- function transform7(args) {
1896
+ function transform6(args) {
1994
1897
  const { next, microfrontend } = args;
1995
1898
  const configWithWebpack = {
1996
1899
  ...next,
@@ -2034,11 +1937,10 @@ function transform7(args) {
2034
1937
  var transforms = {
2035
1938
  assetPrefix: transform,
2036
1939
  draftMode: transform2,
2037
- headers: transform3,
2038
- redirects: transform4,
2039
- rewrites: transform5,
2040
- serverActions: transform6,
2041
- webpack: transform7
1940
+ redirects: transform3,
1941
+ rewrites: transform4,
1942
+ serverActions: transform5,
1943
+ webpack: transform6
2042
1944
  };
2043
1945
 
2044
1946
  // src/next/config/env.ts
@@ -2067,9 +1969,9 @@ function setEnvironment({
2067
1969
  NEXT_PUBLIC_MFE_CLIENT_CONFIG: JSON.stringify(
2068
1970
  microfrontends.config.toClientConfig().serialize()
2069
1971
  ),
2070
- ...process.env.ROUTE_OBSERVABILITY_TO_DEFAULT_APP || !app.getAssetPrefix() ? {} : {
1972
+ ...process.env.ROUTE_OBSERVABILITY_TO_THIS_PROJECT && app.getAssetPrefix() ? {
2071
1973
  NEXT_PUBLIC_VERCEL_OBSERVABILITY_BASEPATH: `/${app.getAssetPrefix()}/_vercel`
2072
- }
1974
+ } : {}
2073
1975
  };
2074
1976
  const serverEnvs = {
2075
1977
  MFE_CURRENT_APPLICATION: app.name,
@@ -2130,13 +2032,13 @@ function withMicrofrontends(nextConfig, opts) {
2130
2032
  const app = microfrontends.config.getApplication(fromApp);
2131
2033
  setEnvironment({ app, microfrontends });
2132
2034
  let next = { ...nextConfig };
2133
- for (const [key, transform8] of typedEntries(transforms)) {
2035
+ for (const [key, transform7] of typedEntries(transforms)) {
2134
2036
  if (opts?.skipTransforms?.includes(key)) {
2135
2037
  console.log(`Skipping ${key} transform`);
2136
2038
  continue;
2137
2039
  }
2138
2040
  try {
2139
- const transformedConfig = transform8({
2041
+ const transformedConfig = transform7({
2140
2042
  app,
2141
2043
  next,
2142
2044
  microfrontend: microfrontends.config,