@vercel/microfrontends 0.10.1 → 0.11.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.
Files changed (53) hide show
  1. package/dist/bin/cli.cjs +261 -91
  2. package/dist/{index-acb44057.d.ts → index-a99d5459.d.ts} +2 -2
  3. package/dist/next/config.cjs +10 -1
  4. package/dist/next/config.cjs.map +1 -1
  5. package/dist/next/config.js +10 -1
  6. package/dist/next/config.js.map +1 -1
  7. package/dist/next/middleware.cjs +17 -1
  8. package/dist/next/middleware.cjs.map +1 -1
  9. package/dist/next/middleware.js +17 -1
  10. package/dist/next/middleware.js.map +1 -1
  11. package/dist/{types-7b1cd9f7.d.ts → types-0030abae.d.ts} +0 -1
  12. package/dist/{types-c3d15d04.d.ts → types-c777c2f5.d.ts} +1 -1
  13. package/dist/v2/config.cjs +1 -1
  14. package/dist/v2/config.cjs.map +1 -1
  15. package/dist/v2/config.d.ts +3 -3
  16. package/dist/v2/config.js +1 -1
  17. package/dist/v2/config.js.map +1 -1
  18. package/dist/v2/microfrontends/server.cjs +214 -44
  19. package/dist/v2/microfrontends/server.cjs.map +1 -1
  20. package/dist/v2/microfrontends/server.d.ts +22 -3
  21. package/dist/v2/microfrontends/server.js +214 -44
  22. package/dist/v2/microfrontends/server.js.map +1 -1
  23. package/dist/v2/microfrontends.cjs +1 -1
  24. package/dist/v2/microfrontends.cjs.map +1 -1
  25. package/dist/v2/microfrontends.d.ts +3 -3
  26. package/dist/v2/microfrontends.js +1 -1
  27. package/dist/v2/microfrontends.js.map +1 -1
  28. package/dist/v2/next/config.cjs +248 -73
  29. package/dist/v2/next/config.cjs.map +1 -1
  30. package/dist/v2/next/config.js +248 -73
  31. package/dist/v2/next/config.js.map +1 -1
  32. package/dist/v2/next/endpoints.cjs +5 -2
  33. package/dist/v2/next/endpoints.cjs.map +1 -1
  34. package/dist/v2/next/endpoints.d.ts +1 -1
  35. package/dist/v2/next/endpoints.js +5 -2
  36. package/dist/v2/next/endpoints.js.map +1 -1
  37. package/dist/v2/next/middleware.cjs +107 -87
  38. package/dist/v2/next/middleware.cjs.map +1 -1
  39. package/dist/v2/next/middleware.js +107 -87
  40. package/dist/v2/next/middleware.js.map +1 -1
  41. package/dist/v2/overrides.cjs +1 -1
  42. package/dist/v2/overrides.cjs.map +1 -1
  43. package/dist/v2/overrides.d.ts +3 -3
  44. package/dist/v2/overrides.js +1 -1
  45. package/dist/v2/overrides.js.map +1 -1
  46. package/dist/v2/schema.d.ts +1 -1
  47. package/dist/validation.cjs +0 -3
  48. package/dist/validation.cjs.map +1 -1
  49. package/dist/validation.d.ts +0 -1
  50. package/dist/validation.js +0 -3
  51. package/dist/validation.js.map +1 -1
  52. package/package.json +7 -7
  53. package/schema/schema-v2.json +0 -3
@@ -126,9 +126,12 @@ async function getWellKnownClientData(clientConfig, flagValues = {}) {
126
126
  }
127
127
 
128
128
  // src/next-v2/endpoints/index.ts
129
- async function wellKnownNextjsClientConfigAppRoute() {
129
+ async function wellKnownNextjsClientConfigAppRoute(flagValues) {
130
130
  return import_server.NextResponse.json(
131
- await getWellKnownClientData(process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG)
131
+ await getWellKnownClientData(
132
+ process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG,
133
+ flagValues
134
+ )
132
135
  );
133
136
  }
134
137
  // Annotate the CommonJS export names for ESM import in node:
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/next-v2/endpoints/index.ts","../../../src/config-v2/microfrontends-config/client/index.ts","../../../src/config-v2/well-known/endpoints.ts"],"sourcesContent":["import { NextResponse } from 'next/server';\nimport { getWellKnownClientData } from '../../config-v2/well-known/endpoints';\nimport type { WellKnownClientData } from '../../config-v2/well-known/types';\n\n/**\n * A Next.js App Router API handler to export the micro-frontends client config\n * data.\n *\n * @example In the `app/.well-known/vercel/micro-frontends/client-config/route.ts` file,\n * add this code:\n * ```\n * export { wellKnownNextjsClientConfigAppRoute as GET } from '@vercel/microfrontends/next/endpoints';\n * ```\n */\nexport async function wellKnownNextjsClientConfigAppRoute(): Promise<NextResponse> {\n return NextResponse.json<WellKnownClientData>(\n await getWellKnownClientData(process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG),\n );\n}\n","import { pathToRegexp } from 'path-to-regexp';\nimport type { ClientConfig } from './types';\n\ninterface MicrofrontendConfigClientOptions {\n removeFlaggedPaths?: boolean;\n}\n\nexport class MicrofrontendConfigClient {\n applications: ClientConfig['applications'];\n pathCache: Record<string, string> = {};\n private readonly serialized: ClientConfig;\n\n constructor(config: ClientConfig, opts?: MicrofrontendConfigClientOptions) {\n this.serialized = config;\n if (opts?.removeFlaggedPaths) {\n for (const app of Object.values(config.applications)) {\n if (app.routing) {\n app.routing = app.routing.filter((match) => !match.flag);\n }\n }\n }\n this.applications = config.applications;\n }\n\n /**\n * Create a new `MicrofrontendConfigClient` from a JSON string.\n * Config must be passed in to remain framework agnostic\n */\n static fromEnv(\n config: string | undefined,\n opts?: MicrofrontendConfigClientOptions,\n ): MicrofrontendConfigClient {\n if (!config) {\n throw new Error('No microfrontends configuration found');\n }\n return new MicrofrontendConfigClient(\n JSON.parse(config) as ClientConfig,\n opts,\n );\n }\n\n isEqual(other: MicrofrontendConfigClient): boolean {\n return (\n JSON.stringify(this.applications) === JSON.stringify(other.applications)\n );\n }\n\n getApplicationNameForPath(path: string): string | null {\n if (!path.startsWith('/')) {\n throw new Error(`Path must start with a /`);\n }\n\n if (this.pathCache[path]) {\n return this.pathCache[path];\n }\n\n const pathname = new URL(path, 'https://example.com').pathname;\n for (const [name, application] of Object.entries(this.applications)) {\n if (application.routing) {\n for (const group of application.routing) {\n for (const childPath of group.paths) {\n const regexp = pathToRegexp(childPath);\n if (regexp.test(pathname)) {\n this.pathCache[path] = name;\n return name;\n }\n }\n }\n }\n }\n const defaultApplication = Object.entries(this.applications).find(\n ([, application]) => application.default,\n );\n if (!defaultApplication) {\n return null;\n }\n\n this.pathCache[path] = defaultApplication[0];\n return defaultApplication[0];\n }\n\n serialize(): ClientConfig {\n return this.serialized;\n }\n}\n","import { MicrofrontendConfigClient } from '../microfrontends-config/client';\nimport type { WellKnownClientData } from './types';\n\n/**\n * Returns data used by the client to ensure that navigations across\n * microfrontend boundaries are routed and prefetched correctly. The client\n * configuration is safe to expose to users.\n *\n * This data should be exposed in a `.well-known/vercel/micro-frontends/client-config`\n * endpoint.\n */\nexport async function getWellKnownClientData(\n clientConfig: string | undefined,\n flagValues: Record<string, () => Promise<boolean>> = {},\n): Promise<WellKnownClientData> {\n const config = MicrofrontendConfigClient.fromEnv(clientConfig);\n for (const [applicationName, application] of Object.entries(\n config.applications,\n )) {\n if (!application.routing) {\n continue;\n }\n const newRoutingMatches = [];\n for (const pathGroup of application.routing) {\n if (pathGroup.flag) {\n const flagName = pathGroup.flag;\n const flagFn = flagValues[flagName];\n if (!flagFn) {\n throw new Error(\n `Flag \"${flagName}\" was specified to control routing for path group \"${pathGroup.group}\" in application ${applicationName} but not found in provided flag values.`,\n );\n }\n // eslint-disable-next-line no-await-in-loop\n const flagEnabled = await flagFn();\n if (flagEnabled) {\n newRoutingMatches.push(pathGroup);\n }\n } else {\n newRoutingMatches.push(pathGroup);\n }\n }\n application.routing = newRoutingMatches;\n }\n\n return {\n config: config.serialize(),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA6B;;;ACA7B,4BAA6B;AAOtB,IAAM,4BAAN,MAAgC;AAAA,EAKrC,YAAY,QAAsB,MAAyC;AAH3E,qBAAoC,CAAC;AAInC,SAAK,aAAa;AAClB,QAAI,6BAAM,oBAAoB;AAC5B,iBAAW,OAAO,OAAO,OAAO,OAAO,YAAY,GAAG;AACpD,YAAI,IAAI,SAAS;AACf,cAAI,UAAU,IAAI,QAAQ,OAAO,CAAC,UAAU,CAAC,MAAM,IAAI;AAAA,QACzD;AAAA,MACF;AAAA,IACF;AACA,SAAK,eAAe,OAAO;AAAA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ACxB;AAC9B,QAAM,SAAS,0BAA0B,QAAQ,YAAY;AAC7D,aAAW,CAAC,iBAAiB,WAAW,KAAK,OAAO;AAAA,IAClD,OAAO;AAAA,EACT,GAAG;AACD,QAAI,CAAC,YAAY,SAAS;AACxB;AAAA,IACF;AACA,UAAM,oBAAoB,CAAC;AAC3B,eAAW,aAAa,YAAY,SAAS;AAC3C,UAAI,UAAU,MAAM;AAClB,cAAM,WAAW,UAAU;AAC3B,cAAM,SAAS,WAAW,QAAQ;AAClC,YAAI,CAAC,QAAQ;AACX,gBAAM,IAAI;AAAA,YACR,SAAS,8DAA8D,UAAU,yBAAyB;AAAA,UAC5G;AAAA,QACF;AAEA,cAAM,cAAc,MAAM,OAAO;AACjC,YAAI,aAAa;AACf,4BAAkB,KAAK,SAAS;AAAA,QAClC;AAAA,MACF,OAAO;AACL,0BAAkB,KAAK,SAAS;AAAA,MAClC;AAAA,IACF;AACA,gBAAY,UAAU;AAAA,EACxB;AAEA,SAAO;AAAA,IACL,QAAQ,OAAO,UAAU;AAAA,EAC3B;AACF;;;AFjCA,eAAsB,sCAA6D;AACjF,SAAO,2BAAa;AAAA,IAClB,MAAM,uBAAuB,QAAQ,IAAI,6BAA6B;AAAA,EACxE;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/next-v2/endpoints/index.ts","../../../src/config-v2/microfrontends-config/client/index.ts","../../../src/config-v2/well-known/endpoints.ts"],"sourcesContent":["import { NextResponse } from 'next/server';\nimport { getWellKnownClientData } from '../../config-v2/well-known/endpoints';\nimport type { WellKnownClientData } from '../../config-v2/well-known/types';\n\n/**\n * A Next.js App Router API handler to export the micro-frontends client config\n * data.\n *\n * @example In the `app/.well-known/vercel/micro-frontends/client-config/route.ts` file,\n * add this code:\n * ```\n * export { wellKnownNextjsClientConfigAppRoute as GET } from '@vercel/microfrontends/next/endpoints';\n * ```\n */\nexport async function wellKnownNextjsClientConfigAppRoute(\n flagValues: Record<string, () => Promise<boolean>>,\n): Promise<NextResponse> {\n return NextResponse.json<WellKnownClientData>(\n await getWellKnownClientData(\n process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG,\n flagValues,\n ),\n );\n}\n","import { pathToRegexp } from 'path-to-regexp';\nimport type { ClientConfig } from './types';\n\ninterface MicrofrontendConfigClientOptions {\n removeFlaggedPaths?: boolean;\n}\n\nexport class MicrofrontendConfigClient {\n applications: ClientConfig['applications'];\n pathCache: Record<string, string> = {};\n private readonly serialized: ClientConfig;\n\n constructor(config: ClientConfig, opts?: MicrofrontendConfigClientOptions) {\n this.serialized = config;\n if (opts?.removeFlaggedPaths) {\n for (const app of Object.values(config.applications)) {\n if (app.routing) {\n app.routing = app.routing.filter((match) => !match.flag);\n }\n }\n }\n this.applications = config.applications;\n }\n\n /**\n * Create a new `MicrofrontendConfigClient` from a JSON string.\n * Config must be passed in to remain framework agnostic\n */\n static fromEnv(\n config: string | undefined,\n opts?: MicrofrontendConfigClientOptions,\n ): MicrofrontendConfigClient {\n if (!config) {\n throw new Error('No microfrontends configuration found');\n }\n return new MicrofrontendConfigClient(\n JSON.parse(config) as ClientConfig,\n opts,\n );\n }\n\n isEqual(other: MicrofrontendConfigClient): boolean {\n return (\n JSON.stringify(this.applications) === JSON.stringify(other.applications)\n );\n }\n\n getApplicationNameForPath(path: string): string | null {\n if (!path.startsWith('/')) {\n throw new Error(`Path must start with a /`);\n }\n\n if (this.pathCache[path]) {\n return this.pathCache[path];\n }\n\n const pathname = new URL(path, 'https://example.com').pathname;\n for (const [name, application] of Object.entries(this.applications)) {\n if (application.routing) {\n for (const group of application.routing) {\n for (const childPath of group.paths) {\n const regexp = pathToRegexp(childPath);\n if (regexp.test(pathname)) {\n this.pathCache[path] = name;\n return name;\n }\n }\n }\n }\n }\n const defaultApplication = Object.entries(this.applications).find(\n ([, application]) => application.default,\n );\n if (!defaultApplication) {\n return null;\n }\n\n this.pathCache[path] = defaultApplication[0];\n return defaultApplication[0];\n }\n\n serialize(): ClientConfig {\n return this.serialized;\n }\n}\n","import { MicrofrontendConfigClient } from '../microfrontends-config/client';\nimport type { WellKnownClientData } from './types';\n\n/**\n * Returns data used by the client to ensure that navigations across\n * microfrontend boundaries are routed and prefetched correctly. The client\n * configuration is safe to expose to users.\n *\n * This data should be exposed in a `.well-known/vercel/micro-frontends/client-config`\n * endpoint.\n */\nexport async function getWellKnownClientData(\n clientConfig: string | undefined,\n flagValues: Record<string, () => Promise<boolean>> = {},\n): Promise<WellKnownClientData> {\n const config = MicrofrontendConfigClient.fromEnv(clientConfig);\n for (const [applicationName, application] of Object.entries(\n config.applications,\n )) {\n if (!application.routing) {\n continue;\n }\n const newRoutingMatches = [];\n for (const pathGroup of application.routing) {\n if (pathGroup.flag) {\n const flagName = pathGroup.flag;\n const flagFn = flagValues[flagName];\n if (!flagFn) {\n throw new Error(\n `Flag \"${flagName}\" was specified to control routing for path group \"${pathGroup.group}\" in application ${applicationName} but not found in provided flag values.`,\n );\n }\n // eslint-disable-next-line no-await-in-loop\n const flagEnabled = await flagFn();\n if (flagEnabled) {\n newRoutingMatches.push(pathGroup);\n }\n } else {\n newRoutingMatches.push(pathGroup);\n }\n }\n application.routing = newRoutingMatches;\n }\n\n return {\n config: config.serialize(),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA6B;;;ACA7B,4BAA6B;AAOtB,IAAM,4BAAN,MAAgC;AAAA,EAKrC,YAAY,QAAsB,MAAyC;AAH3E,qBAAoC,CAAC;AAInC,SAAK,aAAa;AAClB,QAAI,6BAAM,oBAAoB;AAC5B,iBAAW,OAAO,OAAO,OAAO,OAAO,YAAY,GAAG;AACpD,YAAI,IAAI,SAAS;AACf,cAAI,UAAU,IAAI,QAAQ,OAAO,CAAC,UAAU,CAAC,MAAM,IAAI;AAAA,QACzD;AAAA,MACF;AAAA,IACF;AACA,SAAK,eAAe,OAAO;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,QACL,QAC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,CAAC,iBAAiB,WAAW,KAAK,OAAO;AAAA,IAClD,OAAO;AAAA,EACT,GAAG;AACD,QAAI,CAAC,YAAY,SAAS;AACxB;AAAA,IACF;AACA,UAAM,oBAAoB,CAAC;AAC3B,eAAW,aAAa,YAAY,SAAS;AAC3C,UAAI,UAAU,MAAM;AAClB,cAAM,WAAW,UAAU;AAC3B,cAAM,SAAS,WAAW,QAAQ;AAClC,YAAI,CAAC,QAAQ;AACX,gBAAM,IAAI;AAAA,YACR,SAAS,8DAA8D,UAAU,yBAAyB;AAAA,UAC5G;AAAA,QACF;AAEA,cAAM,cAAc,MAAM,OAAO;AACjC,YAAI,aAAa;AACf,4BAAkB,KAAK,SAAS;AAAA,QAClC;AAAA,MACF,OAAO;AACL,0BAAkB,KAAK,SAAS;AAAA,MAClC;AAAA,IACF;AACA,gBAAY,UAAU;AAAA,EACxB;AAEA,SAAO;AAAA,IACL,QAAQ,OAAO,UAAU;AAAA,EAC3B;AACF;;;AFjCA,eAAsB,oCACpB,YACuB;AACvB,SAAO,2BAAa;AAAA,IAClB,MAAM;AAAA,MACJ,QAAQ,IAAI;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
@@ -10,6 +10,6 @@ import { NextResponse } from 'next/server';
10
10
  * export { wellKnownNextjsClientConfigAppRoute as GET } from '@vercel/microfrontends/next/endpoints';
11
11
  * ```
12
12
  */
13
- declare function wellKnownNextjsClientConfigAppRoute(): Promise<NextResponse>;
13
+ declare function wellKnownNextjsClientConfigAppRoute(flagValues: Record<string, () => Promise<boolean>>): Promise<NextResponse>;
14
14
 
15
15
  export { wellKnownNextjsClientConfigAppRoute };
@@ -102,9 +102,12 @@ async function getWellKnownClientData(clientConfig, flagValues = {}) {
102
102
  }
103
103
 
104
104
  // src/next-v2/endpoints/index.ts
105
- async function wellKnownNextjsClientConfigAppRoute() {
105
+ async function wellKnownNextjsClientConfigAppRoute(flagValues) {
106
106
  return NextResponse.json(
107
- await getWellKnownClientData(process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG)
107
+ await getWellKnownClientData(
108
+ process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG,
109
+ flagValues
110
+ )
108
111
  );
109
112
  }
110
113
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/next-v2/endpoints/index.ts","../../../src/config-v2/microfrontends-config/client/index.ts","../../../src/config-v2/well-known/endpoints.ts"],"sourcesContent":["import { NextResponse } from 'next/server';\nimport { getWellKnownClientData } from '../../config-v2/well-known/endpoints';\nimport type { WellKnownClientData } from '../../config-v2/well-known/types';\n\n/**\n * A Next.js App Router API handler to export the micro-frontends client config\n * data.\n *\n * @example In the `app/.well-known/vercel/micro-frontends/client-config/route.ts` file,\n * add this code:\n * ```\n * export { wellKnownNextjsClientConfigAppRoute as GET } from '@vercel/microfrontends/next/endpoints';\n * ```\n */\nexport async function wellKnownNextjsClientConfigAppRoute(): Promise<NextResponse> {\n return NextResponse.json<WellKnownClientData>(\n await getWellKnownClientData(process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG),\n );\n}\n","import { pathToRegexp } from 'path-to-regexp';\nimport type { ClientConfig } from './types';\n\ninterface MicrofrontendConfigClientOptions {\n removeFlaggedPaths?: boolean;\n}\n\nexport class MicrofrontendConfigClient {\n applications: ClientConfig['applications'];\n pathCache: Record<string, string> = {};\n private readonly serialized: ClientConfig;\n\n constructor(config: ClientConfig, opts?: MicrofrontendConfigClientOptions) {\n this.serialized = config;\n if (opts?.removeFlaggedPaths) {\n for (const app of Object.values(config.applications)) {\n if (app.routing) {\n app.routing = app.routing.filter((match) => !match.flag);\n }\n }\n }\n this.applications = config.applications;\n }\n\n /**\n * Create a new `MicrofrontendConfigClient` from a JSON string.\n * Config must be passed in to remain framework agnostic\n */\n static fromEnv(\n config: string | undefined,\n opts?: MicrofrontendConfigClientOptions,\n ): MicrofrontendConfigClient {\n if (!config) {\n throw new Error('No microfrontends configuration found');\n }\n return new MicrofrontendConfigClient(\n JSON.parse(config) as ClientConfig,\n opts,\n );\n }\n\n isEqual(other: MicrofrontendConfigClient): boolean {\n return (\n JSON.stringify(this.applications) === JSON.stringify(other.applications)\n );\n }\n\n getApplicationNameForPath(path: string): string | null {\n if (!path.startsWith('/')) {\n throw new Error(`Path must start with a /`);\n }\n\n if (this.pathCache[path]) {\n return this.pathCache[path];\n }\n\n const pathname = new URL(path, 'https://example.com').pathname;\n for (const [name, application] of Object.entries(this.applications)) {\n if (application.routing) {\n for (const group of application.routing) {\n for (const childPath of group.paths) {\n const regexp = pathToRegexp(childPath);\n if (regexp.test(pathname)) {\n this.pathCache[path] = name;\n return name;\n }\n }\n }\n }\n }\n const defaultApplication = Object.entries(this.applications).find(\n ([, application]) => application.default,\n );\n if (!defaultApplication) {\n return null;\n }\n\n this.pathCache[path] = defaultApplication[0];\n return defaultApplication[0];\n }\n\n serialize(): ClientConfig {\n return this.serialized;\n }\n}\n","import { MicrofrontendConfigClient } from '../microfrontends-config/client';\nimport type { WellKnownClientData } from './types';\n\n/**\n * Returns data used by the client to ensure that navigations across\n * microfrontend boundaries are routed and prefetched correctly. The client\n * configuration is safe to expose to users.\n *\n * This data should be exposed in a `.well-known/vercel/micro-frontends/client-config`\n * endpoint.\n */\nexport async function getWellKnownClientData(\n clientConfig: string | undefined,\n flagValues: Record<string, () => Promise<boolean>> = {},\n): Promise<WellKnownClientData> {\n const config = MicrofrontendConfigClient.fromEnv(clientConfig);\n for (const [applicationName, application] of Object.entries(\n config.applications,\n )) {\n if (!application.routing) {\n continue;\n }\n const newRoutingMatches = [];\n for (const pathGroup of application.routing) {\n if (pathGroup.flag) {\n const flagName = pathGroup.flag;\n const flagFn = flagValues[flagName];\n if (!flagFn) {\n throw new Error(\n `Flag \"${flagName}\" was specified to control routing for path group \"${pathGroup.group}\" in application ${applicationName} but not found in provided flag values.`,\n );\n }\n // eslint-disable-next-line no-await-in-loop\n const flagEnabled = await flagFn();\n if (flagEnabled) {\n newRoutingMatches.push(pathGroup);\n }\n } else {\n newRoutingMatches.push(pathGroup);\n }\n }\n application.routing = newRoutingMatches;\n }\n\n return {\n config: config.serialize(),\n };\n}\n"],"mappings":";AAAA,SAAS,oBAAoB;;;ACA7B,SAAS,oBAAoB;AAOtB,IAAM,4BAAN,MAAgC;AAAA,EAKrC,YAAY,QAAsB,MAAyC;AAH3E,qBAAoC,CAAC;AAInC,SAAK,aAAa;AAClB,QAAI,6BAAM,oBAAoB;AAC5B,iBAAW,OAAO,OAAO,OAAO,OAAO,YAAY,GAAG;AACpD,YAAI,IAAI,SAAS;AACf,cAAI,UAAU,IAAI,QAAQ,OAAO,CAAC,UAAU,CAAC,MAAM,IAAI;AAAA,QACzD;AAAA,MACF;AAAA,IACF;AACA,SAAK,eAAe,OAAO;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,QACL,QACA,MAC2B;AAC3B,QAAI,CAAC,QAAQ;AACX,YAAM,IAAI,MAAM,uCAAuC;AAAA,IACzD;AACA,WAAO,IAAI;AAAA,MACT,KAAK,MAAM,MAAM;AAAA,MACjB;AAAA,IACF;AAAA,EACF;AAAA,EAEA,QAAQ,OAA2C;AACjD,WACE,KAAK,UAAU,KAAK,YAAY,MAAM,KAAK,UAAU,MAAM,YAAY;AAAA,EAE3E;AAAA,EAEA,0BAA0B,MAA6B;AACrD,QAAI,CAAC,KAAK,WAAW,GAAG,GAAG;AACzB,YAAM,IAAI,MAAM,0BAA0B;AAAA,IAC5C;AAEA,QAAI,KAAK,UAAU,IAAI,GAAG;AACxB,aAAO,KAAK,UAAU,IAAI;AAAA,IAC5B;AAEA,UAAM,WAAW,IAAI,IAAI,MAAM,qBAAqB,EAAE;AACtD,eAAW,CAAC,MAAM,WAAW,KAAK,OAAO,QAAQ,KAAK,YAAY,GAAG;AACnE,UAAI,YAAY,SAAS;AACvB,mBAAW,SAAS,YAAY,SAAS;AACvC,qBAAW,aAAa,MAAM,OAAO;AACnC,kBAAM,SAAS,aAAa,SAAS;AACrC,gBAAI,OA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":[]}
1
+ {"version":3,"sources":["../../../src/next-v2/endpoints/index.ts","../../../src/config-v2/microfrontends-config/client/index.ts","../../../src/config-v2/well-known/endpoints.ts"],"sourcesContent":["import { NextResponse } from 'next/server';\nimport { getWellKnownClientData } from '../../config-v2/well-known/endpoints';\nimport type { WellKnownClientData } from '../../config-v2/well-known/types';\n\n/**\n * A Next.js App Router API handler to export the micro-frontends client config\n * data.\n *\n * @example In the `app/.well-known/vercel/micro-frontends/client-config/route.ts` file,\n * add this code:\n * ```\n * export { wellKnownNextjsClientConfigAppRoute as GET } from '@vercel/microfrontends/next/endpoints';\n * ```\n */\nexport async function wellKnownNextjsClientConfigAppRoute(\n flagValues: Record<string, () => Promise<boolean>>,\n): Promise<NextResponse> {\n return NextResponse.json<WellKnownClientData>(\n await getWellKnownClientData(\n process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG,\n flagValues,\n ),\n );\n}\n","import { pathToRegexp } from 'path-to-regexp';\nimport type { ClientConfig } from './types';\n\ninterface MicrofrontendConfigClientOptions {\n removeFlaggedPaths?: boolean;\n}\n\nexport class MicrofrontendConfigClient {\n applications: ClientConfig['applications'];\n pathCache: Record<string, string> = {};\n private readonly serialized: ClientConfig;\n\n constructor(config: ClientConfig, opts?: MicrofrontendConfigClientOptions) {\n this.serialized = config;\n if (opts?.removeFlaggedPaths) {\n for (const app of Object.values(config.applications)) {\n if (app.routing) {\n app.routing = app.routing.filter((match) => !match.flag);\n }\n }\n }\n this.applications = config.applications;\n }\n\n /**\n * Create a new `MicrofrontendConfigClient` from a JSON string.\n * Config must be passed in to remain framework agnostic\n */\n static fromEnv(\n config: string | undefined,\n opts?: MicrofrontendConfigClientOptions,\n ): MicrofrontendConfigClient {\n if (!config) {\n throw new Error('No microfrontends configuration found');\n }\n return new MicrofrontendConfigClient(\n JSON.parse(config) as ClientConfig,\n opts,\n );\n }\n\n isEqual(other: MicrofrontendConfigClient): boolean {\n return (\n JSON.stringify(this.applications) === JSON.stringify(other.applications)\n );\n }\n\n getApplicationNameForPath(path: string): string | null {\n if (!path.startsWith('/')) {\n throw new Error(`Path must start with a /`);\n }\n\n if (this.pathCache[path]) {\n return this.pathCache[path];\n }\n\n const pathname = new URL(path, 'https://example.com').pathname;\n for (const [name, application] of Object.entries(this.applications)) {\n if (application.routing) {\n for (const group of application.routing) {\n for (const childPath of group.paths) {\n const regexp = pathToRegexp(childPath);\n if (regexp.test(pathname)) {\n this.pathCache[path] = name;\n return name;\n }\n }\n }\n }\n }\n const defaultApplication = Object.entries(this.applications).find(\n ([, application]) => application.default,\n );\n if (!defaultApplication) {\n return null;\n }\n\n this.pathCache[path] = defaultApplication[0];\n return defaultApplication[0];\n }\n\n serialize(): ClientConfig {\n return this.serialized;\n }\n}\n","import { MicrofrontendConfigClient } from '../microfrontends-config/client';\nimport type { WellKnownClientData } from './types';\n\n/**\n * Returns data used by the client to ensure that navigations across\n * microfrontend boundaries are routed and prefetched correctly. The client\n * configuration is safe to expose to users.\n *\n * This data should be exposed in a `.well-known/vercel/micro-frontends/client-config`\n * endpoint.\n */\nexport async function getWellKnownClientData(\n clientConfig: string | undefined,\n flagValues: Record<string, () => Promise<boolean>> = {},\n): Promise<WellKnownClientData> {\n const config = MicrofrontendConfigClient.fromEnv(clientConfig);\n for (const [applicationName, application] of Object.entries(\n config.applications,\n )) {\n if (!application.routing) {\n continue;\n }\n const newRoutingMatches = [];\n for (const pathGroup of application.routing) {\n if (pathGroup.flag) {\n const flagName = pathGroup.flag;\n const flagFn = flagValues[flagName];\n if (!flagFn) {\n throw new Error(\n `Flag \"${flagName}\" was specified to control routing for path group \"${pathGroup.group}\" in application ${applicationName} but not found in provided flag values.`,\n );\n }\n // eslint-disable-next-line no-await-in-loop\n const flagEnabled = await flagFn();\n if (flagEnabled) {\n newRoutingMatches.push(pathGroup);\n }\n } else {\n newRoutingMatches.push(pathGroup);\n }\n }\n application.routing = newRoutingMatches;\n }\n\n return {\n config: config.serialize(),\n };\n}\n"],"mappings":";AAAA,SAAS,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ides/constants.ts
216
- var OVERRIDES_COOKIE_PREFIX = "vercel-microfrontends-override";
216
+ var OVERRIDES_COOKIE_PREFIX = "vercel-micro-frontends-override";
217
217
  var OVERRIDES_ENV_COOKIE_PREFIX = `${OVERRIDES_COOKIE_PREFIX}:env:`;
218
218
 
219
219
  // src/config-v2/overrides/is-override-cookie.ts
@@ -807,24 +807,26 @@ function getDomainFromEnvironment({
807
807
  app,
808
808
  target
809
809
  }) {
810
- const projects = JSON.parse(
811
- process.env.VERCEL_RELATED_PROJECTS ?? "[]"
810
+ var _a, _b;
811
+ const mfeProjects = JSON.parse(
812
+ process.env.VERCEL_MICROFRONTENDS_PROJECTS ?? "{}"
812
813
  );
813
- if (projects.length === 0) {
814
- throw new Error("Missing related vercel project information");
814
+ if (Object.keys(mfeProjects).length === 0) {
815
+ throw new Error("Missing related microfrontends project information");
815
816
  }
816
- const vercelProject = projects.find(
817
- (project) => {
818
- var _a;
819
- return project.projectId === ((_a = app.vercel) == null ? void 0 : _a.projectId) || project.appName === app.name;
820
- }
821
- );
817
+ if (!((_a = app.vercel) == null ? void 0 : _a.projectId)) {
818
+ throw new Error(`Missing applications[${app.name}].vercel.projectId`);
819
+ }
820
+ const vercelProject = (_b = mfeProjects.applications) == null ? void 0 : _b[app.vercel.projectId];
822
821
  if (!vercelProject) {
823
822
  throw new Error(
824
- `Missing related vercel project information for application "${app.name}"`
823
+ `Missing related microfrontends project information for application "${app.name}"`
825
824
  );
826
825
  }
827
- return vercelProject.aliases[target];
826
+ if (target === "preview" && vercelProject.deploymentAlias) {
827
+ return vercelProject.deploymentAlias;
828
+ }
829
+ return vercelProject.productionHost;
828
830
  }
829
831
 
830
832
  // src/routing-v2/get-domain-for-current-environment.ts
@@ -863,12 +865,12 @@ function getDomainForCurrentEnvironment(config, appName) {
863
865
  const productionHost = config.getDefaultApplication().production.toString();
864
866
  switch (group) {
865
867
  case "development": {
866
- const domain = process.env.NODE_ENV === "test" ? app.development.local.toString() : productionHost;
868
+ const domain = ["test", "development"].includes(process.env.NODE_ENV) ? app.development.local.toString() : productionHost;
867
869
  debugDomains(appName, "development", domain);
868
870
  return domain;
869
871
  }
870
872
  case "preview": {
871
- return getDomainFromEnvironment({ app, target: "branch" });
873
+ return getDomainFromEnvironment({ app, target: "preview" });
872
874
  }
873
875
  case "production": {
874
876
  return getDomainFromEnvironment({ app, target: "production" });
@@ -939,85 +941,103 @@ function getHandler({
939
941
  }) {
940
942
  return async (req) => {
941
943
  var _a, _b;
942
- const pathname = req.nextUrl.pathname;
943
- const search = req.nextUrl.search;
944
- let rewriteDomain = getDomainForCurrentEnvironment(
945
- config,
946
- application.name
947
- );
948
- const patchedHeaders = new Headers(req.headers);
949
- const responseCallbacks = [];
950
- const onRewrite = (response) => {
951
- for (const callback of responseCallbacks) {
952
- callback(response);
953
- }
954
- return response;
955
- };
956
- if (process.env.VERCEL_ENV === "preview" && rewriteDomain) {
957
- const zoneFallbackCookieName = `__zone_${application.name}_production_fallback`;
958
- const assetPrefix = application.getAssetPrefix();
959
- if (assetPrefix && pathname.startsWith(`/${assetPrefix}`) && ((_a = req.cookies.get(zoneFallbackCookieName)) == null ? void 0 : _a.value) === "1") {
960
- rewriteDomain = production.toString();
961
- } else {
962
- try {
963
- let deploymentFound;
964
- if (req.headers.get("x-vercel-skip-deployment-existence-check")) {
965
- deploymentFound = true;
966
- } else {
967
- deploymentFound = await verifyPreviewDomain(req, rewriteDomain);
968
- }
969
- if (!deploymentFound) {
970
- rewriteDomain = production.toString();
971
- responseCallbacks.push((response) => {
972
- response.cookies.set(zoneFallbackCookieName, "1", {
973
- httpOnly: true,
974
- sameSite: "lax",
975
- secure: true,
976
- maxAge: 60
977
- // 1 minute
944
+ try {
945
+ const pathname = req.nextUrl.pathname;
946
+ const search = req.nextUrl.search;
947
+ const proxyRouting = req.headers.get("X-Vercel-Mfe-Proxy") === "1";
948
+ let rewriteDomain = proxyRouting ? null : getDomainForCurrentEnvironment(config, application.name);
949
+ const patchedHeaders = new Headers(req.headers);
950
+ const responseCallbacks = [];
951
+ const onRewrite = (response) => {
952
+ for (const callback of responseCallbacks) {
953
+ callback(response);
954
+ }
955
+ return response;
956
+ };
957
+ if (process.env.VERCEL_ENV === "preview" && rewriteDomain) {
958
+ const zoneFallbackCookieName = `__zone_${application.name}_production_fallback`;
959
+ const assetPrefix = application.getAssetPrefix();
960
+ if (assetPrefix && pathname.startsWith(`/${assetPrefix}`) && ((_a = req.cookies.get(zoneFallbackCookieName)) == null ? void 0 : _a.value) === "1") {
961
+ rewriteDomain = production.toString();
962
+ } else {
963
+ try {
964
+ let deploymentFound;
965
+ if (req.headers.get("x-vercel-skip-deployment-existence-check")) {
966
+ deploymentFound = true;
967
+ } else {
968
+ deploymentFound = await verifyPreviewDomain(req, rewriteDomain);
969
+ }
970
+ if (!deploymentFound) {
971
+ rewriteDomain = production.toString();
972
+ responseCallbacks.push((response) => {
973
+ response.cookies.set(zoneFallbackCookieName, "1", {
974
+ httpOnly: true,
975
+ sameSite: "lax",
976
+ secure: true,
977
+ maxAge: 60
978
+ // 1 minute
979
+ });
978
980
  });
979
- });
980
- try {
981
- const existingCookie = patchedHeaders.get("cookie");
982
- if (!(existingCookie == null ? void 0 : existingCookie.includes("__vercel_toolbar"))) {
983
- patchedHeaders.set(
984
- "cookie",
985
- [`__vercel_toolbar=1`, existingCookie].join("; ")
986
- );
987
- responseCallbacks.push((response) => {
988
- response.cookies.set("__vercel_toolbar", "1", {
989
- httpOnly: false,
990
- sameSite: "lax",
991
- secure: true,
992
- maxAge: 29030400
981
+ try {
982
+ const existingCookie = patchedHeaders.get("cookie");
983
+ if (!(existingCookie == null ? void 0 : existingCookie.includes("__vercel_toolbar"))) {
984
+ patchedHeaders.set(
985
+ "cookie",
986
+ [`__vercel_toolbar=1`, existingCookie].join("; ")
987
+ );
988
+ responseCallbacks.push((response) => {
989
+ response.cookies.set("__vercel_toolbar", "1", {
990
+ httpOnly: false,
991
+ sameSite: "lax",
992
+ secure: true,
993
+ maxAge: 29030400
994
+ });
993
995
  });
994
- });
996
+ }
997
+ } catch (error) {
995
998
  }
996
- } catch (error) {
997
- }
998
- } else if (((_b = req.cookies.get(zoneFallbackCookieName)) == null ? void 0 : _b.value) === "1") {
999
- responseCallbacks.push((response) => {
1000
- response.cookies.set(zoneFallbackCookieName, "", {
1001
- httpOnly: true,
1002
- sameSite: "lax",
1003
- secure: true,
1004
- maxAge: 0
999
+ } else if (((_b = req.cookies.get(zoneFallbackCookieName)) == null ? void 0 : _b.value) === "1") {
1000
+ responseCallbacks.push((response) => {
1001
+ response.cookies.set(zoneFallbackCookieName, "", {
1002
+ httpOnly: true,
1003
+ sameSite: "lax",
1004
+ secure: true,
1005
+ maxAge: 0
1006
+ });
1005
1007
  });
1006
- });
1008
+ }
1009
+ } catch {
1007
1010
  }
1008
- } catch {
1009
1011
  }
1010
1012
  }
1011
- }
1012
- const isFlagEnabled = async () => flagFn ? flagFn() : true;
1013
- if (pattern.test(pathname) && await isFlagEnabled()) {
1014
- return onRewrite(
1015
- import_server.NextResponse.rewrite(`${rewriteDomain}${pathname}${search}`, {
1016
- request: {
1017
- headers: patchedHeaders
1018
- }
1019
- })
1020
- );
1013
+ const isFlagEnabled = async () => flagFn ? flagFn() : true;
1014
+ if (pattern.test(pathname) && await isFlagEnabled()) {
1015
+ if (proxyRouting) {
1016
+ const headers = new Headers(req.headers);
1017
+ headers.set("x-vercel-mfe-zone", application.name);
1018
+ return onRewrite(
1019
+ import_server.NextResponse.next({
1020
+ request: {
1021
+ headers
1022
+ },
1023
+ headers: {
1024
+ // temporary, can delete when proxyRouting flag is removed
1025
+ "x-vercel-mfe-middleware-used-proxy": "1"
1026
+ }
1027
+ })
1028
+ );
1029
+ }
1030
+ return onRewrite(
1031
+ import_server.NextResponse.rewrite(`${rewriteDomain}${pathname}${search}`, {
1032
+ request: {
1033
+ headers: patchedHeaders
1034
+ }
1035
+ })
1036
+ );
1037
+ }
1038
+ } catch (e) {
1039
+ console.error("An error occured in the microfrontends middleware:", e);
1040
+ throw e;
1021
1041
  }
1022
1042
  };
1023
1043
  }