remote-components 0.3.1 → 0.3.3

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 (131) hide show
  1. package/dist/config/nextjs.cjs +36 -25
  2. package/dist/config/nextjs.cjs.map +1 -1
  3. package/dist/config/nextjs.js +30 -19
  4. package/dist/config/nextjs.js.map +1 -1
  5. package/dist/host/defaults/app.cjs +2 -5
  6. package/dist/host/defaults/app.cjs.map +1 -1
  7. package/dist/host/defaults/app.d.ts +1 -3
  8. package/dist/host/defaults/app.js +1 -4
  9. package/dist/host/defaults/app.js.map +1 -1
  10. package/dist/host/defaults/pages.cjs +2 -5
  11. package/dist/host/defaults/pages.cjs.map +1 -1
  12. package/dist/host/defaults/pages.d.ts +1 -3
  13. package/dist/host/defaults/pages.js +1 -4
  14. package/dist/host/defaults/pages.js.map +1 -1
  15. package/dist/host/defaults/shared.cjs +32 -0
  16. package/dist/host/defaults/shared.cjs.map +1 -0
  17. package/dist/host/defaults/shared.d.ts +3 -0
  18. package/dist/host/defaults/shared.js +8 -0
  19. package/dist/host/defaults/shared.js.map +1 -0
  20. package/dist/host/html.cjs +390 -357
  21. package/dist/host/html.cjs.map +1 -1
  22. package/dist/host/html.js +390 -357
  23. package/dist/host/html.js.map +1 -1
  24. package/dist/host/nextjs/app/client-only.cjs +477 -453
  25. package/dist/host/nextjs/app/client-only.cjs.map +1 -1
  26. package/dist/host/nextjs/app/client-only.js +485 -461
  27. package/dist/host/nextjs/app/client-only.js.map +1 -1
  28. package/dist/host/react.cjs +382 -367
  29. package/dist/host/react.cjs.map +1 -1
  30. package/dist/host/react.js +382 -367
  31. package/dist/host/react.js.map +1 -1
  32. package/dist/internal/host/nextjs/app-client.cjs +2 -19
  33. package/dist/internal/host/nextjs/app-client.cjs.map +1 -1
  34. package/dist/internal/host/nextjs/app-client.js +1 -8
  35. package/dist/internal/host/nextjs/app-client.js.map +1 -1
  36. package/dist/internal/host/nextjs/image-shared.cjs +2 -2
  37. package/dist/internal/host/nextjs/image-shared.cjs.map +1 -1
  38. package/dist/internal/host/nextjs/image-shared.js +2 -2
  39. package/dist/internal/host/nextjs/image-shared.js.map +1 -1
  40. package/dist/internal/host/nextjs/shared-import.cjs +46 -0
  41. package/dist/internal/host/nextjs/shared-import.cjs.map +1 -0
  42. package/dist/internal/host/nextjs/shared-import.d.ts +3 -0
  43. package/dist/internal/host/nextjs/shared-import.js +12 -0
  44. package/dist/internal/host/nextjs/shared-import.js.map +1 -0
  45. package/dist/internal/host/server/fetch-remote-component.cjs +20 -1
  46. package/dist/internal/host/server/fetch-remote-component.cjs.map +1 -1
  47. package/dist/internal/host/server/fetch-remote-component.js +20 -1
  48. package/dist/internal/host/server/fetch-remote-component.js.map +1 -1
  49. package/dist/internal/host/shared/remote-image-loader.cjs +2 -5
  50. package/dist/internal/host/shared/remote-image-loader.cjs.map +1 -1
  51. package/dist/internal/host/shared/remote-image-loader.js +2 -5
  52. package/dist/internal/host/shared/remote-image-loader.js.map +1 -1
  53. package/dist/internal/runtime/loaders/script-loader.cjs +7 -0
  54. package/dist/internal/runtime/loaders/script-loader.cjs.map +1 -1
  55. package/dist/internal/runtime/loaders/script-loader.js +7 -0
  56. package/dist/internal/runtime/loaders/script-loader.js.map +1 -1
  57. package/dist/internal/runtime/turbopack/chunk-loader.cjs +76 -126
  58. package/dist/internal/runtime/turbopack/chunk-loader.cjs.map +1 -1
  59. package/dist/internal/runtime/turbopack/chunk-loader.d.ts +22 -7
  60. package/dist/internal/runtime/turbopack/chunk-loader.js +85 -131
  61. package/dist/internal/runtime/turbopack/chunk-loader.js.map +1 -1
  62. package/dist/internal/runtime/turbopack/module.cjs +53 -65
  63. package/dist/internal/runtime/turbopack/module.cjs.map +1 -1
  64. package/dist/internal/runtime/turbopack/module.d.ts +14 -11
  65. package/dist/internal/runtime/turbopack/module.js +51 -64
  66. package/dist/internal/runtime/turbopack/module.js.map +1 -1
  67. package/dist/internal/runtime/turbopack/remote-scope.cjs +101 -0
  68. package/dist/internal/runtime/turbopack/remote-scope.cjs.map +1 -0
  69. package/dist/internal/runtime/turbopack/remote-scope.d.ts +55 -0
  70. package/dist/internal/runtime/turbopack/remote-scope.js +73 -0
  71. package/dist/internal/runtime/turbopack/remote-scope.js.map +1 -0
  72. package/dist/internal/runtime/turbopack/shared-modules.cjs +64 -56
  73. package/dist/internal/runtime/turbopack/shared-modules.cjs.map +1 -1
  74. package/dist/internal/runtime/turbopack/shared-modules.d.ts +13 -4
  75. package/dist/internal/runtime/turbopack/shared-modules.js +64 -57
  76. package/dist/internal/runtime/turbopack/shared-modules.js.map +1 -1
  77. package/dist/internal/runtime/turbopack/webpack-runtime.cjs +39 -29
  78. package/dist/internal/runtime/turbopack/webpack-runtime.cjs.map +1 -1
  79. package/dist/internal/runtime/turbopack/webpack-runtime.d.ts +5 -2
  80. package/dist/internal/runtime/turbopack/webpack-runtime.js +42 -32
  81. package/dist/internal/runtime/turbopack/webpack-runtime.js.map +1 -1
  82. package/dist/internal/runtime/types.cjs.map +1 -1
  83. package/dist/internal/runtime/types.d.ts +4 -3
  84. package/dist/internal/utils/logger.cjs.map +1 -1
  85. package/dist/internal/utils/logger.d.ts +1 -1
  86. package/dist/internal/utils/logger.js.map +1 -1
  87. package/dist/internal/utils/project-id-env.cjs +31 -0
  88. package/dist/internal/utils/project-id-env.cjs.map +1 -0
  89. package/dist/internal/utils/project-id-env.d.ts +7 -0
  90. package/dist/internal/utils/project-id-env.js +7 -0
  91. package/dist/internal/utils/project-id-env.js.map +1 -0
  92. package/dist/internal/utils/project-id.cjs +46 -0
  93. package/dist/internal/utils/project-id.cjs.map +1 -0
  94. package/dist/internal/utils/project-id.d.ts +8 -0
  95. package/dist/internal/utils/project-id.js +22 -0
  96. package/dist/internal/utils/project-id.js.map +1 -0
  97. package/dist/internal/utils.cjs +5 -0
  98. package/dist/internal/utils.cjs.map +1 -1
  99. package/dist/internal/utils.d.ts +15 -1
  100. package/dist/internal/utils.js +4 -0
  101. package/dist/internal/utils.js.map +1 -1
  102. package/dist/remote/defaults/app.cjs +2 -5
  103. package/dist/remote/defaults/app.cjs.map +1 -1
  104. package/dist/remote/defaults/app.d.ts +1 -3
  105. package/dist/remote/defaults/app.js +1 -4
  106. package/dist/remote/defaults/app.js.map +1 -1
  107. package/dist/remote/defaults/pages.cjs +2 -5
  108. package/dist/remote/defaults/pages.cjs.map +1 -1
  109. package/dist/remote/defaults/pages.d.ts +1 -3
  110. package/dist/remote/defaults/pages.js +1 -4
  111. package/dist/remote/defaults/pages.js.map +1 -1
  112. package/dist/{internal/host/shared/resolved-data.cjs → remote/defaults/shared.cjs} +18 -3
  113. package/dist/remote/defaults/shared.cjs.map +1 -0
  114. package/dist/remote/defaults/shared.d.ts +3 -0
  115. package/dist/remote/defaults/shared.js +8 -0
  116. package/dist/remote/defaults/shared.js.map +1 -0
  117. package/dist/remote/html.cjs.map +1 -1
  118. package/dist/remote/html.js.map +1 -1
  119. package/dist/remote/nextjs/app.cjs +2 -1
  120. package/dist/remote/nextjs/app.cjs.map +1 -1
  121. package/dist/remote/nextjs/app.js +2 -1
  122. package/dist/remote/nextjs/app.js.map +1 -1
  123. package/dist/remote/nextjs/pages.cjs +2 -1
  124. package/dist/remote/nextjs/pages.cjs.map +1 -1
  125. package/dist/remote/nextjs/pages.js +2 -1
  126. package/dist/remote/nextjs/pages.js.map +1 -1
  127. package/package.json +1 -1
  128. package/dist/internal/host/shared/resolved-data.cjs.map +0 -1
  129. package/dist/internal/host/shared/resolved-data.d.ts +0 -48
  130. package/dist/internal/host/shared/resolved-data.js +0 -1
  131. package/dist/internal/host/shared/resolved-data.js.map +0 -1
@@ -42,19 +42,27 @@ var init_constants = __esm({
42
42
  }
43
43
  });
44
44
 
45
+ // src/host/defaults/shared.ts
46
+ var shared;
47
+ var init_shared = __esm({
48
+ "src/host/defaults/shared.ts"() {
49
+ "use strict";
50
+ init_constants();
51
+ shared = {
52
+ __remote_components_missing_shared__: () => Promise.reject(new Error(MISSING_SHARED_MODULES_MESSAGE))
53
+ };
54
+ }
55
+ });
56
+
45
57
  // src/host/defaults/app.ts
46
58
  var app_exports = {};
47
59
  __export(app_exports, {
48
60
  shared: () => shared
49
61
  });
50
- var shared;
51
62
  var init_app = __esm({
52
63
  "src/host/defaults/app.ts"() {
53
64
  "use strict";
54
- init_constants();
55
- shared = {
56
- __remote_components_missing_shared__: () => Promise.reject(new Error(MISSING_SHARED_MODULES_MESSAGE))
57
- };
65
+ init_shared();
58
66
  }
59
67
  });
60
68
 
@@ -67,90 +75,40 @@ __export(app_client_only_exports, {
67
75
  module.exports = __toCommonJS(app_client_only_exports);
68
76
  var import_react5 = require("react");
69
77
 
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
- };
78
+ // src/utils/index.ts
79
+ function escapeString(str) {
80
+ return str.replace(/[^a-z0-9]/g, "_");
107
81
  }
108
- function parseOrigin(url) {
109
- try {
110
- return new URL(url).origin;
111
- } catch {
112
- return void 0;
113
- }
82
+ function computeScopedName(name, options) {
83
+ return options.isCrossOrigin ? `${name}_${escapeString(options.remoteHost.toLowerCase())}` : name;
114
84
  }
85
+ var attrToProp = {
86
+ fetchpriority: "fetchPriority",
87
+ crossorigin: "crossOrigin",
88
+ imagesrcset: "imageSrcSet",
89
+ imagesizes: "imageSizes",
90
+ srcset: "srcSet"
91
+ };
115
92
 
116
- // src/runtime/url/default-resolve-client-url.ts
117
- function bindResolveClientUrl(prop, remoteSrc) {
118
- return prop ? withRemoteSrc(prop, remoteSrc) : void 0;
93
+ // src/runtime/constants.ts
94
+ var DEFAULT_BUNDLE_NAME = "__vercel_remote_bundle";
95
+ var DEFAULT_COMPONENT_NAME = "__vercel_remote_component";
96
+ var DEFAULT_ROUTE = "/";
97
+ var RUNTIME_WEBPACK = "webpack";
98
+ var RUNTIME_TURBOPACK = "turbopack";
99
+ var RUNTIME_SCRIPT = "script";
100
+ function getBundleKey(bundle) {
101
+ return escapeString(bundle);
119
102
  }
120
103
 
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
- };
104
+ // src/runtime/patterns.ts
105
+ var REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
106
+ var NEXT_BUNDLE_PATH_RE = /\/_next\/\[.+\](?:%20| )/;
107
+ var DOUBLE_SLASH_RE = /(?<!:)\/\//g;
108
+ function collapseDoubleSlashes(path) {
109
+ return path.replace(DOUBLE_SLASH_RE, "/");
144
110
  }
145
111
 
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
112
  // src/utils/logger.ts
155
113
  init_constants();
156
114
 
@@ -297,6 +255,158 @@ function warnCrossOriginFetchError(logLocation, url) {
297
255
  }
298
256
  }
299
257
 
258
+ // src/runtime/turbopack/remote-scope.ts
259
+ function getRegistry() {
260
+ const self = globalThis;
261
+ if (!self.__remote_component_scopes__) {
262
+ self.__remote_component_scopes__ = /* @__PURE__ */ new Map();
263
+ }
264
+ return self.__remote_component_scopes__;
265
+ }
266
+ function createScope(name, url, runtime, resolveClientUrl) {
267
+ const isCrossOrigin = url.origin !== location.origin;
268
+ const scopedName = computeScopedName(name, {
269
+ remoteHost: url.host,
270
+ isCrossOrigin
271
+ });
272
+ const globalKey = getBundleKey(scopedName);
273
+ return {
274
+ name,
275
+ scopedName,
276
+ globalKey,
277
+ url,
278
+ runtime,
279
+ resolveClientUrl,
280
+ moduleCache: {},
281
+ sharedModules: {},
282
+ moduleGlobal: {}
283
+ };
284
+ }
285
+ function registerScope(scope) {
286
+ const registry = getRegistry();
287
+ registry.set(scope.scopedName, scope);
288
+ if (scope.scopedName !== scope.name) {
289
+ const existing = registry.get(scope.name);
290
+ if (existing && existing.scopedName !== scope.scopedName) {
291
+ logWarn(
292
+ "RemoteScope",
293
+ `Plain name "${scope.name}" already registered by scope "${existing.scopedName}" \u2014 overwriting with "${scope.scopedName}". Static hosts will only resolve the latest one.`
294
+ );
295
+ }
296
+ registry.set(scope.name, scope);
297
+ }
298
+ logDebug(
299
+ "RemoteScope",
300
+ `Registered scope "${scope.scopedName}" (${registry.size} total)`
301
+ );
302
+ }
303
+ function getScope(name) {
304
+ return getRegistry().get(name);
305
+ }
306
+ function formatRemoteId(scope, path) {
307
+ return `[${scope.scopedName}] ${path}`;
308
+ }
309
+ function parseRemoteId(id) {
310
+ const groups = REMOTE_COMPONENT_REGEX.exec(id)?.groups;
311
+ if (groups?.bundle && groups.id) {
312
+ return {
313
+ bundle: groups.bundle,
314
+ path: groups.id,
315
+ prefix: groups.prefix ?? ""
316
+ };
317
+ }
318
+ return { bundle: "default", path: id, prefix: "" };
319
+ }
320
+
321
+ // src/host/shared/remote-image-loader.ts
322
+ function createRemoteImageLoader(bundle, resolveClientUrl) {
323
+ const loader = Object.assign(
324
+ ({
325
+ config,
326
+ src,
327
+ width,
328
+ quality
329
+ }) => {
330
+ const q = quality ?? 75;
331
+ const remoteOrigin = getScope(bundle)?.url.origin ?? "";
332
+ const isCrossOrigin = remoteOrigin && remoteOrigin !== location.origin;
333
+ const basePath = isCrossOrigin ? `${remoteOrigin}${config.path ?? "/_next/image"}` : config.path ?? `${remoteOrigin}/_next/image`;
334
+ const url = `${basePath}?url=${encodeURIComponent(src)}&w=${width}&q=${q}`;
335
+ return resolveClientUrl?.(url) ?? url;
336
+ },
337
+ // Signals to getImgProps that this is a default loader (not a user-defined
338
+ // one), enabling srcSet generation with device/image sizes from the config.
339
+ { __next_img_default: true }
340
+ );
341
+ return loader;
342
+ }
343
+
344
+ // src/runtime/url/resolve-client-url.ts
345
+ function withRemoteSrc(resolveClientUrl, remoteSrc) {
346
+ const remoteOrigin = parseOrigin(remoteSrc);
347
+ return (url) => {
348
+ const urlOrigin = parseOrigin(url);
349
+ if (remoteOrigin && urlOrigin && urlOrigin !== remoteOrigin) {
350
+ return void 0;
351
+ }
352
+ return resolveClientUrl(remoteSrc, url);
353
+ };
354
+ }
355
+ function parseOrigin(url) {
356
+ try {
357
+ return new URL(url).origin;
358
+ } catch {
359
+ return void 0;
360
+ }
361
+ }
362
+
363
+ // src/runtime/url/default-resolve-client-url.ts
364
+ function bindResolveClientUrl(prop, remoteSrc) {
365
+ return prop ? withRemoteSrc(prop, remoteSrc) : void 0;
366
+ }
367
+
368
+ // src/host/nextjs/image-shared.ts
369
+ function resolveForBundle(unbound, bundle) {
370
+ if (!unbound)
371
+ return void 0;
372
+ const remoteSrc = getScope(bundle)?.url.href ?? "";
373
+ return bindResolveClientUrl(unbound, remoteSrc);
374
+ }
375
+ function createImageLoaderSharedEntries({
376
+ bound,
377
+ unbound
378
+ } = {}) {
379
+ const entry = (bundle) => {
380
+ const resolveClientUrl = bound ?? resolveForBundle(unbound, bundle);
381
+ return Promise.resolve({
382
+ default: createRemoteImageLoader(bundle, resolveClientUrl),
383
+ __esModule: true
384
+ });
385
+ };
386
+ return {
387
+ "next/dist/shared/lib/image-loader": entry,
388
+ "next/dist/esm/shared/lib/image-loader": entry
389
+ };
390
+ }
391
+
392
+ // src/host/nextjs/shared-import.ts
393
+ async function tryImportShared() {
394
+ try {
395
+ const { shared: shared2 } = await Promise.resolve().then(() => (init_app(), app_exports));
396
+ return shared2;
397
+ } catch {
398
+ return {};
399
+ }
400
+ }
401
+
402
+ // src/host/nextjs/app-client-only.tsx
403
+ var import_context3 = require("#internal/host/react/context");
404
+
405
+ // src/host/react/index.tsx
406
+ var import_react3 = require("react");
407
+ var import_react_dom = require("react-dom");
408
+ var import_context2 = require("#internal/host/react/context");
409
+
300
410
  // src/host/server/fetch-headers.ts
301
411
  function remoteFetchHeaders() {
302
412
  return {
@@ -521,29 +631,6 @@ function resolveNameFromSrc(src, defaultName) {
521
631
  return name || defaultName;
522
632
  }
523
633
 
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_BUNDLE_NAME = "__vercel_remote_bundle";
538
- var DEFAULT_COMPONENT_NAME = "__vercel_remote_component";
539
- var DEFAULT_ROUTE = "/";
540
- var RUNTIME_WEBPACK = "webpack";
541
- var RUNTIME_TURBOPACK = "turbopack";
542
- var RUNTIME_SCRIPT = "script";
543
- function getBundleKey(bundle) {
544
- return escapeString(bundle);
545
- }
546
-
547
634
  // src/runtime/html/apply-origin.ts
548
635
  var tagNames = [
549
636
  "img",
@@ -1018,14 +1105,6 @@ function createRSCStream(rscName, data) {
1018
1105
  });
1019
1106
  }
1020
1107
 
1021
- // src/runtime/patterns.ts
1022
- var REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
1023
- var NEXT_BUNDLE_PATH_RE = /\/_next\/\[.+\](?:%20| )/;
1024
- var DOUBLE_SLASH_RE = /(?<!:)\/\//g;
1025
- function collapseDoubleSlashes(path) {
1026
- return path.replace(DOUBLE_SLASH_RE, "/");
1027
- }
1028
-
1029
1108
  // src/runtime/turbopack/patterns.ts
1030
1109
  var REMOTE_SHARED_MARKER_RE = /(?:self|[a-z])\.TURBOPACK_REMOTE_SHARED/;
1031
1110
  var REMOTE_SHARED_ASSIGNMENT_RE = /\.TURBOPACK_REMOTE_SHARED=await (?:__turbopack_context__|[a-z])\.A\((?<sharedModuleId>[0-9]+)\)/;
@@ -1035,62 +1114,39 @@ var ASYNC_MODULE_ALL_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<vCb
1035
1114
  var TURBOPACK_GLOBAL_RE = /(?:globalThis|self)\s*(?:\.TURBOPACK|\[\s*["']TURBOPACK["']\s*\])/;
1036
1115
 
1037
1116
  // src/runtime/turbopack/chunk-loader.ts
1038
- function createChunkLoader(runtime, resolveClientUrl) {
1039
- return function __turbopack_chunk_load__(chunkId, scriptBundle) {
1040
- logDebug("ChunkLoader", `Loading chunk: "${chunkId}"`);
1041
- const self = globalThis;
1042
- const {
1043
- bundle,
1044
- id: path,
1045
- prefix
1046
- } = REMOTE_COMPONENT_REGEX.exec(chunkId)?.groups ?? {
1047
- bundle: scriptBundle ?? "",
1048
- id: chunkId
1049
- };
1050
- logDebug(
1051
- "ChunkLoader",
1052
- `Parsed chunk - bundle: "${bundle}", path: "${path}", prefix: "${prefix}"`
1053
- );
1054
- const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : runtime;
1055
- logDebug("ChunkLoader", `Remote runtime: "${remoteRuntime}"`);
1056
- if (remoteRuntime === RUNTIME_WEBPACK) {
1057
- logDebug("ChunkLoader", "Skipping chunk load - webpack runtime detected");
1058
- return Promise.resolve(void 0);
1059
- }
1060
- const url = new URL(
1061
- path ? `${prefix ?? ""}${path}`.replace(
1062
- /(?<char>[^:])(?<double>\/\/)/g,
1063
- "$1/"
1064
- ) : "/",
1065
- self.__remote_bundle_url__?.[bundle ?? "default"] ?? new URL(location.origin)
1066
- ).href;
1067
- if (url.endsWith(".css")) {
1068
- logDebug("ChunkLoader", `Skipping CSS file: "${url}"`);
1069
- return;
1070
- }
1071
- if (!self.__remote_components_turbopack_chunk_loader_promise__) {
1072
- self.__remote_components_turbopack_chunk_loader_promise__ = {};
1073
- }
1074
- if (self.__remote_components_turbopack_chunk_loader_promise__[url]) {
1075
- logDebug("ChunkLoader", `Returning cached promise for: "${url}"`);
1076
- return self.__remote_components_turbopack_chunk_loader_promise__[url];
1077
- }
1078
- const resolvedUrl = resolveClientUrl?.(url) ?? url;
1079
- logDebug("ChunkLoader", `Fetching chunk from: "${resolvedUrl}"`);
1080
- self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise((resolve, reject) => {
1117
+ function loadChunkWithScope(scope, chunkId) {
1118
+ logDebug(
1119
+ "ChunkLoader",
1120
+ `loadChunkWithScope: "${chunkId}" (scope: "${scope.scopedName}")`
1121
+ );
1122
+ const self = globalThis;
1123
+ const { bundle, path, prefix } = parseRemoteId(chunkId);
1124
+ const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : scope.runtime;
1125
+ if (remoteRuntime === RUNTIME_WEBPACK) {
1126
+ return Promise.resolve(void 0);
1127
+ }
1128
+ const rawPath = path ? collapseDoubleSlashes(`${prefix}${path}`) : "/";
1129
+ const url = new URL(rawPath, scope.url).href;
1130
+ if (url.endsWith(".css")) {
1131
+ return;
1132
+ }
1133
+ if (!self.__remote_components_turbopack_chunk_loader_promise__) {
1134
+ self.__remote_components_turbopack_chunk_loader_promise__ = {};
1135
+ }
1136
+ if (self.__remote_components_turbopack_chunk_loader_promise__[url]) {
1137
+ return self.__remote_components_turbopack_chunk_loader_promise__[url];
1138
+ }
1139
+ const resolvedUrl = scope.resolveClientUrl?.(url) ?? url;
1140
+ if (resolvedUrl !== url) {
1141
+ logDebug("ChunkLoader", `Proxied chunk URL: "${url}" \u2192 "${resolvedUrl}"`);
1142
+ }
1143
+ self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise(
1144
+ (resolve, reject) => {
1081
1145
  fetch(resolvedUrl).then((res) => res.text()).then((code) => {
1082
1146
  const hasTurbopack = TURBOPACK_GLOBAL_RE.test(code);
1083
1147
  if (hasTurbopack) {
1084
- return handleTurbopackChunk(code, bundle ?? "", url);
1148
+ return handleTurbopackChunk(code, scope, url);
1085
1149
  }
1086
- logDebug(
1087
- "ChunkLoader",
1088
- `Chunk does not contain globalThis.TURBOPACK or self.TURBOPACK: "${url}"`
1089
- );
1090
- logDebug(
1091
- "ChunkLoader",
1092
- `First 500 chars of chunk: ${code.slice(0, 500)}`
1093
- );
1094
1150
  }).then(resolve).catch((error) => {
1095
1151
  const isProxied = isProxiedUrl(resolvedUrl);
1096
1152
  if (isProxied) {
@@ -1100,17 +1156,29 @@ function createChunkLoader(runtime, resolveClientUrl) {
1100
1156
  reject(error);
1101
1157
  }
1102
1158
  });
1103
- });
1104
- return self.__remote_components_turbopack_chunk_loader_promise__[url];
1159
+ }
1160
+ );
1161
+ return self.__remote_components_turbopack_chunk_loader_promise__[url];
1162
+ }
1163
+ function createChunkDispatcher() {
1164
+ return function __chunk_dispatcher__(chunkId, scriptBundle) {
1165
+ logDebug("ChunkDispatcher", `Dispatching chunk: "${chunkId}"`);
1166
+ const { bundle } = parseRemoteId(chunkId);
1167
+ const bundleName = bundle || scriptBundle || "default";
1168
+ const scope = getScope(bundleName);
1169
+ logDebug(
1170
+ "ChunkDispatcher",
1171
+ `Scope resolution: bundle="${bundleName}", scope=${scope?.scopedName ?? "null"}`
1172
+ );
1173
+ if (!scope) {
1174
+ logWarn("ChunkDispatcher", `No scope found for bundle "${bundleName}"`);
1175
+ return Promise.resolve(void 0);
1176
+ }
1177
+ return loadChunkWithScope(scope, chunkId);
1105
1178
  };
1106
1179
  }
1107
- async function handleTurbopackChunk(code, bundle, url) {
1108
- logDebug(
1109
- "ChunkLoader",
1110
- `Handling Turbopack chunk - bundle: "${bundle}", url: "${url}"`
1111
- );
1180
+ async function handleTurbopackChunk(code, scope, url) {
1112
1181
  if (/importScripts\(\.\.\.self.TURBOPACK_NEXT_CHUNK_URLS/.test(code)) {
1113
- logDebug("ChunkLoader", `Skipping worker chunk: "${url}"`);
1114
1182
  const preloadLinks = document.querySelectorAll(
1115
1183
  `link[rel="preload"][href="${new URL(url).pathname}"]`
1116
1184
  );
@@ -1118,58 +1186,41 @@ async function handleTurbopackChunk(code, bundle, url) {
1118
1186
  return;
1119
1187
  }
1120
1188
  const self = globalThis;
1121
- const bundleKey = getBundleKey(bundle);
1122
- logDebug("ChunkLoader", `Bundle key: "${bundleKey}"`);
1189
+ const { globalKey } = scope;
1123
1190
  const transformedCode = code.replace(
1124
1191
  /globalThis\[\s*["']TURBOPACK["']\s*\]/g,
1125
- `globalThis["TURBOPACK_${bundleKey}"]`
1192
+ `globalThis["TURBOPACK_${globalKey}"]`
1126
1193
  ).replace(
1127
1194
  /self\[\s*["']TURBOPACK["']\s*\]/g,
1128
- `self["TURBOPACK_${bundleKey}"]`
1129
- ).replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${bundleKey}`).replace(/self\.TURBOPACK(?!_)/g, `self.TURBOPACK_${bundleKey}`).replace(
1195
+ `self["TURBOPACK_${globalKey}"]`
1196
+ ).replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${globalKey}`).replace(/self\.TURBOPACK(?!_)/g, `self.TURBOPACK_${globalKey}`).replace(
1130
1197
  /TURBOPACK_WORKER_LOCATION/g,
1131
- `TURBOPACK_WORKER_LOCATION_${bundleKey}`
1198
+ `TURBOPACK_WORKER_LOCATION_${globalKey}`
1132
1199
  ).replace(
1133
1200
  /TURBOPACK_NEXT_CHUNK_URLS/g,
1134
- `TURBOPACK_NEXT_CHUNK_URLS_${bundleKey}`
1201
+ `TURBOPACK_NEXT_CHUNK_URLS_${globalKey}`
1135
1202
  ).replace(
1136
1203
  /TURBOPACK_CHUNK_UPDATE_LISTENERS/g,
1137
- `TURBOPACK_CHUNK_UPDATE_LISTENERS_${bundleKey}`
1138
- ).replace(/__next_require__/g, `__${bundleKey}_next_require__`).replace(
1204
+ `TURBOPACK_CHUNK_UPDATE_LISTENERS_${globalKey}`
1205
+ ).replace(/__next_require__/g, `__${globalKey}_next_require__`).replace(
1139
1206
  /\/\/# sourceMappingURL=(?<name>.+)(?<optional>\._)?\.js\.map/g,
1140
- `//# sourceMappingURL=${new URL(
1141
- ".",
1142
- new URL(
1143
- url,
1144
- self.__remote_bundle_url__?.[bundle] ?? new URL(location.origin)
1145
- )
1146
- ).href}$1$2.js.map`
1207
+ `//# sourceMappingURL=${new URL(".", new URL(url, scope.url)).href}$1$2.js.map`
1147
1208
  );
1148
- if (!self[`TURBOPACK_${bundleKey}`]) {
1209
+ if (!self[`TURBOPACK_${globalKey}`]) {
1149
1210
  const chunkData = [];
1150
1211
  const turbopackObject = {
1151
1212
  push: (item) => {
1152
- logDebug(
1153
- "ChunkLoader",
1154
- `TURBOPACK_${bundleKey}.push() called with item type: ${Array.isArray(item) ? "array" : typeof item}`
1155
- );
1156
1213
  if (Array.isArray(item)) {
1157
1214
  chunkData.push(item);
1158
- logDebug(
1159
- "ChunkLoader",
1160
- `TURBOPACK_${bundleKey} now has ${chunkData.length} chunks`
1161
- );
1162
1215
  } else {
1163
1216
  chunkData.push([item]);
1164
1217
  }
1165
1218
  return chunkData.length;
1166
1219
  },
1167
- // Store chunks for later access
1168
1220
  __chunks__: chunkData
1169
1221
  };
1170
- self[`TURBOPACK_${bundleKey}`] = turbopackObject;
1222
+ self[`TURBOPACK_${globalKey}`] = turbopackObject;
1171
1223
  }
1172
- logDebug("ChunkLoader", `Creating blob script for: "${url}"`);
1173
1224
  await new Promise((scriptResolve, scriptReject) => {
1174
1225
  const blob = new Blob([transformedCode], {
1175
1226
  type: "application/javascript; charset=UTF-8"
@@ -1181,21 +1232,6 @@ async function handleTurbopackChunk(code, bundle, url) {
1181
1232
  script.async = true;
1182
1233
  script.onload = () => {
1183
1234
  URL.revokeObjectURL(scriptUrl);
1184
- logDebug(
1185
- "ChunkLoader",
1186
- `Script loaded successfully for bundle "${bundle}"`
1187
- );
1188
- const turbopackBundle = self[`TURBOPACK_${bundleKey}`];
1189
- logDebug(
1190
- "ChunkLoader",
1191
- `TURBOPACK_${bundleKey} type: ${typeof turbopackBundle}, isArray: ${Array.isArray(turbopackBundle)}, keys: ${turbopackBundle ? Object.keys(turbopackBundle).slice(0, 10).join(", ") : "none"}`
1192
- );
1193
- if (turbopackBundle && typeof turbopackBundle === "object") {
1194
- logDebug(
1195
- "ChunkLoader",
1196
- `TURBOPACK_${bundleKey} length/size: ${Array.isArray(turbopackBundle) ? turbopackBundle.length : Object.keys(turbopackBundle).length}`
1197
- );
1198
- }
1199
1235
  scriptResolve(void 0);
1200
1236
  script.remove();
1201
1237
  };
@@ -1210,93 +1246,203 @@ async function handleTurbopackChunk(code, bundle, url) {
1210
1246
  };
1211
1247
  document.head.appendChild(script);
1212
1248
  });
1213
- const chunkLists = self[`TURBOPACK_${bundleKey}_CHUNK_LISTS`];
1214
- logDebug(
1215
- "ChunkLoader",
1216
- `Processing chunk lists for bundle "${bundle}": ${chunkLists?.length ?? 0} lists`
1217
- );
1218
- const loadChunkLists = [];
1249
+ const chunkLists = self[`TURBOPACK_${globalKey}_CHUNK_LISTS`];
1250
+ const loadChunkPromises = [];
1219
1251
  while (chunkLists?.length) {
1220
1252
  const { chunks } = chunkLists.shift() ?? { chunks: [] };
1221
1253
  if (chunks.length > 0) {
1222
- logDebug(
1223
- "ChunkLoader",
1224
- `Loading ${chunks.length} additional chunks for bundle "${bundle}": [${chunks.join(", ")}]`
1225
- );
1226
- chunks.forEach((id) => {
1227
- const chunkLoadResult = self.__webpack_chunk_load__?.(
1228
- `[${bundle}] ${url.slice(0, url.indexOf("/_next"))}/_next/${id}`
1254
+ for (const id of chunks) {
1255
+ const baseUrl = url.slice(0, url.indexOf("/_next"));
1256
+ const chunkLoadResult = loadChunkWithScope(
1257
+ scope,
1258
+ formatRemoteId(scope, `${baseUrl}/_next/${id}`)
1229
1259
  );
1230
1260
  if (chunkLoadResult) {
1231
- loadChunkLists.push(chunkLoadResult);
1261
+ loadChunkPromises.push(chunkLoadResult);
1232
1262
  }
1233
- });
1263
+ }
1234
1264
  }
1235
1265
  }
1236
- if (loadChunkLists.length > 0) {
1237
- logDebug(
1238
- "ChunkLoader",
1239
- `Waiting for ${loadChunkLists.length} additional chunks to load`
1240
- );
1241
- await Promise.all(loadChunkLists);
1266
+ if (loadChunkPromises.length > 0) {
1267
+ await Promise.all(loadChunkPromises);
1242
1268
  }
1243
1269
  }
1244
1270
 
1245
- // src/runtime/turbopack/module.ts
1246
- function handleTurbopackModule(bundle, moduleId, id) {
1271
+ // src/runtime/turbopack/shared-modules.ts
1272
+ 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).";
1273
+ function getTurbopackModules(scope) {
1247
1274
  const self = globalThis;
1248
- const bundleKey = getBundleKey(bundle);
1249
- const raw = self[`TURBOPACK_${bundleKey}`];
1250
- let modules;
1251
- if (raw && typeof raw === "object" && "__chunks__" in raw) {
1252
- modules = raw.__chunks__.flat();
1253
- } else if (Array.isArray(raw)) {
1254
- modules = raw.flat();
1275
+ const raw = self[`TURBOPACK_${scope.globalKey}`];
1276
+ if (!raw)
1277
+ return void 0;
1278
+ if (typeof raw === "object" && "__chunks__" in raw) {
1279
+ return raw.__chunks__.flat();
1280
+ }
1281
+ if (Array.isArray(raw)) {
1282
+ return raw.flat();
1283
+ }
1284
+ return Object.entries(raw).flat();
1285
+ }
1286
+ async function initializeSharedModules(scope, hostShared = {}, remoteShared = {}) {
1287
+ const allModules = getTurbopackModules(scope);
1288
+ logDebug(
1289
+ "SharedModules",
1290
+ `initializeSharedModules: scope="${scope.scopedName}", allModules=${allModules ? allModules.length : "null"}, hostShared=[${Object.keys(hostShared).join(", ")}], remoteShared=${JSON.stringify(remoteShared)}`
1291
+ );
1292
+ let sharedModuleInitializer = null;
1293
+ if (allModules) {
1294
+ const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {
1295
+ if (typeof idOrFunc !== "function") {
1296
+ return false;
1297
+ }
1298
+ const funcCode = idOrFunc.toString();
1299
+ return REMOTE_SHARED_MARKER_RE.test(funcCode);
1300
+ });
1301
+ if (sharedModuleInitializerIndex > 0) {
1302
+ const sharedModuleInitializerCode = allModules[sharedModuleInitializerIndex].toString();
1303
+ const sharedModuleInitializerId = allModules[sharedModuleInitializerIndex - 1];
1304
+ const { sharedModuleId } = REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ?? {};
1305
+ if (sharedModuleId) {
1306
+ const { default: sharedModuleInitializerInstance } = handleTurbopackModule(
1307
+ scope,
1308
+ sharedModuleId,
1309
+ formatRemoteId(scope, String(sharedModuleInitializerId))
1310
+ );
1311
+ sharedModuleInitializer = sharedModuleInitializerInstance;
1312
+ }
1313
+ }
1314
+ if (sharedModuleInitializer) {
1315
+ const { shared: shared2 } = await sharedModuleInitializer;
1316
+ const sharedModuleIds = extractSharedModuleIds(shared2, scope);
1317
+ logDebug(
1318
+ "SharedModules",
1319
+ `Resolved shared modules for scope="${scope.scopedName}": ${JSON.stringify(sharedModuleIds)}`
1320
+ );
1321
+ return Promise.all(
1322
+ Object.entries(sharedModuleIds).map(async ([id, module2]) => {
1323
+ if (hostShared[module2]) {
1324
+ scope.sharedModules[id] = await hostShared[module2](scope.name);
1325
+ } else {
1326
+ logError(
1327
+ "SharedModules",
1328
+ `Host shared module "${module2}" not found for ID ${id}. ${DEDUPLICATION_WARNING}`
1329
+ );
1330
+ }
1331
+ })
1332
+ );
1333
+ }
1334
+ logWarn(
1335
+ "SharedModules",
1336
+ `No shared module initializer found in bundle for scope="${scope.scopedName}" \u2014 falling back to remoteShared mapping`
1337
+ );
1255
1338
  } else {
1256
- modules = raw;
1339
+ logWarn(
1340
+ "SharedModules",
1341
+ `No TURBOPACK modules found for scope="${scope.scopedName}" (TURBOPACK_${scope.globalKey} is empty)`
1342
+ );
1257
1343
  }
1258
- if (!self.__remote_components_turbopack_modules__) {
1259
- self.__remote_components_turbopack_modules__ = {};
1344
+ return Promise.all(
1345
+ Object.entries(remoteShared).map(async ([id, module2]) => {
1346
+ if (hostShared[module2]) {
1347
+ const normalizedId = id.replace("[app-ssr]", "[app-client]");
1348
+ scope.sharedModules[normalizedId] = await hostShared[module2](
1349
+ scope.name
1350
+ );
1351
+ } else {
1352
+ logError(
1353
+ "SharedModules",
1354
+ `Shared module "${module2}" not found for "${scope.name}". ${DEDUPLICATION_WARNING}`
1355
+ );
1356
+ }
1357
+ })
1358
+ );
1359
+ }
1360
+ function extractSharedModuleIds(shared2, scope) {
1361
+ return Object.entries(shared2).filter(([, value]) => typeof value === "function").reduce((acc, [key, value]) => {
1362
+ const { asyncSharedModuleId } = ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};
1363
+ if (asyncSharedModuleId) {
1364
+ const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);
1365
+ let asyncSharedModule;
1366
+ const turbopackModules = getTurbopackModules(scope) ?? [];
1367
+ const asyncSharedModuleIdIndex = turbopackModules.indexOf(
1368
+ asyncSharedModuleIdNumber
1369
+ );
1370
+ if (asyncSharedModuleIdIndex !== -1 && typeof turbopackModules[asyncSharedModuleIdIndex + 1] === "function") {
1371
+ asyncSharedModule = turbopackModules[asyncSharedModuleIdIndex + 1];
1372
+ }
1373
+ if (asyncSharedModule) {
1374
+ const asyncSharedModuleCode = asyncSharedModule.toString();
1375
+ const { sharedModuleId } = ASYNC_MODULE_RESOLVE_RE.exec(asyncSharedModuleCode)?.groups ?? ASYNC_MODULE_ALL_RE.exec(asyncSharedModuleCode)?.groups ?? {};
1376
+ acc[sharedModuleId ?? asyncSharedModuleId] = key.replace(
1377
+ "__remote_shared_module_",
1378
+ ""
1379
+ );
1380
+ }
1381
+ }
1382
+ return acc;
1383
+ }, {});
1384
+ }
1385
+ function getSharedModule(scope, id) {
1386
+ const idStr = String(id);
1387
+ if (scope.sharedModules[idStr] !== void 0) {
1388
+ return scope.sharedModules[idStr];
1260
1389
  }
1261
- if (!self.__remote_components_turbopack_modules__[bundle]) {
1262
- self.__remote_components_turbopack_modules__[bundle] = {};
1390
+ for (const [key, value] of Object.entries(scope.sharedModules)) {
1391
+ if (typeof value !== "undefined" && idStr !== key && idStr.endsWith(key)) {
1392
+ return value;
1393
+ }
1263
1394
  }
1264
- if (self.__remote_components_turbopack_modules__[bundle][moduleId]) {
1265
- return self.__remote_components_turbopack_modules__[bundle][moduleId];
1395
+ return null;
1396
+ }
1397
+
1398
+ // src/runtime/turbopack/module.ts
1399
+ function requireModule(scope, moduleId, fullId) {
1400
+ const idStr = String(moduleId);
1401
+ if (scope.moduleCache[idStr])
1402
+ return scope.moduleCache[idStr];
1403
+ const sharedModule = getSharedModule(scope, moduleId);
1404
+ if (sharedModule)
1405
+ return sharedModule;
1406
+ return handleTurbopackModule(
1407
+ scope,
1408
+ idStr,
1409
+ fullId ?? formatRemoteId(scope, idStr)
1410
+ );
1411
+ }
1412
+ function handleTurbopackModule(scope, moduleId, id) {
1413
+ if (scope.moduleCache[moduleId]) {
1414
+ return scope.moduleCache[moduleId];
1266
1415
  }
1416
+ const modules = getTurbopackModules(scope);
1267
1417
  if (!modules) {
1268
- logError("TurbopackModule", `TURBOPACK_${bundleKey} is undefined`);
1418
+ logError(
1419
+ "TurbopackModule",
1420
+ `TURBOPACK_${scope.globalKey} is undefined (scope: "${scope.scopedName}")`
1421
+ );
1269
1422
  }
1270
1423
  const moduleInit = findModuleInit(modules, moduleId);
1271
1424
  const exports = {};
1272
1425
  const moduleExports = { exports };
1273
1426
  if (typeof moduleInit !== "function") {
1274
1427
  throw new Error(
1275
- `Module ${id} not found in bundle ${bundle} with id ${moduleId}`
1428
+ `Module ${id} not found in bundle ${scope.name} with id ${moduleId}`
1276
1429
  );
1277
1430
  }
1278
- self.__remote_components_turbopack_modules__[bundle][moduleId] = moduleExports.exports;
1279
- if (!self.__remote_components_turbopack_global__) {
1280
- self.__remote_components_turbopack_global__ = {};
1281
- }
1282
- if (!self.__remote_components_turbopack_global__[bundle]) {
1283
- self.__remote_components_turbopack_global__[bundle] = {};
1284
- }
1431
+ scope.moduleCache[moduleId] = moduleExports.exports;
1285
1432
  moduleInit(
1286
1433
  createTurbopackContext(
1287
- bundle,
1434
+ scope,
1288
1435
  exports,
1289
1436
  moduleExports,
1290
1437
  modules,
1291
1438
  moduleInit,
1292
- id,
1293
- self
1439
+ id
1294
1440
  ),
1295
1441
  moduleExports,
1296
1442
  exports
1297
1443
  );
1298
- if (self.__remote_components_turbopack_modules__[bundle][moduleId] !== moduleExports.exports) {
1299
- self.__remote_components_turbopack_modules__[bundle][moduleId] = moduleExports.exports;
1444
+ if (scope.moduleCache[moduleId] !== moduleExports.exports) {
1445
+ scope.moduleCache[moduleId] = moduleExports.exports;
1300
1446
  }
1301
1447
  return moduleExports.exports;
1302
1448
  }
@@ -1321,7 +1467,8 @@ function findModuleInit(modules, moduleId) {
1321
1467
  (e) => Boolean(e && typeof e === "object" && moduleId in e)
1322
1468
  )?.[moduleId];
1323
1469
  }
1324
- function createTurbopackContext(bundle, exports, moduleExports, modules, moduleInit, id, self) {
1470
+ function createTurbopackContext(scope, exports, moduleExports, modules, moduleInit, id) {
1471
+ const scopedRequire = (moduleId) => requireModule(scope, moduleId, formatRemoteId(scope, String(moduleId)));
1325
1472
  return {
1326
1473
  // HMR not implemented for Remote Components
1327
1474
  k: {
@@ -1337,16 +1484,10 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1337
1484
  s(bindings, esmId) {
1338
1485
  let mod = exports;
1339
1486
  if (typeof esmId === "string" || typeof esmId === "number") {
1340
- if (!self.__remote_components_turbopack_modules__) {
1341
- self.__remote_components_turbopack_modules__ = {};
1342
- }
1343
- if (!self.__remote_components_turbopack_modules__[bundle]) {
1344
- self.__remote_components_turbopack_modules__[bundle] = {};
1487
+ if (!scope.moduleCache[esmId]) {
1488
+ scope.moduleCache[esmId] = {};
1345
1489
  }
1346
- if (!self.__remote_components_turbopack_modules__[bundle][esmId]) {
1347
- self.__remote_components_turbopack_modules__[bundle][esmId] = {};
1348
- }
1349
- mod = self.__remote_components_turbopack_modules__[bundle][esmId];
1490
+ mod = scope.moduleCache[esmId];
1350
1491
  }
1351
1492
  Object.defineProperty(mod, "__esModule", { value: true });
1352
1493
  if (Array.isArray(bindings)) {
@@ -1379,7 +1520,7 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1379
1520
  }
1380
1521
  }
1381
1522
  },
1382
- // import
1523
+ // import — resolves directly via scope, no global dispatch
1383
1524
  i(importId) {
1384
1525
  let mod;
1385
1526
  if (typeof importId === "string") {
@@ -1390,7 +1531,7 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1390
1531
  /\s+<export(?<specifier>.*)>$/,
1391
1532
  ""
1392
1533
  );
1393
- mod = self.__webpack_require__?.(`[${bundle}] ${normalizedId}`);
1534
+ mod = scopedRequire(normalizedId);
1394
1535
  if (mod && exportSource && exportName && (exportSource === "*" || typeof mod[exportSource] !== "undefined") && typeof mod[exportName] === "undefined") {
1395
1536
  if (exportSource === "*") {
1396
1537
  mod[exportName] = mod;
@@ -1399,7 +1540,7 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1399
1540
  }
1400
1541
  }
1401
1542
  } else {
1402
- mod = self.__webpack_require__?.(`[${bundle}] ${importId}`);
1543
+ mod = scopedRequire(importId);
1403
1544
  }
1404
1545
  if (typeof mod !== "object" || mod === null) {
1405
1546
  mod = { default: mod };
@@ -1411,16 +1552,14 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1411
1552
  }
1412
1553
  return mod;
1413
1554
  },
1414
- // require
1555
+ // require — resolves directly via scope
1415
1556
  r(requireId) {
1416
- return self.__webpack_require__?.(`[${bundle}] ${requireId}`);
1557
+ return scopedRequire(requireId);
1417
1558
  },
1418
1559
  // value exports
1419
1560
  v(value) {
1420
1561
  if (typeof value === "function") {
1421
- exports.default = value((vid) => {
1422
- return self.__webpack_require__?.(`[${bundle}] ${vid}`);
1423
- });
1562
+ exports.default = value((vid) => scopedRequire(vid));
1424
1563
  } else {
1425
1564
  moduleExports.exports = value;
1426
1565
  }
@@ -1435,17 +1574,15 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1435
1574
  );
1436
1575
  exports.default = result;
1437
1576
  },
1438
- // async module loader
1577
+ // async module loader — resolves directly via scope
1439
1578
  async A(Aid) {
1440
- const mod = self.__webpack_require__?.(`[${bundle}] ${Aid}`);
1441
- return mod.default(
1442
- (parentId) => self.__webpack_require__?.(`[${bundle}] ${parentId}`)
1443
- );
1579
+ const mod = scopedRequire(Aid);
1580
+ return mod.default((parentId) => scopedRequire(parentId));
1444
1581
  },
1445
1582
  // dynamic import tracking — no-op for remote components
1446
1583
  j() {
1447
1584
  },
1448
- // chunk loader
1585
+ // chunk loader — loads directly via scope, no global dispatch
1449
1586
  l(url) {
1450
1587
  const flatModules = Array.isArray(modules) ? modules : [];
1451
1588
  const moduleInitIndex = flatModules.indexOf(moduleInit);
@@ -1456,169 +1593,57 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1456
1593
  const scriptSrc = script.getAttribute("data-turbopack-src") || "";
1457
1594
  const nextIndex = scriptSrc.indexOf("/_next");
1458
1595
  const baseUrl = nextIndex !== -1 ? scriptSrc.slice(0, nextIndex) : "";
1459
- const bundleUrl = `[${bundle}] ${baseUrl}/_next/${url}`;
1460
- return self.__webpack_chunk_load__?.(bundleUrl, bundle);
1596
+ const chunkUrl = `${baseUrl}/_next/${url}`;
1597
+ return loadChunkWithScope(scope, formatRemoteId(scope, chunkUrl));
1461
1598
  }
1462
1599
  }
1463
1600
  throw new Error(
1464
1601
  `Failed to load Turbopack chunk "${url}" for module "${id}". Check the URL is correct.`
1465
1602
  );
1466
1603
  },
1467
- // global object for this bundle
1468
- g: self.__remote_components_turbopack_global__?.[bundle],
1604
+ // globalThis substitute shared across all modules in this scope
1605
+ g: scope.moduleGlobal,
1469
1606
  m: moduleExports,
1470
1607
  e: exports
1471
1608
  };
1472
1609
  }
1473
1610
 
1474
- // src/runtime/turbopack/shared-modules.ts
1475
- 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).";
1476
- async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {}) {
1477
- const self = globalThis;
1478
- self.__remote_shared_modules__ = self.__remote_shared_modules__ ?? {};
1479
- if (!self.__remote_shared_modules__[bundle]) {
1480
- self.__remote_shared_modules__[bundle] = {};
1481
- }
1482
- const bundleKey = getBundleKey(bundle);
1483
- let modules = self[`TURBOPACK_${bundleKey}`];
1484
- if (modules && typeof modules === "object" && "__chunks__" in modules) {
1485
- const chunks = modules.__chunks__;
1486
- modules = chunks.flat();
1487
- }
1488
- let sharedModuleInitializer = null;
1489
- if (modules) {
1490
- const allModules = Array.isArray(modules) ? modules.flat() : Object.entries(modules).flat();
1491
- const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {
1492
- if (typeof idOrFunc !== "function") {
1493
- return false;
1494
- }
1495
- const funcCode = idOrFunc.toString();
1496
- return REMOTE_SHARED_MARKER_RE.test(funcCode);
1497
- });
1498
- if (sharedModuleInitializerIndex > 0) {
1499
- const sharedModuleInitializerCode = allModules[sharedModuleInitializerIndex].toString();
1500
- const sharedModuleInitializerId = allModules[sharedModuleInitializerIndex - 1];
1501
- const { sharedModuleId } = REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ?? {};
1502
- if (sharedModuleId) {
1503
- const { default: sharedModuleInitializerInstance } = handleTurbopackModule(
1504
- bundle,
1505
- sharedModuleId,
1506
- `[${bundle}] ${sharedModuleInitializerId}`
1507
- );
1508
- sharedModuleInitializer = sharedModuleInitializerInstance;
1509
- }
1510
- }
1511
- if (sharedModuleInitializer) {
1512
- const { shared: shared2 } = await sharedModuleInitializer;
1513
- const sharedModuleIds = extractSharedModuleIds(
1514
- shared2,
1515
- bundleKey,
1516
- self
1517
- );
1518
- return Promise.all(
1519
- Object.entries(sharedModuleIds).map(async ([id, module2]) => {
1520
- if (self.__remote_shared_modules__?.[bundle]) {
1521
- if (hostShared[module2]) {
1522
- self.__remote_shared_modules__[bundle][id] = await hostShared[module2](bundle);
1523
- } else {
1524
- logError(
1525
- "SharedModules",
1526
- `Host shared module "${module2}" not found for ID ${id}. ${DEDUPLICATION_WARNING}`
1527
- );
1528
- }
1529
- }
1530
- })
1531
- );
1532
- }
1533
- }
1534
- return Promise.all(
1535
- Object.entries(remoteShared).map(async ([id, module2]) => {
1536
- if (self.__remote_shared_modules__?.[bundle]) {
1537
- if (hostShared[module2]) {
1538
- const normalizedId = id.replace("[app-ssr]", "[app-client]");
1539
- self.__remote_shared_modules__[bundle][normalizedId] = await hostShared[module2](bundle);
1540
- } else {
1541
- logError(
1542
- "SharedModules",
1543
- `Shared module "${module2}" not found for "${bundle}". ${DEDUPLICATION_WARNING}`
1544
- );
1545
- }
1546
- }
1547
- })
1548
- );
1549
- }
1550
- function extractSharedModuleIds(shared2, bundleKey, self) {
1551
- return Object.entries(shared2).filter(([, value]) => typeof value === "function").reduce((acc, [key, value]) => {
1552
- const { asyncSharedModuleId } = ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};
1553
- if (asyncSharedModuleId) {
1554
- const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);
1555
- let asyncSharedModule;
1556
- let turbopackModules = self[`TURBOPACK_${bundleKey}`];
1557
- if (turbopackModules && typeof turbopackModules === "object" && "__chunks__" in turbopackModules) {
1558
- const chunks = turbopackModules.__chunks__;
1559
- turbopackModules = chunks.flat();
1560
- }
1561
- const newAllModules = Array.isArray(turbopackModules) ? turbopackModules.flat() : turbopackModules ? Object.entries(turbopackModules).flatMap(([key2, value2]) => [
1562
- key2,
1563
- value2
1564
- ]) : [];
1565
- const asyncSharedModuleIdIndex = newAllModules.indexOf(
1566
- asyncSharedModuleIdNumber
1567
- );
1568
- if (asyncSharedModuleIdIndex !== -1 && typeof newAllModules[asyncSharedModuleIdIndex + 1] === "function") {
1569
- asyncSharedModule = newAllModules[asyncSharedModuleIdIndex + 1];
1570
- }
1571
- if (asyncSharedModule) {
1572
- const asyncSharedModuleCode = asyncSharedModule.toString();
1573
- const { sharedModuleId } = ASYNC_MODULE_RESOLVE_RE.exec(asyncSharedModuleCode)?.groups ?? ASYNC_MODULE_ALL_RE.exec(asyncSharedModuleCode)?.groups ?? {};
1574
- acc[sharedModuleId ?? asyncSharedModuleId] = key.replace(
1575
- "__remote_shared_module_",
1576
- ""
1577
- );
1578
- }
1579
- }
1580
- return acc;
1581
- }, {});
1582
- }
1583
- function getSharedModule(bundle, id) {
1584
- const self = globalThis;
1585
- for (const [key, value] of Object.entries(
1586
- self.__remote_shared_modules__?.[bundle] ?? {}
1587
- )) {
1588
- if (typeof value !== "undefined" && (typeof id === "string" && id.includes(key) || id === key)) {
1589
- return value;
1590
- }
1591
- }
1592
- return null;
1593
- }
1594
-
1595
1611
  // src/runtime/turbopack/webpack-runtime.ts
1596
1612
  async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location.href), bundle, shared2 = {}, remoteShared = {}, resolveClientUrl) {
1597
1613
  const self = globalThis;
1614
+ const bundleName = bundle ?? "default";
1615
+ const scope = createScope(bundleName, url, runtime, resolveClientUrl);
1616
+ registerScope(scope);
1598
1617
  if (!self.__remote_bundle_url__) {
1599
1618
  self.__remote_bundle_url__ = {};
1600
1619
  }
1601
- self.__remote_bundle_url__[bundle ?? "default"] = url;
1620
+ self.__remote_bundle_url__[bundleName] = url;
1621
+ if (scope.scopedName !== bundleName) {
1622
+ self.__remote_bundle_url__[scope.scopedName] = url;
1623
+ }
1602
1624
  self.__webpack_get_script_filename__ = () => null;
1603
- if (typeof self.__webpack_require__ !== "function" || self.__webpack_require_type__ !== "turbopack") {
1625
+ const willCreateDispatchers = typeof self.__webpack_require__ !== "function" || self.__webpack_require_type__ !== "turbopack";
1626
+ if (willCreateDispatchers) {
1604
1627
  if (!self.__original_webpack_require__ && !self.__original_webpack_chunk_load__) {
1605
1628
  self.__original_webpack_chunk_load__ = self.__webpack_chunk_load__;
1606
1629
  self.__original_webpack_require__ = self.__webpack_require__;
1607
1630
  }
1608
- self.__webpack_chunk_load__ = createChunkLoader(runtime, resolveClientUrl);
1609
- self.__webpack_require__ = createModuleRequire(runtime);
1631
+ self.__webpack_chunk_load__ = createChunkDispatcher();
1632
+ self.__webpack_require__ = createModuleDispatcher(runtime);
1610
1633
  self.__webpack_require_type__ = runtime;
1611
1634
  if (self.__remote_webpack_require__ && runtime === RUNTIME_TURBOPACK) {
1612
- const remoteBundle = bundle ?? "default";
1613
- self.__remote_webpack_require__[remoteBundle] = self.__webpack_require__;
1614
- self.__remote_webpack_require__[remoteBundle].type = "turbopack";
1635
+ self.__remote_webpack_require__[bundleName] = self.__webpack_require__;
1636
+ self.__remote_webpack_require__[bundleName].type = "turbopack";
1615
1637
  }
1616
1638
  }
1639
+ if (self.__remote_webpack_require__?.[bundleName] && scope.scopedName !== bundleName) {
1640
+ self.__remote_webpack_require__[scope.scopedName] = self.__remote_webpack_require__[bundleName];
1641
+ }
1617
1642
  if (runtime === RUNTIME_TURBOPACK) {
1618
1643
  const results = await Promise.allSettled(
1619
1644
  scripts.map((script) => {
1620
1645
  if (script.src) {
1621
- return self.__webpack_chunk_load__?.(script.src, bundle);
1646
+ return loadChunkWithScope(scope, script.src);
1622
1647
  }
1623
1648
  return Promise.resolve(void 0);
1624
1649
  })
@@ -1640,54 +1665,54 @@ async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location
1640
1665
  "react-dom/client": async () => (await import("react-dom/client")).default,
1641
1666
  ...shared2
1642
1667
  };
1643
- await initializeSharedModules(
1644
- bundle ?? "default",
1645
- // include all core modules as shared
1646
- coreShared,
1647
- remoteShared
1648
- );
1668
+ await initializeSharedModules(scope, coreShared, remoteShared);
1649
1669
  }
1650
- function createModuleRequire(runtime) {
1670
+ function createModuleDispatcher(runtime) {
1651
1671
  return (id) => {
1652
1672
  const self = globalThis;
1653
1673
  const { bundle, id: moduleId } = id.match(REMOTE_COMPONENT_REGEX)?.groups ?? {
1654
1674
  bundle: "default",
1655
1675
  id
1656
1676
  };
1657
- const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : runtime;
1658
- logDebug("WebpackRuntime", `remoteRuntime: "${remoteRuntime}"`);
1677
+ const bundleName = bundle ?? "default";
1678
+ const remoteRuntime = self.__remote_webpack_require__?.[bundleName] ? self.__remote_webpack_require__[bundleName]?.type || "webpack" : runtime;
1679
+ logDebug(
1680
+ "ModuleDispatcher",
1681
+ `Resolving "${id}" (bundle: "${bundleName}", runtime: "${remoteRuntime}")`
1682
+ );
1659
1683
  try {
1660
1684
  if (remoteRuntime === RUNTIME_WEBPACK && bundle && moduleId) {
1661
1685
  return self.__remote_webpack_require__?.[bundle]?.(moduleId);
1662
1686
  }
1663
- const sharedModuleId = moduleId ?? id;
1664
- const sharedModule = getSharedModule(bundle ?? "default", sharedModuleId);
1665
- if (sharedModule) {
1666
- return sharedModule;
1667
- }
1668
- if (bundle && moduleId) {
1669
- return handleTurbopackModule(bundle, moduleId, id);
1687
+ const scope = getScope(bundleName);
1688
+ if (scope) {
1689
+ return requireModule(scope, moduleId ?? id, id);
1670
1690
  }
1671
- throw new Error(`Module "${id}" not found.`);
1691
+ throw new Error(
1692
+ `Module "${id}" not found \u2014 no scope for bundle "${bundleName}".`
1693
+ );
1672
1694
  } catch (requireError) {
1673
1695
  logWarn(
1674
- "WebpackRuntime",
1696
+ "ModuleDispatcher",
1675
1697
  `Module require failed: ${String(requireError)}`
1676
1698
  );
1677
1699
  if (typeof self.__original_webpack_require__ !== "function") {
1678
1700
  throw new RemoteComponentsError(
1679
- `Module "${id}" not found in remote component bundle "${bundle}".`,
1701
+ `Module "${id}" not found in remote component bundle "${bundleName}".`,
1680
1702
  {
1681
1703
  cause: requireError instanceof Error ? requireError : void 0
1682
1704
  }
1683
1705
  );
1684
1706
  }
1685
1707
  try {
1686
- logDebug("WebpackRuntime", "Falling back to original webpack require");
1708
+ logDebug(
1709
+ "ModuleDispatcher",
1710
+ "Falling back to original webpack require"
1711
+ );
1687
1712
  return self.__original_webpack_require__(id);
1688
1713
  } catch (originalError) {
1689
1714
  throw new RemoteComponentsError(
1690
- `Module "${id}" not found in remote component bundle "${bundle}".`,
1715
+ `Module "${id}" not found in remote component bundle "${bundleName}".`,
1691
1716
  { cause: originalError instanceof Error ? originalError : void 0 }
1692
1717
  );
1693
1718
  }
@@ -1706,6 +1731,13 @@ async function loadScripts(scripts, resolveClientUrl) {
1706
1731
  location.origin
1707
1732
  ).href;
1708
1733
  const resolvedSrc = resolveClientUrl?.(newSrc) ?? newSrc;
1734
+ const alreadyLoaded = Array.from(
1735
+ document.querySelectorAll("script[src]")
1736
+ ).some((s) => s.src === resolvedSrc);
1737
+ if (alreadyLoaded) {
1738
+ resolve();
1739
+ return;
1740
+ }
1709
1741
  const newScript = document.createElement("script");
1710
1742
  newScript.onload = () => resolve();
1711
1743
  newScript.onerror = () => {
@@ -2690,14 +2722,6 @@ var routerImpl = async () => {
2690
2722
 
2691
2723
  // src/host/nextjs/app-client-only.tsx
2692
2724
  var import_jsx_runtime3 = require("react/jsx-runtime");
2693
- async function tryImportShared() {
2694
- try {
2695
- const { shared: shared2 } = await Promise.resolve().then(() => (init_app(), app_exports));
2696
- return shared2;
2697
- } catch {
2698
- return {};
2699
- }
2700
- }
2701
2725
  var sharedModules = async (userShared, resolveClientUrl) => {
2702
2726
  const [resolvedShared, resolvedUserShared] = await Promise.all([
2703
2727
  tryImportShared(),