remote-components 0.3.0 → 0.3.2

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 (105) hide show
  1. package/dist/config/nextjs.cjs.map +1 -1
  2. package/dist/config/nextjs.js.map +1 -1
  3. package/dist/host/html.cjs +467 -395
  4. package/dist/host/html.cjs.map +1 -1
  5. package/dist/host/html.js +467 -395
  6. package/dist/host/html.js.map +1 -1
  7. package/dist/host/nextjs/app/client-only.cjs +507 -447
  8. package/dist/host/nextjs/app/client-only.cjs.map +1 -1
  9. package/dist/host/nextjs/app/client-only.js +515 -455
  10. package/dist/host/nextjs/app/client-only.js.map +1 -1
  11. package/dist/host/nextjs/app.cjs +5 -6
  12. package/dist/host/nextjs/app.cjs.map +1 -1
  13. package/dist/host/nextjs/app.js +5 -6
  14. package/dist/host/nextjs/app.js.map +1 -1
  15. package/dist/host/nextjs/pages.cjs +6 -11
  16. package/dist/host/nextjs/pages.cjs.map +1 -1
  17. package/dist/host/nextjs/pages.js +9 -11
  18. package/dist/host/nextjs/pages.js.map +1 -1
  19. package/dist/host/react.cjs +435 -374
  20. package/dist/host/react.cjs.map +1 -1
  21. package/dist/host/react.js +435 -374
  22. package/dist/host/react.js.map +1 -1
  23. package/dist/internal/host/nextjs/dom-flight.cjs +16 -7
  24. package/dist/internal/host/nextjs/dom-flight.cjs.map +1 -1
  25. package/dist/internal/host/nextjs/dom-flight.d.ts +2 -2
  26. package/dist/internal/host/nextjs/dom-flight.js +16 -7
  27. package/dist/internal/host/nextjs/dom-flight.js.map +1 -1
  28. package/dist/internal/host/nextjs/image-shared.cjs +2 -2
  29. package/dist/internal/host/nextjs/image-shared.cjs.map +1 -1
  30. package/dist/internal/host/nextjs/image-shared.js +2 -2
  31. package/dist/internal/host/nextjs/image-shared.js.map +1 -1
  32. package/dist/internal/host/server/fetch-remote-component.cjs +183 -149
  33. package/dist/internal/host/server/fetch-remote-component.cjs.map +1 -1
  34. package/dist/internal/host/server/fetch-remote-component.js +185 -149
  35. package/dist/internal/host/server/fetch-remote-component.js.map +1 -1
  36. package/dist/internal/host/shared/remote-image-loader.cjs +2 -5
  37. package/dist/internal/host/shared/remote-image-loader.cjs.map +1 -1
  38. package/dist/internal/host/shared/remote-image-loader.js +2 -5
  39. package/dist/internal/host/shared/remote-image-loader.js.map +1 -1
  40. package/dist/internal/runtime/constants.cjs +6 -6
  41. package/dist/internal/runtime/constants.cjs.map +1 -1
  42. package/dist/internal/runtime/constants.d.ts +3 -3
  43. package/dist/internal/runtime/constants.js +4 -4
  44. package/dist/internal/runtime/constants.js.map +1 -1
  45. package/dist/internal/runtime/html/parse-remote-html.cjs +11 -15
  46. package/dist/internal/runtime/html/parse-remote-html.cjs.map +1 -1
  47. package/dist/internal/runtime/html/parse-remote-html.d.ts +2 -12
  48. package/dist/internal/runtime/html/parse-remote-html.js +17 -15
  49. package/dist/internal/runtime/html/parse-remote-html.js.map +1 -1
  50. package/dist/internal/runtime/loaders/script-loader.cjs +2 -2
  51. package/dist/internal/runtime/loaders/script-loader.cjs.map +1 -1
  52. package/dist/internal/runtime/loaders/script-loader.js +1 -1
  53. package/dist/internal/runtime/loaders/script-loader.js.map +1 -1
  54. package/dist/internal/runtime/metadata.cjs +42 -0
  55. package/dist/internal/runtime/metadata.cjs.map +1 -1
  56. package/dist/internal/runtime/metadata.d.ts +21 -1
  57. package/dist/internal/runtime/metadata.js +38 -0
  58. package/dist/internal/runtime/metadata.js.map +1 -1
  59. package/dist/internal/runtime/patterns.cjs +38 -0
  60. package/dist/internal/runtime/patterns.cjs.map +1 -0
  61. package/dist/internal/runtime/patterns.d.ts +5 -0
  62. package/dist/internal/runtime/patterns.js +12 -0
  63. package/dist/internal/runtime/patterns.js.map +1 -0
  64. package/dist/internal/runtime/turbopack/chunk-loader.cjs +79 -128
  65. package/dist/internal/runtime/turbopack/chunk-loader.cjs.map +1 -1
  66. package/dist/internal/runtime/turbopack/chunk-loader.d.ts +22 -7
  67. package/dist/internal/runtime/turbopack/chunk-loader.js +85 -131
  68. package/dist/internal/runtime/turbopack/chunk-loader.js.map +1 -1
  69. package/dist/internal/runtime/turbopack/module.cjs +56 -57
  70. package/dist/internal/runtime/turbopack/module.cjs.map +1 -1
  71. package/dist/internal/runtime/turbopack/module.d.ts +14 -11
  72. package/dist/internal/runtime/turbopack/module.js +54 -56
  73. package/dist/internal/runtime/turbopack/module.js.map +1 -1
  74. package/dist/internal/runtime/turbopack/remote-scope.cjs +101 -0
  75. package/dist/internal/runtime/turbopack/remote-scope.cjs.map +1 -0
  76. package/dist/internal/runtime/turbopack/remote-scope.d.ts +55 -0
  77. package/dist/internal/runtime/turbopack/remote-scope.js +73 -0
  78. package/dist/internal/runtime/turbopack/remote-scope.js.map +1 -0
  79. package/dist/internal/runtime/turbopack/shared-modules.cjs +62 -56
  80. package/dist/internal/runtime/turbopack/shared-modules.cjs.map +1 -1
  81. package/dist/internal/runtime/turbopack/shared-modules.d.ts +7 -3
  82. package/dist/internal/runtime/turbopack/shared-modules.js +63 -57
  83. package/dist/internal/runtime/turbopack/shared-modules.js.map +1 -1
  84. package/dist/internal/runtime/turbopack/webpack-runtime.cjs +50 -31
  85. package/dist/internal/runtime/turbopack/webpack-runtime.cjs.map +1 -1
  86. package/dist/internal/runtime/turbopack/webpack-runtime.d.ts +5 -2
  87. package/dist/internal/runtime/turbopack/webpack-runtime.js +52 -34
  88. package/dist/internal/runtime/turbopack/webpack-runtime.js.map +1 -1
  89. package/dist/internal/runtime/types.cjs.map +1 -1
  90. package/dist/internal/runtime/types.d.ts +4 -3
  91. package/dist/internal/utils/logger.cjs.map +1 -1
  92. package/dist/internal/utils/logger.d.ts +1 -1
  93. package/dist/internal/utils/logger.js.map +1 -1
  94. package/dist/internal/utils.cjs +5 -0
  95. package/dist/internal/utils.cjs.map +1 -1
  96. package/dist/internal/utils.d.ts +15 -1
  97. package/dist/internal/utils.js +4 -0
  98. package/dist/internal/utils.js.map +1 -1
  99. package/dist/remote/html.cjs.map +1 -1
  100. package/dist/remote/html.js.map +1 -1
  101. package/dist/remote/nextjs/app.cjs +2 -1
  102. package/dist/remote/nextjs/app.cjs.map +1 -1
  103. package/dist/remote/nextjs/app.js +2 -1
  104. package/dist/remote/nextjs/app.js.map +1 -1
  105. package/package.json +1 -1
@@ -67,90 +67,40 @@ __export(app_client_only_exports, {
67
67
  module.exports = __toCommonJS(app_client_only_exports);
68
68
  var import_react5 = require("react");
69
69
 
70
- // src/host/shared/remote-image-loader.ts
71
- function getRemoteBundleOrigin(bundle) {
72
- const self = globalThis;
73
- return self.__remote_bundle_url__?.[bundle]?.origin ?? "";
74
- }
75
- function createRemoteImageLoader(bundle, resolveClientUrl) {
76
- const loader = Object.assign(
77
- ({
78
- config,
79
- src,
80
- width,
81
- quality
82
- }) => {
83
- const q = quality ?? 75;
84
- const remoteOrigin = getRemoteBundleOrigin(bundle);
85
- const isCrossOrigin = remoteOrigin && remoteOrigin !== location.origin;
86
- const basePath = isCrossOrigin ? `${remoteOrigin}${config.path ?? "/_next/image"}` : config.path ?? `${remoteOrigin}/_next/image`;
87
- const url = `${basePath}?url=${encodeURIComponent(src)}&w=${width}&q=${q}`;
88
- return resolveClientUrl?.(url) ?? url;
89
- },
90
- // Signals to getImgProps that this is a default loader (not a user-defined
91
- // one), enabling srcSet generation with device/image sizes from the config.
92
- { __next_img_default: true }
93
- );
94
- return loader;
95
- }
96
-
97
- // src/runtime/url/resolve-client-url.ts
98
- function withRemoteSrc(resolveClientUrl, remoteSrc) {
99
- const remoteOrigin = parseOrigin(remoteSrc);
100
- return (url) => {
101
- const urlOrigin = parseOrigin(url);
102
- if (remoteOrigin && urlOrigin && urlOrigin !== remoteOrigin) {
103
- return void 0;
104
- }
105
- return resolveClientUrl(remoteSrc, url);
106
- };
70
+ // src/utils/index.ts
71
+ function escapeString(str) {
72
+ return str.replace(/[^a-z0-9]/g, "_");
107
73
  }
108
- function parseOrigin(url) {
109
- try {
110
- return new URL(url).origin;
111
- } catch {
112
- return void 0;
113
- }
74
+ function computeScopedName(name, options) {
75
+ return options.isCrossOrigin ? `${name}_${escapeString(options.remoteHost.toLowerCase())}` : name;
114
76
  }
77
+ var attrToProp = {
78
+ fetchpriority: "fetchPriority",
79
+ crossorigin: "crossOrigin",
80
+ imagesrcset: "imageSrcSet",
81
+ imagesizes: "imageSizes",
82
+ srcset: "srcSet"
83
+ };
115
84
 
116
- // src/runtime/url/default-resolve-client-url.ts
117
- function bindResolveClientUrl(prop, remoteSrc) {
118
- return prop ? withRemoteSrc(prop, remoteSrc) : void 0;
85
+ // src/runtime/constants.ts
86
+ var DEFAULT_BUNDLE_NAME = "__vercel_remote_bundle";
87
+ var DEFAULT_COMPONENT_NAME = "__vercel_remote_component";
88
+ var DEFAULT_ROUTE = "/";
89
+ var RUNTIME_WEBPACK = "webpack";
90
+ var RUNTIME_TURBOPACK = "turbopack";
91
+ var RUNTIME_SCRIPT = "script";
92
+ function getBundleKey(bundle) {
93
+ return escapeString(bundle);
119
94
  }
120
95
 
121
- // src/host/nextjs/image-shared.ts
122
- function resolveForBundle(unbound, bundle) {
123
- if (!unbound)
124
- return void 0;
125
- const self = globalThis;
126
- const remoteSrc = self.__remote_bundle_url__?.[bundle]?.href ?? "";
127
- return bindResolveClientUrl(unbound, remoteSrc);
128
- }
129
- function createImageLoaderSharedEntries({
130
- bound,
131
- unbound
132
- } = {}) {
133
- const entry = (bundle) => {
134
- const resolveClientUrl = bound ?? resolveForBundle(unbound, bundle);
135
- return Promise.resolve({
136
- default: createRemoteImageLoader(bundle, resolveClientUrl),
137
- __esModule: true
138
- });
139
- };
140
- return {
141
- "next/dist/shared/lib/image-loader": entry,
142
- "next/dist/esm/shared/lib/image-loader": entry
143
- };
96
+ // src/runtime/patterns.ts
97
+ var REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
98
+ var NEXT_BUNDLE_PATH_RE = /\/_next\/\[.+\](?:%20| )/;
99
+ var DOUBLE_SLASH_RE = /(?<!:)\/\//g;
100
+ function collapseDoubleSlashes(path) {
101
+ return path.replace(DOUBLE_SLASH_RE, "/");
144
102
  }
145
103
 
146
- // src/host/nextjs/app-client-only.tsx
147
- var import_context3 = require("#internal/host/react/context");
148
-
149
- // src/host/react/index.tsx
150
- var import_react3 = require("react");
151
- var import_react_dom = require("react-dom");
152
- var import_context2 = require("#internal/host/react/context");
153
-
154
104
  // src/utils/logger.ts
155
105
  init_constants();
156
106
 
@@ -297,6 +247,148 @@ function warnCrossOriginFetchError(logLocation, url) {
297
247
  }
298
248
  }
299
249
 
250
+ // src/runtime/turbopack/remote-scope.ts
251
+ function getRegistry() {
252
+ const self = globalThis;
253
+ if (!self.__remote_component_scopes__) {
254
+ self.__remote_component_scopes__ = /* @__PURE__ */ new Map();
255
+ }
256
+ return self.__remote_component_scopes__;
257
+ }
258
+ function createScope(name, url, runtime, resolveClientUrl) {
259
+ const isCrossOrigin = url.origin !== location.origin;
260
+ const scopedName = computeScopedName(name, {
261
+ remoteHost: url.host,
262
+ isCrossOrigin
263
+ });
264
+ const globalKey = getBundleKey(scopedName);
265
+ return {
266
+ name,
267
+ scopedName,
268
+ globalKey,
269
+ url,
270
+ runtime,
271
+ resolveClientUrl,
272
+ moduleCache: {},
273
+ sharedModules: {},
274
+ moduleGlobal: {}
275
+ };
276
+ }
277
+ function registerScope(scope) {
278
+ const registry = getRegistry();
279
+ registry.set(scope.scopedName, scope);
280
+ if (scope.scopedName !== scope.name) {
281
+ const existing = registry.get(scope.name);
282
+ if (existing && existing.scopedName !== scope.scopedName) {
283
+ logWarn(
284
+ "RemoteScope",
285
+ `Plain name "${scope.name}" already registered by scope "${existing.scopedName}" \u2014 overwriting with "${scope.scopedName}". Static hosts will only resolve the latest one.`
286
+ );
287
+ }
288
+ registry.set(scope.name, scope);
289
+ }
290
+ logDebug(
291
+ "RemoteScope",
292
+ `Registered scope "${scope.scopedName}" (${registry.size} total)`
293
+ );
294
+ }
295
+ function getScope(name) {
296
+ return getRegistry().get(name);
297
+ }
298
+ function formatRemoteId(scope, path) {
299
+ return `[${scope.scopedName}] ${path}`;
300
+ }
301
+ function parseRemoteId(id) {
302
+ const groups = REMOTE_COMPONENT_REGEX.exec(id)?.groups;
303
+ if (groups?.bundle && groups.id) {
304
+ return {
305
+ bundle: groups.bundle,
306
+ path: groups.id,
307
+ prefix: groups.prefix ?? ""
308
+ };
309
+ }
310
+ return { bundle: "default", path: id, prefix: "" };
311
+ }
312
+
313
+ // src/host/shared/remote-image-loader.ts
314
+ function createRemoteImageLoader(bundle, resolveClientUrl) {
315
+ const loader = Object.assign(
316
+ ({
317
+ config,
318
+ src,
319
+ width,
320
+ quality
321
+ }) => {
322
+ const q = quality ?? 75;
323
+ const remoteOrigin = getScope(bundle)?.url.origin ?? "";
324
+ const isCrossOrigin = remoteOrigin && remoteOrigin !== location.origin;
325
+ const basePath = isCrossOrigin ? `${remoteOrigin}${config.path ?? "/_next/image"}` : config.path ?? `${remoteOrigin}/_next/image`;
326
+ const url = `${basePath}?url=${encodeURIComponent(src)}&w=${width}&q=${q}`;
327
+ return resolveClientUrl?.(url) ?? url;
328
+ },
329
+ // Signals to getImgProps that this is a default loader (not a user-defined
330
+ // one), enabling srcSet generation with device/image sizes from the config.
331
+ { __next_img_default: true }
332
+ );
333
+ return loader;
334
+ }
335
+
336
+ // src/runtime/url/resolve-client-url.ts
337
+ function withRemoteSrc(resolveClientUrl, remoteSrc) {
338
+ const remoteOrigin = parseOrigin(remoteSrc);
339
+ return (url) => {
340
+ const urlOrigin = parseOrigin(url);
341
+ if (remoteOrigin && urlOrigin && urlOrigin !== remoteOrigin) {
342
+ return void 0;
343
+ }
344
+ return resolveClientUrl(remoteSrc, url);
345
+ };
346
+ }
347
+ function parseOrigin(url) {
348
+ try {
349
+ return new URL(url).origin;
350
+ } catch {
351
+ return void 0;
352
+ }
353
+ }
354
+
355
+ // src/runtime/url/default-resolve-client-url.ts
356
+ function bindResolveClientUrl(prop, remoteSrc) {
357
+ return prop ? withRemoteSrc(prop, remoteSrc) : void 0;
358
+ }
359
+
360
+ // src/host/nextjs/image-shared.ts
361
+ function resolveForBundle(unbound, bundle) {
362
+ if (!unbound)
363
+ return void 0;
364
+ const remoteSrc = getScope(bundle)?.url.href ?? "";
365
+ return bindResolveClientUrl(unbound, remoteSrc);
366
+ }
367
+ function createImageLoaderSharedEntries({
368
+ bound,
369
+ unbound
370
+ } = {}) {
371
+ const entry = (bundle) => {
372
+ const resolveClientUrl = bound ?? resolveForBundle(unbound, bundle);
373
+ return Promise.resolve({
374
+ default: createRemoteImageLoader(bundle, resolveClientUrl),
375
+ __esModule: true
376
+ });
377
+ };
378
+ return {
379
+ "next/dist/shared/lib/image-loader": entry,
380
+ "next/dist/esm/shared/lib/image-loader": entry
381
+ };
382
+ }
383
+
384
+ // src/host/nextjs/app-client-only.tsx
385
+ var import_context3 = require("#internal/host/react/context");
386
+
387
+ // src/host/react/index.tsx
388
+ var import_react3 = require("react");
389
+ var import_react_dom = require("react-dom");
390
+ var import_context2 = require("#internal/host/react/context");
391
+
300
392
  // src/host/server/fetch-headers.ts
301
393
  function remoteFetchHeaders() {
302
394
  return {
@@ -521,29 +613,6 @@ function resolveNameFromSrc(src, defaultName) {
521
613
  return name || defaultName;
522
614
  }
523
615
 
524
- // src/utils/index.ts
525
- function escapeString(str) {
526
- return str.replace(/[^a-z0-9]/g, "_");
527
- }
528
- var attrToProp = {
529
- fetchpriority: "fetchPriority",
530
- crossorigin: "crossOrigin",
531
- imagesrcset: "imageSrcSet",
532
- imagesizes: "imageSizes",
533
- srcset: "srcSet"
534
- };
535
-
536
- // src/runtime/constants.ts
537
- var DEFAULT_ROUTE = "/";
538
- var RUNTIME_WEBPACK = "webpack";
539
- var RUNTIME_TURBOPACK = "turbopack";
540
- var RUNTIME_SCRIPT = "script";
541
- var REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
542
- var NEXT_BUNDLE_PATH_RE = /\/_next\/\[.+\](?:%20| )/;
543
- function getBundleKey(bundle) {
544
- return escapeString(bundle);
545
- }
546
-
547
616
  // src/runtime/html/apply-origin.ts
548
617
  var tagNames = [
549
618
  "img",
@@ -606,6 +675,38 @@ function applyOriginToNodes(doc, url, resolveClientUrl) {
606
675
  }
607
676
  }
608
677
 
678
+ // src/runtime/metadata.ts
679
+ var VALID_RUNTIMES = /* @__PURE__ */ new Set(["webpack", "turbopack", "script"]);
680
+ var VALID_TYPES = /* @__PURE__ */ new Set([
681
+ "nextjs",
682
+ "remote-component",
683
+ "unknown"
684
+ ]);
685
+ function isRuntime(value) {
686
+ return VALID_RUNTIMES.has(value);
687
+ }
688
+ function isComponentType(value) {
689
+ return VALID_TYPES.has(value);
690
+ }
691
+ function toRuntime(value) {
692
+ return value && isRuntime(value) ? value : "webpack";
693
+ }
694
+ function toComponentType(value) {
695
+ return value && isComponentType(value) ? value : "unknown";
696
+ }
697
+ function buildMetadata(attrs, url) {
698
+ const id = attrs.id || DEFAULT_COMPONENT_NAME;
699
+ const bundle = attrs.bundle || process.env.NEXT_PUBLIC_MFE_CURRENT_APPLICATION || DEFAULT_BUNDLE_NAME;
700
+ return {
701
+ name: attrs.name || id.replace(/_ssr$/, ""),
702
+ bundle,
703
+ route: attrs.route || url.pathname || DEFAULT_ROUTE,
704
+ runtime: toRuntime(attrs.runtime),
705
+ id,
706
+ type: toComponentType(attrs.type)
707
+ };
708
+ }
709
+
609
710
  // src/runtime/html/parse-remote-html.ts
610
711
  function validateSingleComponent(doc, name, url) {
611
712
  if (doc.querySelectorAll("div[data-bundle][data-route]").length > 1 && !doc.querySelector(`div[data-bundle][data-route][id^="${name}"]`) || doc.querySelectorAll("remote-component:not([src])").length > 1 && !doc.querySelector(`remote-component[name="${name}"]`)) {
@@ -625,14 +726,6 @@ function resolveComponentName(component, nextData, fallbackName) {
625
726
  const name = component?.getAttribute("id")?.replace(/_ssr$/, "") || isRemoteComponent && component?.getAttribute("name") || (nextData ? "__next" : fallbackName);
626
727
  return { name, isRemoteComponent };
627
728
  }
628
- function extractComponentMetadata(component, nextData, name, url) {
629
- return {
630
- name,
631
- bundle: component?.getAttribute("data-bundle") || nextData?.props.__REMOTE_COMPONENT__?.bundle || "default",
632
- route: component?.getAttribute("data-route") ?? nextData?.page ?? (url.pathname || DEFAULT_ROUTE),
633
- runtime: component?.getAttribute("data-runtime") ?? (nextData?.props.__REMOTE_COMPONENT__?.runtime || RUNTIME_SCRIPT)
634
- };
635
- }
636
729
  function extractRemoteShared(doc, name, nextData) {
637
730
  const remoteSharedEl = doc.querySelector(
638
731
  `#${name}_shared[data-remote-components-shared]`
@@ -644,7 +737,7 @@ function extractRemoteShared(doc, name, nextData) {
644
737
  function validateComponentFound(component, rsc, nextData, isRemoteComponent, url, name) {
645
738
  if (!component || !(rsc || nextData || isRemoteComponent)) {
646
739
  throw new RemoteComponentsError(
647
- `Remote Component not found on ${url}.${name !== "__vercel_remote_component" ? ` The name for the <RemoteComponent> is "${name}". Check <RemoteComponent> usage.` : ""} Did you forget to wrap the content in <RemoteComponent>?`
740
+ `Remote Component not found on ${url}.${name !== DEFAULT_COMPONENT_NAME ? ` The name for the <RemoteComponent> is "${name}". Check <RemoteComponent> usage.` : ""} Did you forget to wrap the content in <RemoteComponent>?`
648
741
  );
649
742
  }
650
743
  }
@@ -670,10 +763,15 @@ function parseRemoteComponentDocument(doc, name, url) {
670
763
  name
671
764
  );
672
765
  const rsc = doc.querySelector(`#${resolvedName}_rsc`);
673
- const metadata = extractComponentMetadata(
674
- component,
675
- nextData,
676
- resolvedName,
766
+ const metadata = buildMetadata(
767
+ {
768
+ name: resolvedName,
769
+ bundle: component?.getAttribute("data-bundle") || nextData?.props.__REMOTE_COMPONENT__?.bundle,
770
+ route: component?.getAttribute("data-route") ?? nextData?.page,
771
+ runtime: component?.getAttribute("data-runtime") ?? nextData?.props.__REMOTE_COMPONENT__?.runtime ?? RUNTIME_SCRIPT,
772
+ id: component?.getAttribute("id"),
773
+ type: component?.getAttribute("data-type")
774
+ },
677
775
  url
678
776
  );
679
777
  const remoteShared = extractRemoteShared(doc, resolvedName, nextData);
@@ -998,62 +1096,39 @@ var ASYNC_MODULE_ALL_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<vCb
998
1096
  var TURBOPACK_GLOBAL_RE = /(?:globalThis|self)\s*(?:\.TURBOPACK|\[\s*["']TURBOPACK["']\s*\])/;
999
1097
 
1000
1098
  // src/runtime/turbopack/chunk-loader.ts
1001
- function createChunkLoader(runtime, resolveClientUrl) {
1002
- return function __turbopack_chunk_load__(chunkId, scriptBundle) {
1003
- logDebug("ChunkLoader", `Loading chunk: "${chunkId}"`);
1004
- const self = globalThis;
1005
- const {
1006
- bundle,
1007
- id: path,
1008
- prefix
1009
- } = REMOTE_COMPONENT_REGEX.exec(chunkId)?.groups ?? {
1010
- bundle: scriptBundle ?? "",
1011
- id: chunkId
1012
- };
1013
- logDebug(
1014
- "ChunkLoader",
1015
- `Parsed chunk - bundle: "${bundle}", path: "${path}", prefix: "${prefix}"`
1016
- );
1017
- const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : runtime;
1018
- logDebug("ChunkLoader", `Remote runtime: "${remoteRuntime}"`);
1019
- if (remoteRuntime === RUNTIME_WEBPACK) {
1020
- logDebug("ChunkLoader", "Skipping chunk load - webpack runtime detected");
1021
- return Promise.resolve(void 0);
1022
- }
1023
- const url = new URL(
1024
- path ? `${prefix ?? ""}${path}`.replace(
1025
- /(?<char>[^:])(?<double>\/\/)/g,
1026
- "$1/"
1027
- ) : "/",
1028
- self.__remote_bundle_url__?.[bundle ?? "default"] ?? new URL(location.origin)
1029
- ).href;
1030
- if (url.endsWith(".css")) {
1031
- logDebug("ChunkLoader", `Skipping CSS file: "${url}"`);
1032
- return;
1033
- }
1034
- if (!self.__remote_components_turbopack_chunk_loader_promise__) {
1035
- self.__remote_components_turbopack_chunk_loader_promise__ = {};
1036
- }
1037
- if (self.__remote_components_turbopack_chunk_loader_promise__[url]) {
1038
- logDebug("ChunkLoader", `Returning cached promise for: "${url}"`);
1039
- return self.__remote_components_turbopack_chunk_loader_promise__[url];
1040
- }
1041
- const resolvedUrl = resolveClientUrl?.(url) ?? url;
1042
- logDebug("ChunkLoader", `Fetching chunk from: "${resolvedUrl}"`);
1043
- self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise((resolve, reject) => {
1099
+ function loadChunkWithScope(scope, chunkId) {
1100
+ logDebug(
1101
+ "ChunkLoader",
1102
+ `loadChunkWithScope: "${chunkId}" (scope: "${scope.scopedName}")`
1103
+ );
1104
+ const self = globalThis;
1105
+ const { bundle, path, prefix } = parseRemoteId(chunkId);
1106
+ const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : scope.runtime;
1107
+ if (remoteRuntime === RUNTIME_WEBPACK) {
1108
+ return Promise.resolve(void 0);
1109
+ }
1110
+ const rawPath = path ? collapseDoubleSlashes(`${prefix}${path}`) : "/";
1111
+ const url = new URL(rawPath, scope.url).href;
1112
+ if (url.endsWith(".css")) {
1113
+ return;
1114
+ }
1115
+ if (!self.__remote_components_turbopack_chunk_loader_promise__) {
1116
+ self.__remote_components_turbopack_chunk_loader_promise__ = {};
1117
+ }
1118
+ if (self.__remote_components_turbopack_chunk_loader_promise__[url]) {
1119
+ return self.__remote_components_turbopack_chunk_loader_promise__[url];
1120
+ }
1121
+ const resolvedUrl = scope.resolveClientUrl?.(url) ?? url;
1122
+ if (resolvedUrl !== url) {
1123
+ logDebug("ChunkLoader", `Proxied chunk URL: "${url}" \u2192 "${resolvedUrl}"`);
1124
+ }
1125
+ self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise(
1126
+ (resolve, reject) => {
1044
1127
  fetch(resolvedUrl).then((res) => res.text()).then((code) => {
1045
1128
  const hasTurbopack = TURBOPACK_GLOBAL_RE.test(code);
1046
1129
  if (hasTurbopack) {
1047
- return handleTurbopackChunk(code, bundle ?? "", url);
1130
+ return handleTurbopackChunk(code, scope, url);
1048
1131
  }
1049
- logDebug(
1050
- "ChunkLoader",
1051
- `Chunk does not contain globalThis.TURBOPACK or self.TURBOPACK: "${url}"`
1052
- );
1053
- logDebug(
1054
- "ChunkLoader",
1055
- `First 500 chars of chunk: ${code.slice(0, 500)}`
1056
- );
1057
1132
  }).then(resolve).catch((error) => {
1058
1133
  const isProxied = isProxiedUrl(resolvedUrl);
1059
1134
  if (isProxied) {
@@ -1063,17 +1138,29 @@ function createChunkLoader(runtime, resolveClientUrl) {
1063
1138
  reject(error);
1064
1139
  }
1065
1140
  });
1066
- });
1067
- return self.__remote_components_turbopack_chunk_loader_promise__[url];
1141
+ }
1142
+ );
1143
+ return self.__remote_components_turbopack_chunk_loader_promise__[url];
1144
+ }
1145
+ function createChunkDispatcher() {
1146
+ return function __chunk_dispatcher__(chunkId, scriptBundle) {
1147
+ logDebug("ChunkDispatcher", `Dispatching chunk: "${chunkId}"`);
1148
+ const { bundle } = parseRemoteId(chunkId);
1149
+ const bundleName = bundle || scriptBundle || "default";
1150
+ const scope = getScope(bundleName);
1151
+ logDebug(
1152
+ "ChunkDispatcher",
1153
+ `Scope resolution: bundle="${bundleName}", scope=${scope?.scopedName ?? "null"}`
1154
+ );
1155
+ if (!scope) {
1156
+ logWarn("ChunkDispatcher", `No scope found for bundle "${bundleName}"`);
1157
+ return Promise.resolve(void 0);
1158
+ }
1159
+ return loadChunkWithScope(scope, chunkId);
1068
1160
  };
1069
1161
  }
1070
- async function handleTurbopackChunk(code, bundle, url) {
1071
- logDebug(
1072
- "ChunkLoader",
1073
- `Handling Turbopack chunk - bundle: "${bundle}", url: "${url}"`
1074
- );
1162
+ async function handleTurbopackChunk(code, scope, url) {
1075
1163
  if (/importScripts\(\.\.\.self.TURBOPACK_NEXT_CHUNK_URLS/.test(code)) {
1076
- logDebug("ChunkLoader", `Skipping worker chunk: "${url}"`);
1077
1164
  const preloadLinks = document.querySelectorAll(
1078
1165
  `link[rel="preload"][href="${new URL(url).pathname}"]`
1079
1166
  );
@@ -1081,58 +1168,41 @@ async function handleTurbopackChunk(code, bundle, url) {
1081
1168
  return;
1082
1169
  }
1083
1170
  const self = globalThis;
1084
- const bundleKey = getBundleKey(bundle);
1085
- logDebug("ChunkLoader", `Bundle key: "${bundleKey}"`);
1171
+ const { globalKey } = scope;
1086
1172
  const transformedCode = code.replace(
1087
1173
  /globalThis\[\s*["']TURBOPACK["']\s*\]/g,
1088
- `globalThis["TURBOPACK_${bundleKey}"]`
1174
+ `globalThis["TURBOPACK_${globalKey}"]`
1089
1175
  ).replace(
1090
1176
  /self\[\s*["']TURBOPACK["']\s*\]/g,
1091
- `self["TURBOPACK_${bundleKey}"]`
1092
- ).replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${bundleKey}`).replace(/self\.TURBOPACK(?!_)/g, `self.TURBOPACK_${bundleKey}`).replace(
1177
+ `self["TURBOPACK_${globalKey}"]`
1178
+ ).replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${globalKey}`).replace(/self\.TURBOPACK(?!_)/g, `self.TURBOPACK_${globalKey}`).replace(
1093
1179
  /TURBOPACK_WORKER_LOCATION/g,
1094
- `TURBOPACK_WORKER_LOCATION_${bundleKey}`
1180
+ `TURBOPACK_WORKER_LOCATION_${globalKey}`
1095
1181
  ).replace(
1096
1182
  /TURBOPACK_NEXT_CHUNK_URLS/g,
1097
- `TURBOPACK_NEXT_CHUNK_URLS_${bundleKey}`
1183
+ `TURBOPACK_NEXT_CHUNK_URLS_${globalKey}`
1098
1184
  ).replace(
1099
1185
  /TURBOPACK_CHUNK_UPDATE_LISTENERS/g,
1100
- `TURBOPACK_CHUNK_UPDATE_LISTENERS_${bundleKey}`
1101
- ).replace(/__next_require__/g, `__${bundleKey}_next_require__`).replace(
1186
+ `TURBOPACK_CHUNK_UPDATE_LISTENERS_${globalKey}`
1187
+ ).replace(/__next_require__/g, `__${globalKey}_next_require__`).replace(
1102
1188
  /\/\/# sourceMappingURL=(?<name>.+)(?<optional>\._)?\.js\.map/g,
1103
- `//# sourceMappingURL=${new URL(
1104
- ".",
1105
- new URL(
1106
- url,
1107
- self.__remote_bundle_url__?.[bundle] ?? new URL(location.origin)
1108
- )
1109
- ).href}$1$2.js.map`
1189
+ `//# sourceMappingURL=${new URL(".", new URL(url, scope.url)).href}$1$2.js.map`
1110
1190
  );
1111
- if (!self[`TURBOPACK_${bundleKey}`]) {
1191
+ if (!self[`TURBOPACK_${globalKey}`]) {
1112
1192
  const chunkData = [];
1113
1193
  const turbopackObject = {
1114
1194
  push: (item) => {
1115
- logDebug(
1116
- "ChunkLoader",
1117
- `TURBOPACK_${bundleKey}.push() called with item type: ${Array.isArray(item) ? "array" : typeof item}`
1118
- );
1119
1195
  if (Array.isArray(item)) {
1120
1196
  chunkData.push(item);
1121
- logDebug(
1122
- "ChunkLoader",
1123
- `TURBOPACK_${bundleKey} now has ${chunkData.length} chunks`
1124
- );
1125
1197
  } else {
1126
1198
  chunkData.push([item]);
1127
1199
  }
1128
1200
  return chunkData.length;
1129
1201
  },
1130
- // Store chunks for later access
1131
1202
  __chunks__: chunkData
1132
1203
  };
1133
- self[`TURBOPACK_${bundleKey}`] = turbopackObject;
1204
+ self[`TURBOPACK_${globalKey}`] = turbopackObject;
1134
1205
  }
1135
- logDebug("ChunkLoader", `Creating blob script for: "${url}"`);
1136
1206
  await new Promise((scriptResolve, scriptReject) => {
1137
1207
  const blob = new Blob([transformedCode], {
1138
1208
  type: "application/javascript; charset=UTF-8"
@@ -1144,21 +1214,6 @@ async function handleTurbopackChunk(code, bundle, url) {
1144
1214
  script.async = true;
1145
1215
  script.onload = () => {
1146
1216
  URL.revokeObjectURL(scriptUrl);
1147
- logDebug(
1148
- "ChunkLoader",
1149
- `Script loaded successfully for bundle "${bundle}"`
1150
- );
1151
- const turbopackBundle = self[`TURBOPACK_${bundleKey}`];
1152
- logDebug(
1153
- "ChunkLoader",
1154
- `TURBOPACK_${bundleKey} type: ${typeof turbopackBundle}, isArray: ${Array.isArray(turbopackBundle)}, keys: ${turbopackBundle ? Object.keys(turbopackBundle).slice(0, 10).join(", ") : "none"}`
1155
- );
1156
- if (turbopackBundle && typeof turbopackBundle === "object") {
1157
- logDebug(
1158
- "ChunkLoader",
1159
- `TURBOPACK_${bundleKey} length/size: ${Array.isArray(turbopackBundle) ? turbopackBundle.length : Object.keys(turbopackBundle).length}`
1160
- );
1161
- }
1162
1217
  scriptResolve(void 0);
1163
1218
  script.remove();
1164
1219
  };
@@ -1173,43 +1228,177 @@ async function handleTurbopackChunk(code, bundle, url) {
1173
1228
  };
1174
1229
  document.head.appendChild(script);
1175
1230
  });
1176
- const chunkLists = self[`TURBOPACK_${bundleKey}_CHUNK_LISTS`];
1177
- logDebug(
1178
- "ChunkLoader",
1179
- `Processing chunk lists for bundle "${bundle}": ${chunkLists?.length ?? 0} lists`
1180
- );
1181
- const loadChunkLists = [];
1231
+ const chunkLists = self[`TURBOPACK_${globalKey}_CHUNK_LISTS`];
1232
+ const loadChunkPromises = [];
1182
1233
  while (chunkLists?.length) {
1183
1234
  const { chunks } = chunkLists.shift() ?? { chunks: [] };
1184
1235
  if (chunks.length > 0) {
1185
- logDebug(
1186
- "ChunkLoader",
1187
- `Loading ${chunks.length} additional chunks for bundle "${bundle}": [${chunks.join(", ")}]`
1188
- );
1189
- chunks.forEach((id) => {
1190
- const chunkLoadResult = self.__webpack_chunk_load__?.(
1191
- `[${bundle}] ${url.slice(0, url.indexOf("/_next"))}/_next/${id}`
1236
+ for (const id of chunks) {
1237
+ const baseUrl = url.slice(0, url.indexOf("/_next"));
1238
+ const chunkLoadResult = loadChunkWithScope(
1239
+ scope,
1240
+ formatRemoteId(scope, `${baseUrl}/_next/${id}`)
1192
1241
  );
1193
1242
  if (chunkLoadResult) {
1194
- loadChunkLists.push(chunkLoadResult);
1243
+ loadChunkPromises.push(chunkLoadResult);
1195
1244
  }
1196
- });
1245
+ }
1197
1246
  }
1198
1247
  }
1199
- if (loadChunkLists.length > 0) {
1200
- logDebug(
1201
- "ChunkLoader",
1202
- `Waiting for ${loadChunkLists.length} additional chunks to load`
1248
+ if (loadChunkPromises.length > 0) {
1249
+ await Promise.all(loadChunkPromises);
1250
+ }
1251
+ }
1252
+
1253
+ // src/runtime/turbopack/shared-modules.ts
1254
+ var DEDUPLICATION_WARNING = "This module will not be deduplicated \u2014 the remote may load its own copy, which can cause duplicate instance errors (e.g. invalid hook calls if React is loaded twice).";
1255
+ function getTurbopackModules(scope) {
1256
+ const self = globalThis;
1257
+ const raw = self[`TURBOPACK_${scope.globalKey}`];
1258
+ if (!raw)
1259
+ return void 0;
1260
+ if (typeof raw === "object" && "__chunks__" in raw) {
1261
+ return raw.__chunks__.flat();
1262
+ }
1263
+ if (Array.isArray(raw)) {
1264
+ return raw.flat();
1265
+ }
1266
+ return Object.entries(raw).flat();
1267
+ }
1268
+ async function initializeSharedModules(scope, hostShared = {}, remoteShared = {}) {
1269
+ const allModules = getTurbopackModules(scope);
1270
+ logDebug(
1271
+ "SharedModules",
1272
+ `initializeSharedModules: scope="${scope.scopedName}", allModules=${allModules ? allModules.length : "null"}, hostShared=[${Object.keys(hostShared).join(", ")}], remoteShared=${JSON.stringify(remoteShared)}`
1273
+ );
1274
+ let sharedModuleInitializer = null;
1275
+ if (allModules) {
1276
+ const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {
1277
+ if (typeof idOrFunc !== "function") {
1278
+ return false;
1279
+ }
1280
+ const funcCode = idOrFunc.toString();
1281
+ return REMOTE_SHARED_MARKER_RE.test(funcCode);
1282
+ });
1283
+ if (sharedModuleInitializerIndex > 0) {
1284
+ const sharedModuleInitializerCode = allModules[sharedModuleInitializerIndex].toString();
1285
+ const sharedModuleInitializerId = allModules[sharedModuleInitializerIndex - 1];
1286
+ const { sharedModuleId } = REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ?? {};
1287
+ if (sharedModuleId) {
1288
+ const { default: sharedModuleInitializerInstance } = handleTurbopackModule(
1289
+ scope,
1290
+ sharedModuleId,
1291
+ formatRemoteId(scope, String(sharedModuleInitializerId))
1292
+ );
1293
+ sharedModuleInitializer = sharedModuleInitializerInstance;
1294
+ }
1295
+ }
1296
+ if (sharedModuleInitializer) {
1297
+ const { shared: shared2 } = await sharedModuleInitializer;
1298
+ const sharedModuleIds = extractSharedModuleIds(shared2, scope);
1299
+ logDebug(
1300
+ "SharedModules",
1301
+ `Resolved shared modules for scope="${scope.scopedName}": ${JSON.stringify(sharedModuleIds)}`
1302
+ );
1303
+ return Promise.all(
1304
+ Object.entries(sharedModuleIds).map(async ([id, module2]) => {
1305
+ if (hostShared[module2]) {
1306
+ scope.sharedModules[id] = await hostShared[module2](scope.name);
1307
+ } else {
1308
+ logError(
1309
+ "SharedModules",
1310
+ `Host shared module "${module2}" not found for ID ${id}. ${DEDUPLICATION_WARNING}`
1311
+ );
1312
+ }
1313
+ })
1314
+ );
1315
+ }
1316
+ logWarn(
1317
+ "SharedModules",
1318
+ `No shared module initializer found in bundle for scope="${scope.scopedName}" \u2014 falling back to remoteShared mapping`
1319
+ );
1320
+ } else {
1321
+ logWarn(
1322
+ "SharedModules",
1323
+ `No TURBOPACK modules found for scope="${scope.scopedName}" (TURBOPACK_${scope.globalKey} is empty)`
1203
1324
  );
1204
- await Promise.all(loadChunkLists);
1205
1325
  }
1326
+ return Promise.all(
1327
+ Object.entries(remoteShared).map(async ([id, module2]) => {
1328
+ if (hostShared[module2]) {
1329
+ const normalizedId = id.replace("[app-ssr]", "[app-client]");
1330
+ scope.sharedModules[normalizedId] = await hostShared[module2](
1331
+ scope.name
1332
+ );
1333
+ } else {
1334
+ logError(
1335
+ "SharedModules",
1336
+ `Shared module "${module2}" not found for "${scope.name}". ${DEDUPLICATION_WARNING}`
1337
+ );
1338
+ }
1339
+ })
1340
+ );
1341
+ }
1342
+ function extractSharedModuleIds(shared2, scope) {
1343
+ return Object.entries(shared2).filter(([, value]) => typeof value === "function").reduce((acc, [key, value]) => {
1344
+ const { asyncSharedModuleId } = ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};
1345
+ if (asyncSharedModuleId) {
1346
+ const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);
1347
+ let asyncSharedModule;
1348
+ const turbopackModules = getTurbopackModules(scope) ?? [];
1349
+ const asyncSharedModuleIdIndex = turbopackModules.indexOf(
1350
+ asyncSharedModuleIdNumber
1351
+ );
1352
+ if (asyncSharedModuleIdIndex !== -1 && typeof turbopackModules[asyncSharedModuleIdIndex + 1] === "function") {
1353
+ asyncSharedModule = turbopackModules[asyncSharedModuleIdIndex + 1];
1354
+ }
1355
+ if (asyncSharedModule) {
1356
+ const asyncSharedModuleCode = asyncSharedModule.toString();
1357
+ const { sharedModuleId } = ASYNC_MODULE_RESOLVE_RE.exec(asyncSharedModuleCode)?.groups ?? ASYNC_MODULE_ALL_RE.exec(asyncSharedModuleCode)?.groups ?? {};
1358
+ acc[sharedModuleId ?? asyncSharedModuleId] = key.replace(
1359
+ "__remote_shared_module_",
1360
+ ""
1361
+ );
1362
+ }
1363
+ }
1364
+ return acc;
1365
+ }, {});
1366
+ }
1367
+ function getSharedModule(scope, id) {
1368
+ const idStr = String(id);
1369
+ if (scope.sharedModules[idStr] !== void 0) {
1370
+ return scope.sharedModules[idStr];
1371
+ }
1372
+ for (const [key, value] of Object.entries(scope.sharedModules)) {
1373
+ if (typeof value !== "undefined" && idStr !== key && idStr.endsWith(key)) {
1374
+ return value;
1375
+ }
1376
+ }
1377
+ return null;
1206
1378
  }
1207
1379
 
1208
1380
  // src/runtime/turbopack/module.ts
1209
- function handleTurbopackModule(bundle, moduleId, id) {
1381
+ function requireModule(scope, moduleId, fullId) {
1382
+ const idStr = String(moduleId);
1383
+ if (scope.moduleCache[idStr])
1384
+ return scope.moduleCache[idStr];
1385
+ const sharedModule = getSharedModule(scope, moduleId);
1386
+ if (sharedModule) {
1387
+ scope.moduleCache[idStr] = sharedModule;
1388
+ return sharedModule;
1389
+ }
1390
+ return handleTurbopackModule(
1391
+ scope,
1392
+ idStr,
1393
+ fullId ?? formatRemoteId(scope, idStr)
1394
+ );
1395
+ }
1396
+ function handleTurbopackModule(scope, moduleId, id) {
1210
1397
  const self = globalThis;
1211
- const bundleKey = getBundleKey(bundle);
1212
- const raw = self[`TURBOPACK_${bundleKey}`];
1398
+ if (scope.moduleCache[moduleId]) {
1399
+ return scope.moduleCache[moduleId];
1400
+ }
1401
+ const raw = self[`TURBOPACK_${scope.globalKey}`];
1213
1402
  let modules;
1214
1403
  if (raw && typeof raw === "object" && "__chunks__" in raw) {
1215
1404
  modules = raw.__chunks__.flat();
@@ -1218,48 +1407,35 @@ function handleTurbopackModule(bundle, moduleId, id) {
1218
1407
  } else {
1219
1408
  modules = raw;
1220
1409
  }
1221
- if (!self.__remote_components_turbopack_modules__) {
1222
- self.__remote_components_turbopack_modules__ = {};
1223
- }
1224
- if (!self.__remote_components_turbopack_modules__[bundle]) {
1225
- self.__remote_components_turbopack_modules__[bundle] = {};
1226
- }
1227
- if (self.__remote_components_turbopack_modules__[bundle][moduleId]) {
1228
- return self.__remote_components_turbopack_modules__[bundle][moduleId];
1229
- }
1230
1410
  if (!modules) {
1231
- logError("TurbopackModule", `TURBOPACK_${bundleKey} is undefined`);
1411
+ logError(
1412
+ "TurbopackModule",
1413
+ `TURBOPACK_${scope.globalKey} is undefined (scope: "${scope.scopedName}")`
1414
+ );
1232
1415
  }
1233
1416
  const moduleInit = findModuleInit(modules, moduleId);
1234
1417
  const exports = {};
1235
1418
  const moduleExports = { exports };
1236
1419
  if (typeof moduleInit !== "function") {
1237
1420
  throw new Error(
1238
- `Module ${id} not found in bundle ${bundle} with id ${moduleId}`
1421
+ `Module ${id} not found in bundle ${scope.name} with id ${moduleId}`
1239
1422
  );
1240
1423
  }
1241
- self.__remote_components_turbopack_modules__[bundle][moduleId] = moduleExports.exports;
1242
- if (!self.__remote_components_turbopack_global__) {
1243
- self.__remote_components_turbopack_global__ = {};
1244
- }
1245
- if (!self.__remote_components_turbopack_global__[bundle]) {
1246
- self.__remote_components_turbopack_global__[bundle] = {};
1247
- }
1424
+ scope.moduleCache[moduleId] = moduleExports.exports;
1248
1425
  moduleInit(
1249
1426
  createTurbopackContext(
1250
- bundle,
1427
+ scope,
1251
1428
  exports,
1252
1429
  moduleExports,
1253
1430
  modules,
1254
1431
  moduleInit,
1255
- id,
1256
- self
1432
+ id
1257
1433
  ),
1258
1434
  moduleExports,
1259
1435
  exports
1260
1436
  );
1261
- if (self.__remote_components_turbopack_modules__[bundle][moduleId] !== moduleExports.exports) {
1262
- self.__remote_components_turbopack_modules__[bundle][moduleId] = moduleExports.exports;
1437
+ if (scope.moduleCache[moduleId] !== moduleExports.exports) {
1438
+ scope.moduleCache[moduleId] = moduleExports.exports;
1263
1439
  }
1264
1440
  return moduleExports.exports;
1265
1441
  }
@@ -1284,7 +1460,8 @@ function findModuleInit(modules, moduleId) {
1284
1460
  (e) => Boolean(e && typeof e === "object" && moduleId in e)
1285
1461
  )?.[moduleId];
1286
1462
  }
1287
- function createTurbopackContext(bundle, exports, moduleExports, modules, moduleInit, id, self) {
1463
+ function createTurbopackContext(scope, exports, moduleExports, modules, moduleInit, id) {
1464
+ const scopedRequire = (moduleId) => requireModule(scope, moduleId, formatRemoteId(scope, String(moduleId)));
1288
1465
  return {
1289
1466
  // HMR not implemented for Remote Components
1290
1467
  k: {
@@ -1300,16 +1477,10 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1300
1477
  s(bindings, esmId) {
1301
1478
  let mod = exports;
1302
1479
  if (typeof esmId === "string" || typeof esmId === "number") {
1303
- if (!self.__remote_components_turbopack_modules__) {
1304
- self.__remote_components_turbopack_modules__ = {};
1305
- }
1306
- if (!self.__remote_components_turbopack_modules__[bundle]) {
1307
- self.__remote_components_turbopack_modules__[bundle] = {};
1480
+ if (!scope.moduleCache[esmId]) {
1481
+ scope.moduleCache[esmId] = {};
1308
1482
  }
1309
- if (!self.__remote_components_turbopack_modules__[bundle][esmId]) {
1310
- self.__remote_components_turbopack_modules__[bundle][esmId] = {};
1311
- }
1312
- mod = self.__remote_components_turbopack_modules__[bundle][esmId];
1483
+ mod = scope.moduleCache[esmId];
1313
1484
  }
1314
1485
  Object.defineProperty(mod, "__esModule", { value: true });
1315
1486
  if (Array.isArray(bindings)) {
@@ -1342,7 +1513,7 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1342
1513
  }
1343
1514
  }
1344
1515
  },
1345
- // import
1516
+ // import — resolves directly via scope, no global dispatch
1346
1517
  i(importId) {
1347
1518
  let mod;
1348
1519
  if (typeof importId === "string") {
@@ -1353,7 +1524,7 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1353
1524
  /\s+<export(?<specifier>.*)>$/,
1354
1525
  ""
1355
1526
  );
1356
- mod = self.__webpack_require__?.(`[${bundle}] ${normalizedId}`);
1527
+ mod = scopedRequire(normalizedId);
1357
1528
  if (mod && exportSource && exportName && (exportSource === "*" || typeof mod[exportSource] !== "undefined") && typeof mod[exportName] === "undefined") {
1358
1529
  if (exportSource === "*") {
1359
1530
  mod[exportName] = mod;
@@ -1362,7 +1533,7 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1362
1533
  }
1363
1534
  }
1364
1535
  } else {
1365
- mod = self.__webpack_require__?.(`[${bundle}] ${importId}`);
1536
+ mod = scopedRequire(importId);
1366
1537
  }
1367
1538
  if (typeof mod !== "object" || mod === null) {
1368
1539
  mod = { default: mod };
@@ -1374,16 +1545,14 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1374
1545
  }
1375
1546
  return mod;
1376
1547
  },
1377
- // require
1548
+ // require — resolves directly via scope
1378
1549
  r(requireId) {
1379
- return self.__webpack_require__?.(`[${bundle}] ${requireId}`);
1550
+ return scopedRequire(requireId);
1380
1551
  },
1381
1552
  // value exports
1382
1553
  v(value) {
1383
1554
  if (typeof value === "function") {
1384
- exports.default = value((vid) => {
1385
- return self.__webpack_require__?.(`[${bundle}] ${vid}`);
1386
- });
1555
+ exports.default = value((vid) => scopedRequire(vid));
1387
1556
  } else {
1388
1557
  moduleExports.exports = value;
1389
1558
  }
@@ -1398,17 +1567,15 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1398
1567
  );
1399
1568
  exports.default = result;
1400
1569
  },
1401
- // async module loader
1570
+ // async module loader — resolves directly via scope
1402
1571
  async A(Aid) {
1403
- const mod = self.__webpack_require__?.(`[${bundle}] ${Aid}`);
1404
- return mod.default(
1405
- (parentId) => self.__webpack_require__?.(`[${bundle}] ${parentId}`)
1406
- );
1572
+ const mod = scopedRequire(Aid);
1573
+ return mod.default((parentId) => scopedRequire(parentId));
1407
1574
  },
1408
1575
  // dynamic import tracking — no-op for remote components
1409
1576
  j() {
1410
1577
  },
1411
- // chunk loader
1578
+ // chunk loader — loads directly via scope, no global dispatch
1412
1579
  l(url) {
1413
1580
  const flatModules = Array.isArray(modules) ? modules : [];
1414
1581
  const moduleInitIndex = flatModules.indexOf(moduleInit);
@@ -1419,173 +1586,69 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1419
1586
  const scriptSrc = script.getAttribute("data-turbopack-src") || "";
1420
1587
  const nextIndex = scriptSrc.indexOf("/_next");
1421
1588
  const baseUrl = nextIndex !== -1 ? scriptSrc.slice(0, nextIndex) : "";
1422
- const bundleUrl = `[${bundle}] ${baseUrl}/_next/${url}`;
1423
- return self.__webpack_chunk_load__?.(bundleUrl, bundle);
1589
+ const chunkUrl = `${baseUrl}/_next/${url}`;
1590
+ return loadChunkWithScope(scope, formatRemoteId(scope, chunkUrl));
1424
1591
  }
1425
1592
  }
1426
1593
  throw new Error(
1427
1594
  `Failed to load Turbopack chunk "${url}" for module "${id}". Check the URL is correct.`
1428
1595
  );
1429
1596
  },
1430
- // global object for this bundle
1431
- g: self.__remote_components_turbopack_global__?.[bundle],
1597
+ // globalThis substitute shared across all modules in this scope
1598
+ g: scope.moduleGlobal,
1432
1599
  m: moduleExports,
1433
1600
  e: exports
1434
1601
  };
1435
1602
  }
1436
1603
 
1437
- // src/runtime/turbopack/shared-modules.ts
1438
- var DEDUPLICATION_WARNING = "This module will not be deduplicated \u2014 the remote may load its own copy, which can cause duplicate instance errors (e.g. invalid hook calls if React is loaded twice).";
1439
- async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {}) {
1440
- const self = globalThis;
1441
- self.__remote_shared_modules__ = self.__remote_shared_modules__ ?? {};
1442
- if (!self.__remote_shared_modules__[bundle]) {
1443
- self.__remote_shared_modules__[bundle] = {};
1444
- }
1445
- const bundleKey = getBundleKey(bundle);
1446
- let modules = self[`TURBOPACK_${bundleKey}`];
1447
- if (modules && typeof modules === "object" && "__chunks__" in modules) {
1448
- const chunks = modules.__chunks__;
1449
- modules = chunks.flat();
1450
- }
1451
- let sharedModuleInitializer = null;
1452
- if (modules) {
1453
- const allModules = Array.isArray(modules) ? modules.flat() : Object.entries(modules).flat();
1454
- const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {
1455
- if (typeof idOrFunc !== "function") {
1456
- return false;
1457
- }
1458
- const funcCode = idOrFunc.toString();
1459
- return REMOTE_SHARED_MARKER_RE.test(funcCode);
1460
- });
1461
- if (sharedModuleInitializerIndex > 0) {
1462
- const sharedModuleInitializerCode = allModules[sharedModuleInitializerIndex].toString();
1463
- const sharedModuleInitializerId = allModules[sharedModuleInitializerIndex - 1];
1464
- const { sharedModuleId } = REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ?? {};
1465
- if (sharedModuleId) {
1466
- const { default: sharedModuleInitializerInstance } = handleTurbopackModule(
1467
- bundle,
1468
- sharedModuleId,
1469
- `[${bundle}] ${sharedModuleInitializerId}`
1470
- );
1471
- sharedModuleInitializer = sharedModuleInitializerInstance;
1472
- }
1473
- }
1474
- if (sharedModuleInitializer) {
1475
- const { shared: shared2 } = await sharedModuleInitializer;
1476
- const sharedModuleIds = extractSharedModuleIds(
1477
- shared2,
1478
- bundleKey,
1479
- self
1480
- );
1481
- return Promise.all(
1482
- Object.entries(sharedModuleIds).map(async ([id, module2]) => {
1483
- if (self.__remote_shared_modules__?.[bundle]) {
1484
- if (hostShared[module2]) {
1485
- self.__remote_shared_modules__[bundle][id] = await hostShared[module2](bundle);
1486
- } else {
1487
- logError(
1488
- "SharedModules",
1489
- `Host shared module "${module2}" not found for ID ${id}. ${DEDUPLICATION_WARNING}`
1490
- );
1491
- }
1492
- }
1493
- })
1494
- );
1495
- }
1496
- }
1497
- return Promise.all(
1498
- Object.entries(remoteShared).map(async ([id, module2]) => {
1499
- if (self.__remote_shared_modules__?.[bundle]) {
1500
- if (hostShared[module2]) {
1501
- const normalizedId = id.replace("[app-ssr]", "[app-client]");
1502
- self.__remote_shared_modules__[bundle][normalizedId] = await hostShared[module2](bundle);
1503
- } else {
1504
- logError(
1505
- "SharedModules",
1506
- `Shared module "${module2}" not found for "${bundle}". ${DEDUPLICATION_WARNING}`
1507
- );
1508
- }
1509
- }
1510
- })
1511
- );
1512
- }
1513
- function extractSharedModuleIds(shared2, bundleKey, self) {
1514
- return Object.entries(shared2).filter(([, value]) => typeof value === "function").reduce((acc, [key, value]) => {
1515
- const { asyncSharedModuleId } = ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};
1516
- if (asyncSharedModuleId) {
1517
- const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);
1518
- let asyncSharedModule;
1519
- let turbopackModules = self[`TURBOPACK_${bundleKey}`];
1520
- if (turbopackModules && typeof turbopackModules === "object" && "__chunks__" in turbopackModules) {
1521
- const chunks = turbopackModules.__chunks__;
1522
- turbopackModules = chunks.flat();
1523
- }
1524
- const newAllModules = Array.isArray(turbopackModules) ? turbopackModules.flat() : turbopackModules ? Object.entries(turbopackModules).flatMap(([key2, value2]) => [
1525
- key2,
1526
- value2
1527
- ]) : [];
1528
- const asyncSharedModuleIdIndex = newAllModules.indexOf(
1529
- asyncSharedModuleIdNumber
1530
- );
1531
- if (asyncSharedModuleIdIndex !== -1 && typeof newAllModules[asyncSharedModuleIdIndex + 1] === "function") {
1532
- asyncSharedModule = newAllModules[asyncSharedModuleIdIndex + 1];
1533
- }
1534
- if (asyncSharedModule) {
1535
- const asyncSharedModuleCode = asyncSharedModule.toString();
1536
- const { sharedModuleId } = ASYNC_MODULE_RESOLVE_RE.exec(asyncSharedModuleCode)?.groups ?? ASYNC_MODULE_ALL_RE.exec(asyncSharedModuleCode)?.groups ?? {};
1537
- acc[sharedModuleId ?? asyncSharedModuleId] = key.replace(
1538
- "__remote_shared_module_",
1539
- ""
1540
- );
1541
- }
1542
- }
1543
- return acc;
1544
- }, {});
1545
- }
1546
- function getSharedModule(bundle, id) {
1547
- const self = globalThis;
1548
- for (const [key, value] of Object.entries(
1549
- self.__remote_shared_modules__?.[bundle] ?? {}
1550
- )) {
1551
- if (typeof value !== "undefined" && (typeof id === "string" && id.includes(key) || id === key)) {
1552
- return value;
1553
- }
1554
- }
1555
- return null;
1556
- }
1557
-
1558
1604
  // src/runtime/turbopack/webpack-runtime.ts
1559
1605
  async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location.href), bundle, shared2 = {}, remoteShared = {}, resolveClientUrl) {
1560
1606
  const self = globalThis;
1607
+ const bundleName = bundle ?? "default";
1608
+ const scope = createScope(bundleName, url, runtime, resolveClientUrl);
1609
+ registerScope(scope);
1561
1610
  if (!self.__remote_bundle_url__) {
1562
1611
  self.__remote_bundle_url__ = {};
1563
1612
  }
1564
- self.__remote_bundle_url__[bundle ?? "default"] = url;
1613
+ self.__remote_bundle_url__[bundleName] = url;
1614
+ if (scope.scopedName !== bundleName) {
1615
+ self.__remote_bundle_url__[scope.scopedName] = url;
1616
+ }
1565
1617
  self.__webpack_get_script_filename__ = () => null;
1566
- if (typeof self.__webpack_require__ !== "function" || self.__webpack_require_type__ !== "turbopack") {
1618
+ const willCreateDispatchers = typeof self.__webpack_require__ !== "function" || self.__webpack_require_type__ !== "turbopack";
1619
+ if (willCreateDispatchers) {
1567
1620
  if (!self.__original_webpack_require__ && !self.__original_webpack_chunk_load__) {
1568
1621
  self.__original_webpack_chunk_load__ = self.__webpack_chunk_load__;
1569
1622
  self.__original_webpack_require__ = self.__webpack_require__;
1570
1623
  }
1571
- self.__webpack_chunk_load__ = createChunkLoader(runtime, resolveClientUrl);
1572
- self.__webpack_require__ = createModuleRequire(runtime);
1624
+ self.__webpack_chunk_load__ = createChunkDispatcher();
1625
+ self.__webpack_require__ = createModuleDispatcher(runtime);
1573
1626
  self.__webpack_require_type__ = runtime;
1574
1627
  if (self.__remote_webpack_require__ && runtime === RUNTIME_TURBOPACK) {
1575
- const remoteBundle = bundle ?? "default";
1576
- self.__remote_webpack_require__[remoteBundle] = self.__webpack_require__;
1577
- self.__remote_webpack_require__[remoteBundle].type = "turbopack";
1628
+ self.__remote_webpack_require__[bundleName] = self.__webpack_require__;
1629
+ self.__remote_webpack_require__[bundleName].type = "turbopack";
1578
1630
  }
1579
1631
  }
1632
+ if (self.__remote_webpack_require__?.[bundleName] && scope.scopedName !== bundleName) {
1633
+ self.__remote_webpack_require__[scope.scopedName] = self.__remote_webpack_require__[bundleName];
1634
+ }
1580
1635
  if (runtime === RUNTIME_TURBOPACK) {
1581
- await Promise.all(
1636
+ const results = await Promise.allSettled(
1582
1637
  scripts.map((script) => {
1583
1638
  if (script.src) {
1584
- return self.__webpack_chunk_load__?.(script.src, bundle);
1639
+ return loadChunkWithScope(scope, script.src);
1585
1640
  }
1586
1641
  return Promise.resolve(void 0);
1587
1642
  })
1588
1643
  );
1644
+ for (const result of results) {
1645
+ if (result.status === "rejected") {
1646
+ logWarn(
1647
+ "WebpackRuntime",
1648
+ `Initial chunk load failed: ${String(result.reason)}`
1649
+ );
1650
+ }
1651
+ }
1589
1652
  }
1590
1653
  const coreShared = {
1591
1654
  react: async () => (await import("react")).default,
@@ -1595,54 +1658,54 @@ async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location
1595
1658
  "react-dom/client": async () => (await import("react-dom/client")).default,
1596
1659
  ...shared2
1597
1660
  };
1598
- await initializeSharedModules(
1599
- bundle ?? "default",
1600
- // include all core modules as shared
1601
- coreShared,
1602
- remoteShared
1603
- );
1661
+ await initializeSharedModules(scope, coreShared, remoteShared);
1604
1662
  }
1605
- function createModuleRequire(runtime) {
1663
+ function createModuleDispatcher(runtime) {
1606
1664
  return (id) => {
1607
1665
  const self = globalThis;
1608
1666
  const { bundle, id: moduleId } = id.match(REMOTE_COMPONENT_REGEX)?.groups ?? {
1609
1667
  bundle: "default",
1610
1668
  id
1611
1669
  };
1612
- const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : runtime;
1613
- logDebug("WebpackRuntime", `remoteRuntime: "${remoteRuntime}"`);
1670
+ const bundleName = bundle ?? "default";
1671
+ const remoteRuntime = self.__remote_webpack_require__?.[bundleName] ? self.__remote_webpack_require__[bundleName]?.type || "webpack" : runtime;
1672
+ logDebug(
1673
+ "ModuleDispatcher",
1674
+ `Resolving "${id}" (bundle: "${bundleName}", runtime: "${remoteRuntime}")`
1675
+ );
1614
1676
  try {
1615
1677
  if (remoteRuntime === RUNTIME_WEBPACK && bundle && moduleId) {
1616
1678
  return self.__remote_webpack_require__?.[bundle]?.(moduleId);
1617
1679
  }
1618
- const sharedModuleId = moduleId ?? id;
1619
- const sharedModule = getSharedModule(bundle ?? "default", sharedModuleId);
1620
- if (sharedModule) {
1621
- return sharedModule;
1680
+ const scope = getScope(bundleName);
1681
+ if (scope) {
1682
+ return requireModule(scope, moduleId ?? id, id);
1622
1683
  }
1623
- if (bundle && moduleId) {
1624
- return handleTurbopackModule(bundle, moduleId, id);
1625
- }
1626
- throw new Error(`Module "${id}" not found.`);
1684
+ throw new Error(
1685
+ `Module "${id}" not found \u2014 no scope for bundle "${bundleName}".`
1686
+ );
1627
1687
  } catch (requireError) {
1628
1688
  logWarn(
1629
- "WebpackRuntime",
1689
+ "ModuleDispatcher",
1630
1690
  `Module require failed: ${String(requireError)}`
1631
1691
  );
1632
1692
  if (typeof self.__original_webpack_require__ !== "function") {
1633
1693
  throw new RemoteComponentsError(
1634
- `Module "${id}" not found in remote component bundle "${bundle}".`,
1694
+ `Module "${id}" not found in remote component bundle "${bundleName}".`,
1635
1695
  {
1636
1696
  cause: requireError instanceof Error ? requireError : void 0
1637
1697
  }
1638
1698
  );
1639
1699
  }
1640
1700
  try {
1641
- logDebug("WebpackRuntime", "Falling back to original webpack require");
1701
+ logDebug(
1702
+ "ModuleDispatcher",
1703
+ "Falling back to original webpack require"
1704
+ );
1642
1705
  return self.__original_webpack_require__(id);
1643
1706
  } catch (originalError) {
1644
1707
  throw new RemoteComponentsError(
1645
- `Module "${id}" not found in remote component bundle "${bundle}".`,
1708
+ `Module "${id}" not found in remote component bundle "${bundleName}".`,
1646
1709
  { cause: originalError instanceof Error ? originalError : void 0 }
1647
1710
  );
1648
1711
  }
@@ -2042,7 +2105,7 @@ function getRemoteComponentHtml(html) {
2042
2105
  return ssrRemoteComponentContainer.innerHTML;
2043
2106
  }
2044
2107
  const remoteComponentContainer = temp.querySelectorAll(
2045
- `div[data-bundle][data-route][data-runtime][id^="__vercel_remote_component"],div[data-bundle][data-route],div#__next,remote-component:not([src])`
2108
+ `div[data-bundle][data-route][data-runtime][id^="${DEFAULT_COMPONENT_NAME}"],div[data-bundle][data-route],div#__next,remote-component:not([src])`
2046
2109
  );
2047
2110
  if (remoteComponentContainer.length > 0) {
2048
2111
  return `${Array.from(temp.querySelectorAll("link,script")).map((link) => link.outerHTML).join("")}${Array.from(remoteComponentContainer).map((container) => container.outerHTML).join("")}`;
@@ -2062,7 +2125,7 @@ function ConsumeRemoteComponent({
2062
2125
  mode = "open",
2063
2126
  reset,
2064
2127
  credentials: credentialsProp,
2065
- name: nameProp = "__vercel_remote_component",
2128
+ name: nameProp = DEFAULT_COMPONENT_NAME,
2066
2129
  shared: sharedProp,
2067
2130
  children,
2068
2131
  onBeforeLoad,
@@ -2401,10 +2464,7 @@ function ConsumeRemoteComponent({
2401
2464
  };
2402
2465
  return {
2403
2466
  src: new URL(
2404
- `${prefix ?? ""}${path}`.replace(
2405
- /(?<char>[^:])(?<double>\/\/)/g,
2406
- "$1/"
2407
- ),
2467
+ collapseDoubleSlashes(`${prefix ?? ""}${path}`),
2408
2468
  url
2409
2469
  ).href
2410
2470
  };