remote-components 0.0.49 → 0.0.51

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 (71) hide show
  1. package/dist/html/host.cjs +348 -176
  2. package/dist/html/host.cjs.map +1 -1
  3. package/dist/html/host.js +348 -176
  4. package/dist/html/host.js.map +1 -1
  5. package/dist/html/remote.cjs +114 -20
  6. package/dist/html/remote.cjs.map +1 -1
  7. package/dist/html/remote.js +114 -20
  8. package/dist/html/remote.js.map +1 -1
  9. package/dist/internal/shared/{ssr → client}/fetch-with-protected-rc-fallback.cjs +9 -6
  10. package/dist/internal/shared/client/fetch-with-protected-rc-fallback.cjs.map +1 -0
  11. package/dist/internal/shared/{ssr → client}/fetch-with-protected-rc-fallback.d.ts +1 -2
  12. package/dist/internal/shared/{ssr → client}/fetch-with-protected-rc-fallback.js +10 -6
  13. package/dist/internal/shared/client/fetch-with-protected-rc-fallback.js.map +1 -0
  14. package/dist/internal/shared/client/protected-rc-fallback.cjs +32 -0
  15. package/dist/internal/shared/client/protected-rc-fallback.cjs.map +1 -0
  16. package/dist/internal/shared/client/protected-rc-fallback.d.ts +6 -0
  17. package/dist/internal/shared/client/protected-rc-fallback.js +8 -0
  18. package/dist/internal/shared/client/protected-rc-fallback.js.map +1 -0
  19. package/dist/internal/shared/client/remote-component.cjs +205 -108
  20. package/dist/internal/shared/client/remote-component.cjs.map +1 -1
  21. package/dist/internal/shared/client/remote-component.js +205 -108
  22. package/dist/internal/shared/client/remote-component.js.map +1 -1
  23. package/dist/internal/shared/constants.cjs +29 -0
  24. package/dist/internal/shared/constants.cjs.map +1 -0
  25. package/dist/internal/shared/constants.d.ts +3 -0
  26. package/dist/internal/shared/constants.js +5 -0
  27. package/dist/internal/shared/constants.js.map +1 -0
  28. package/dist/internal/shared/ssr/fetch-remote-component.cjs +1 -2
  29. package/dist/internal/shared/ssr/fetch-remote-component.cjs.map +1 -1
  30. package/dist/internal/shared/ssr/fetch-remote-component.js +1 -2
  31. package/dist/internal/shared/ssr/fetch-remote-component.js.map +1 -1
  32. package/dist/internal/shared/ssr/fetch-with-hooks.cjs +1 -1
  33. package/dist/internal/shared/ssr/fetch-with-hooks.cjs.map +1 -1
  34. package/dist/internal/shared/ssr/fetch-with-hooks.js +1 -1
  35. package/dist/internal/shared/ssr/fetch-with-hooks.js.map +1 -1
  36. package/dist/internal/shared/utils/logger.cjs.map +1 -1
  37. package/dist/internal/shared/utils/logger.d.ts +1 -1
  38. package/dist/internal/shared/utils/logger.js.map +1 -1
  39. package/dist/next/config.cjs.map +1 -1
  40. package/dist/next/config.js.map +1 -1
  41. package/dist/next/host/client/index.cjs +217 -115
  42. package/dist/next/host/client/index.cjs.map +1 -1
  43. package/dist/next/host/client/index.js +217 -115
  44. package/dist/next/host/client/index.js.map +1 -1
  45. package/dist/next/host/pages-router-server.cjs +1 -2
  46. package/dist/next/host/pages-router-server.cjs.map +1 -1
  47. package/dist/next/host/pages-router-server.d.ts +5 -0
  48. package/dist/next/host/pages-router-server.js +1 -2
  49. package/dist/next/host/pages-router-server.js.map +1 -1
  50. package/dist/next/proxy.cjs +36 -24
  51. package/dist/next/proxy.cjs.map +1 -1
  52. package/dist/next/proxy.d.ts +0 -1
  53. package/dist/next/proxy.js +36 -24
  54. package/dist/next/proxy.js.map +1 -1
  55. package/dist/react/index.cjs +217 -115
  56. package/dist/react/index.cjs.map +1 -1
  57. package/dist/react/index.js +217 -115
  58. package/dist/react/index.js.map +1 -1
  59. package/dist/shared/host/proxy.cjs +23 -20
  60. package/dist/shared/host/proxy.cjs.map +1 -1
  61. package/dist/shared/host/proxy.d.ts +0 -3
  62. package/dist/shared/host/proxy.js +23 -19
  63. package/dist/shared/host/proxy.js.map +1 -1
  64. package/dist/shared/remote/proxy.cjs +9 -1
  65. package/dist/shared/remote/proxy.cjs.map +1 -1
  66. package/dist/shared/remote/proxy.d.ts +7 -1
  67. package/dist/shared/remote/proxy.js +8 -1
  68. package/dist/shared/remote/proxy.js.map +1 -1
  69. package/package.json +1 -1
  70. package/dist/internal/shared/ssr/fetch-with-protected-rc-fallback.cjs.map +0 -1
  71. package/dist/internal/shared/ssr/fetch-with-protected-rc-fallback.js.map +0 -1
@@ -270,6 +270,121 @@ var init_shared_modules = __esm({
270
270
  }
271
271
  });
272
272
 
273
+ // src/shared/constants.ts
274
+ var RC_PROTECTED_REMOTE_FETCH_PATHNAME;
275
+ var init_constants = __esm({
276
+ "src/shared/constants.ts"() {
277
+ "use strict";
278
+ RC_PROTECTED_REMOTE_FETCH_PATHNAME = "/rc-fetch-protected-remote";
279
+ }
280
+ });
281
+
282
+ // src/shared/client/protected-rc-fallback.ts
283
+ function generateProtectedRcFallbackSrc(url) {
284
+ return `${RC_PROTECTED_REMOTE_FETCH_PATHNAME}?url=${encodeURIComponent(url)}`;
285
+ }
286
+ var init_protected_rc_fallback = __esm({
287
+ "src/shared/client/protected-rc-fallback.ts"() {
288
+ "use strict";
289
+ init_constants();
290
+ }
291
+ });
292
+
293
+ // src/shared/client/webpack-patterns.ts
294
+ var NEXT_BUNDLE_PATH_RE;
295
+ var init_webpack_patterns = __esm({
296
+ "src/shared/client/webpack-patterns.ts"() {
297
+ "use strict";
298
+ NEXT_BUNDLE_PATH_RE = /\/_next\/\[.+\](?:%20| )/;
299
+ }
300
+ });
301
+
302
+ // src/shared/client/script-loader.ts
303
+ async function loadScripts(scripts) {
304
+ await Promise.all(
305
+ scripts.map((script) => {
306
+ return new Promise((resolve, reject) => {
307
+ const newSrc = new URL(
308
+ // remove the remote component bundle name identifier from the script src
309
+ script.src.replace(NEXT_BUNDLE_PATH_RE, "/_next/"),
310
+ location.origin
311
+ ).href;
312
+ const loadScriptWithProtectedRcFallback = (src, isFallback = false) => {
313
+ const newScript = document.createElement("script");
314
+ newScript.onload = () => {
315
+ if (isFallback) {
316
+ logInfo(
317
+ "ScriptLoader",
318
+ `Successfully loaded <script src="${newSrc}"> using fallback.`
319
+ );
320
+ }
321
+ resolve();
322
+ };
323
+ newScript.onerror = () => {
324
+ if (!isFallback) {
325
+ const fallbackSrc = generateProtectedRcFallbackSrc(newSrc);
326
+ logWarn(
327
+ "ScriptLoader",
328
+ `Failed to load <script src="${newSrc}"> for Remote Component. Trying fallback with ${RC_PROTECTED_REMOTE_FETCH_PATHNAME} (withRemoteComponentsHost)...`
329
+ );
330
+ loadScriptWithProtectedRcFallback(fallbackSrc, true);
331
+ } else {
332
+ logError(
333
+ "ScriptLoader",
334
+ `Failed to load fallback for <script src="${newSrc}"> for Remote Component.`
335
+ );
336
+ reject(
337
+ new RemoteComponentsError(
338
+ `Failed to load <script src="${newSrc}"> for Remote Component. Check the URL is correct.`
339
+ )
340
+ );
341
+ }
342
+ };
343
+ newScript.src = src;
344
+ newScript.async = true;
345
+ document.head.appendChild(newScript);
346
+ };
347
+ loadScriptWithProtectedRcFallback(newSrc);
348
+ });
349
+ })
350
+ );
351
+ }
352
+ var init_script_loader = __esm({
353
+ "src/shared/client/script-loader.ts"() {
354
+ "use strict";
355
+ init_constants();
356
+ init_error();
357
+ init_logger();
358
+ init_protected_rc_fallback();
359
+ init_webpack_patterns();
360
+ }
361
+ });
362
+
363
+ // src/shared/utils/index.ts
364
+ function escapeString(str) {
365
+ return str.replace(/[^a-z0-9]/g, "_");
366
+ }
367
+ var init_utils = __esm({
368
+ "src/shared/utils/index.ts"() {
369
+ "use strict";
370
+ }
371
+ });
372
+
373
+ // src/shared/client/const.ts
374
+ function getBundleKey(bundle) {
375
+ return escapeString(bundle);
376
+ }
377
+ var RUNTIME_WEBPACK, RUNTIME_TURBOPACK, REMOTE_COMPONENT_REGEX;
378
+ var init_const = __esm({
379
+ "src/shared/client/const.ts"() {
380
+ "use strict";
381
+ init_utils();
382
+ RUNTIME_WEBPACK = "webpack";
383
+ RUNTIME_TURBOPACK = "turbopack";
384
+ REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
385
+ }
386
+ });
387
+
273
388
  // src/shared/utils/abort.ts
274
389
  function isAbortError(error) {
275
390
  if (error instanceof DOMException && error.name === "AbortError") {
@@ -287,7 +402,7 @@ var init_abort = __esm({
287
402
  }
288
403
  });
289
404
 
290
- // src/shared/ssr/fetch-with-protected-rc-fallback.ts
405
+ // src/shared/client/fetch-with-protected-rc-fallback.ts
291
406
  async function fetchWithProtectedRcFallback(url, init) {
292
407
  try {
293
408
  const res = await fetch(url, init);
@@ -296,16 +411,21 @@ async function fetchWithProtectedRcFallback(url, init) {
296
411
  if (isAbortError(error)) {
297
412
  throw error;
298
413
  }
299
- if (typeof document === "object" && typeof document.location === "object" && document.location.origin !== new URL(url).origin) {
300
- logInfo(
414
+ const parsedUrl = new URL(url);
415
+ if (typeof document === "object" && typeof document.location === "object" && document.location.origin !== parsedUrl.origin) {
416
+ logWarn(
301
417
  "FetchRemoteComponent",
302
418
  "Request failed due to CORS, attempting to fetch it via the withRemoteComponentsHost proxy."
303
419
  );
304
420
  const proxiedRes = await fetch(
305
- `${RC_PROTECTED_REMOTE_FETCH_PATHNAME}?url=${url}`,
421
+ generateProtectedRcFallbackSrc(parsedUrl.href),
306
422
  init?.signal ? { signal: init.signal } : void 0
307
423
  );
308
424
  if (proxiedRes.status === 200) {
425
+ logInfo(
426
+ "FetchRemoteComponent",
427
+ `Successfully fetched ${parsedUrl.href} with fallback withRemoteComponentsHost proxy`
428
+ );
309
429
  return proxiedRes;
310
430
  } else {
311
431
  logError(
@@ -317,38 +437,26 @@ async function fetchWithProtectedRcFallback(url, init) {
317
437
  throw error;
318
438
  }
319
439
  }
320
- var RC_PROTECTED_REMOTE_FETCH_PATHNAME;
321
440
  var init_fetch_with_protected_rc_fallback = __esm({
322
- "src/shared/ssr/fetch-with-protected-rc-fallback.ts"() {
441
+ "src/shared/client/fetch-with-protected-rc-fallback.ts"() {
323
442
  "use strict";
443
+ init_protected_rc_fallback();
324
444
  init_abort();
325
445
  init_logger();
326
- RC_PROTECTED_REMOTE_FETCH_PATHNAME = "/rc-fetch-protected-remote";
327
446
  }
328
447
  });
329
448
 
330
- // src/shared/utils/index.ts
331
- function escapeString(str) {
332
- return str.replace(/[^a-z0-9]/g, "_");
333
- }
334
- var init_utils = __esm({
335
- "src/shared/utils/index.ts"() {
336
- "use strict";
337
- }
338
- });
339
-
340
- // src/shared/client/const.ts
341
- function getBundleKey(bundle) {
342
- return escapeString(bundle);
343
- }
344
- var RUNTIME_WEBPACK, RUNTIME_TURBOPACK, REMOTE_COMPONENT_REGEX;
345
- var init_const = __esm({
346
- "src/shared/client/const.ts"() {
449
+ // src/shared/client/turbopack-patterns.ts
450
+ var REMOTE_SHARED_MARKER_RE, REMOTE_SHARED_ASSIGNMENT_RE, ASYNC_MODULE_LOADER_RE, ASYNC_MODULE_RESOLVE_RE, ASYNC_MODULE_ALL_RE, TURBOPACK_GLOBAL_RE;
451
+ var init_turbopack_patterns = __esm({
452
+ "src/shared/client/turbopack-patterns.ts"() {
347
453
  "use strict";
348
- init_utils();
349
- RUNTIME_WEBPACK = "webpack";
350
- RUNTIME_TURBOPACK = "turbopack";
351
- REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
454
+ REMOTE_SHARED_MARKER_RE = /(?:self|[a-z])\.TURBOPACK_REMOTE_SHARED/;
455
+ REMOTE_SHARED_ASSIGNMENT_RE = /\.TURBOPACK_REMOTE_SHARED=await (?:__turbopack_context__|e)\.A\((?<sharedModuleId>[0-9]+)\)/;
456
+ ASYNC_MODULE_LOADER_RE = /(?:__turbopack_context__|e)\.A\((?<asyncSharedModuleId>[0-9]+)\)/;
457
+ ASYNC_MODULE_RESOLVE_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<inner>parentImport|e)=>Promise\.resolve\(\)\.then\(\(\)=>\k<inner>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
458
+ ASYNC_MODULE_ALL_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<vCb>parentImport|t)=>Promise\.all\(\["[^"]+"\]\.map\((?<mapCb>chunk|t)=>\k<ctx>\.l\(\k<mapCb>\)\)\)\.then\(\(\)=>\k<vCb>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
459
+ TURBOPACK_GLOBAL_RE = /(?:globalThis|self)\s*(?:\.TURBOPACK|\[\s*["']TURBOPACK["']\s*\])/;
352
460
  }
353
461
  });
354
462
 
@@ -396,7 +504,7 @@ function createChunkLoader(runtime) {
396
504
  logDebug("ChunkLoader", `Fetching chunk from: "${url}"`);
397
505
  self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise((resolve, reject) => {
398
506
  fetchWithProtectedRcFallback(url).then((res) => res.text()).then((code) => {
399
- const hasTurbopack = code.includes("globalThis.TURBOPACK") || code.includes("self.TURBOPACK");
507
+ const hasTurbopack = TURBOPACK_GLOBAL_RE.test(code);
400
508
  if (hasTurbopack) {
401
509
  return handleTurbopackChunk(code, bundle ?? "", url);
402
510
  }
@@ -429,7 +537,13 @@ async function handleTurbopackChunk(code, bundle, url) {
429
537
  const self = globalThis;
430
538
  const bundleKey = getBundleKey(bundle);
431
539
  logDebug("ChunkLoader", `Bundle key: "${bundleKey}"`);
432
- const transformedCode = code.replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${bundleKey}`).replace(/self\.TURBOPACK(?!_)/g, `self.TURBOPACK_${bundleKey}`).replace(
540
+ const transformedCode = code.replace(
541
+ /globalThis\[\s*["']TURBOPACK["']\s*\]/g,
542
+ `globalThis["TURBOPACK_${bundleKey}"]`
543
+ ).replace(
544
+ /self\[\s*["']TURBOPACK["']\s*\]/g,
545
+ `self["TURBOPACK_${bundleKey}"]`
546
+ ).replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${bundleKey}`).replace(/self\.TURBOPACK(?!_)/g, `self.TURBOPACK_${bundleKey}`).replace(
433
547
  /TURBOPACK_WORKER_LOCATION/g,
434
548
  `TURBOPACK_WORKER_LOCATION_${bundleKey}`
435
549
  ).replace(
@@ -548,23 +662,10 @@ var init_chunk_loader = __esm({
548
662
  "src/shared/client/chunk-loader.ts"() {
549
663
  "use strict";
550
664
  init_error();
551
- init_fetch_with_protected_rc_fallback();
552
665
  init_logger();
553
666
  init_const();
554
- }
555
- });
556
-
557
- // src/shared/client/turbopack-patterns.ts
558
- var REMOTE_SHARED_MARKER_RE, REMOTE_SHARED_ASSIGNMENT_RE, ASYNC_MODULE_LOADER_RE, ASYNC_MODULE_RESOLVE_RE, ASYNC_MODULE_ALL_RE, NUMERIC_MODULE_ID_RE;
559
- var init_turbopack_patterns = __esm({
560
- "src/shared/client/turbopack-patterns.ts"() {
561
- "use strict";
562
- REMOTE_SHARED_MARKER_RE = /(?:self|[a-z])\.TURBOPACK_REMOTE_SHARED/;
563
- REMOTE_SHARED_ASSIGNMENT_RE = /\.TURBOPACK_REMOTE_SHARED=await (?:__turbopack_context__|e)\.A\((?<sharedModuleId>[0-9]+)\)/;
564
- ASYNC_MODULE_LOADER_RE = /(?:__turbopack_context__|e)\.A\((?<asyncSharedModuleId>[0-9]+)\)/;
565
- ASYNC_MODULE_RESOLVE_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<inner>parentImport|e)=>Promise\.resolve\(\)\.then\(\(\)=>\k<inner>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
566
- ASYNC_MODULE_ALL_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<vCb>parentImport|t)=>Promise\.all\(\["[^"]+"\]\.map\((?<mapCb>chunk|t)=>\k<ctx>\.l\(\k<mapCb>\)\)\)\.then\(\(\)=>\k<vCb>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
567
- NUMERIC_MODULE_ID_RE = /^[0-9]+$/;
667
+ init_fetch_with_protected_rc_fallback();
668
+ init_turbopack_patterns();
568
669
  }
569
670
  });
570
671
 
@@ -572,10 +673,14 @@ var init_turbopack_patterns = __esm({
572
673
  function handleTurbopackModule(bundle, moduleId, id) {
573
674
  const self = globalThis;
574
675
  const bundleKey = getBundleKey(bundle);
575
- let modules = self[`TURBOPACK_${bundleKey}`];
576
- if (modules && typeof modules === "object" && "__chunks__" in modules) {
577
- const chunks = modules.__chunks__;
578
- modules = chunks.flat();
676
+ const raw = self[`TURBOPACK_${bundleKey}`];
677
+ let modules;
678
+ if (raw && typeof raw === "object" && "__chunks__" in raw) {
679
+ modules = raw.__chunks__.flat();
680
+ } else if (Array.isArray(raw)) {
681
+ modules = raw.flat();
682
+ } else {
683
+ modules = raw;
579
684
  }
580
685
  if (!self.__remote_components_turbopack_modules__) {
581
686
  self.__remote_components_turbopack_modules__ = {};
@@ -604,13 +709,12 @@ function handleTurbopackModule(bundle, moduleId, id) {
604
709
  if (!self.__remote_components_turbopack_global__[bundle]) {
605
710
  self.__remote_components_turbopack_global__[bundle] = {};
606
711
  }
607
- const allModules = Array.isArray(modules) ? modules.flat() : modules ? Object.values(modules) : [];
608
712
  moduleInit(
609
713
  createTurbopackContext(
610
714
  bundle,
611
715
  exports,
612
716
  moduleExports,
613
- allModules,
717
+ modules,
614
718
  moduleInit,
615
719
  id,
616
720
  self
@@ -624,46 +728,27 @@ function handleTurbopackModule(bundle, moduleId, id) {
624
728
  return moduleExports.exports;
625
729
  }
626
730
  function findModuleInit(modules, moduleId) {
627
- if (modules && !Array.isArray(modules) && typeof modules === "object") {
628
- const normalizedId = NUMERIC_MODULE_ID_RE.test(moduleId) ? Number(moduleId) : moduleId;
629
- if (normalizedId in modules) {
630
- return modules[normalizedId];
631
- }
632
- if (typeof normalizedId === "number" && String(normalizedId) in modules) {
633
- return modules[String(normalizedId)];
634
- }
635
- const matchingKey = Object.keys(modules).find(
636
- (key) => typeof key === "string" && key.includes(String(moduleId))
731
+ if (!modules || typeof modules !== "object")
732
+ return;
733
+ if (!Array.isArray(modules)) {
734
+ const key = moduleId in modules ? moduleId : Object.keys(modules).find((k) => k.startsWith(moduleId));
735
+ return key !== void 0 ? modules[key] : void 0;
736
+ }
737
+ const flat = modules.flat();
738
+ let idx = flat.findIndex((e) => String(e) === String(moduleId));
739
+ if (idx < 0) {
740
+ idx = flat.findIndex(
741
+ (e) => typeof e === "string" && e.startsWith(moduleId)
637
742
  );
638
- if (matchingKey) {
639
- return modules[matchingKey];
640
- }
641
- logError("TurbopackModule", `No match found for module ID: ${moduleId}`);
642
- return void 0;
643
- }
644
- const allModules = modules?.flat() ?? [];
645
- if (typeof allModules[1] === "string" || typeof allModules[1] === "number") {
646
- const normalizedId = NUMERIC_MODULE_ID_RE.test(moduleId) ? Number(moduleId) : moduleId;
647
- let moduleIdIndex = allModules.indexOf(normalizedId);
648
- if (moduleIdIndex === -1) {
649
- moduleIdIndex = allModules.findIndex(
650
- (bundleEntry) => typeof bundleEntry === "string" && bundleEntry.startsWith(moduleId) || bundleEntry === normalizedId
651
- );
652
- }
653
- if (moduleIdIndex !== -1) {
654
- while (typeof allModules[moduleIdIndex] !== "function" && moduleIdIndex < allModules.length) {
655
- moduleIdIndex++;
656
- }
657
- return allModules[moduleIdIndex];
658
- }
659
- } else {
660
- return allModules.find(
661
- (bundleEntry) => typeof bundleEntry === "object" && bundleEntry !== null && moduleId in bundleEntry
662
- )?.[moduleId];
663
743
  }
664
- return void 0;
744
+ if (idx >= 0) {
745
+ return flat.slice(idx + 1).find((e) => typeof e === "function");
746
+ }
747
+ return flat.find(
748
+ (e) => Boolean(e && typeof e === "object" && moduleId in e)
749
+ )?.[moduleId];
665
750
  }
666
- function createTurbopackContext(bundle, exports, moduleExports, allModules, moduleInit, id, self) {
751
+ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleInit, id, self) {
667
752
  return {
668
753
  // HMR not implemented for Remote Components
669
754
  k: {
@@ -784,13 +869,17 @@ function createTurbopackContext(bundle, exports, moduleExports, allModules, modu
784
869
  (parentId) => self.__webpack_require__?.(`[${bundle}] ${parentId}`)
785
870
  );
786
871
  },
872
+ // dynamic import tracking — no-op for remote components
873
+ j() {
874
+ },
787
875
  // chunk loader
788
876
  l(url) {
789
- const moduleInitIndex = allModules.indexOf(moduleInit);
877
+ const flatModules = Array.isArray(modules) ? modules : [];
878
+ const moduleInitIndex = flatModules.indexOf(moduleInit);
790
879
  if (moduleInitIndex !== -1) {
791
- const scriptIndex = allModules.slice(0, moduleInitIndex).findLastIndex((bundleEntry) => bundleEntry instanceof Element);
880
+ const scriptIndex = flatModules.slice(0, moduleInitIndex).findLastIndex((bundleEntry) => bundleEntry instanceof Element);
792
881
  if (scriptIndex !== -1) {
793
- const script = allModules[scriptIndex];
882
+ const script = flatModules[scriptIndex];
794
883
  const scriptSrc = script.getAttribute("data-turbopack-src") || "";
795
884
  const nextIndex = scriptSrc.indexOf("/_next");
796
885
  const baseUrl = nextIndex !== -1 ? scriptSrc.slice(0, nextIndex) : "";
@@ -813,7 +902,6 @@ var init_turbopack_module = __esm({
813
902
  "use strict";
814
903
  init_logger();
815
904
  init_const();
816
- init_turbopack_patterns();
817
905
  }
818
906
  });
819
907
 
@@ -1053,6 +1141,89 @@ var init_webpack_adapter = __esm({
1053
1141
  });
1054
1142
 
1055
1143
  // src/shared/client/static-loader.ts
1144
+ async function importViaProxy(absoluteSrc) {
1145
+ const proxyUrl = new URL(
1146
+ generateProtectedRcFallbackSrc(absoluteSrc),
1147
+ location.href
1148
+ ).href;
1149
+ const response = await fetch(proxyUrl);
1150
+ if (!response.ok)
1151
+ throw new Error(`Proxy fetch failed: ${response.status}`);
1152
+ logInfo(
1153
+ "StaticLoader",
1154
+ `Successfully loaded ${absoluteSrc} via protected RC proxy fallback.`
1155
+ );
1156
+ const content = (await response.text()).replace(/import\.meta\.url/g, JSON.stringify(absoluteSrc)).replace(
1157
+ /\b(from|import)\s*(["'])(\.\.?\/[^"']+)\2/g,
1158
+ (_, keyword, quote, relativePath) => {
1159
+ const absoluteImportUrl = new URL(relativePath, absoluteSrc).href;
1160
+ const absoluteProxyUrl = new URL(
1161
+ generateProtectedRcFallbackSrc(absoluteImportUrl),
1162
+ location.href
1163
+ ).href;
1164
+ return `${keyword} ${quote}${absoluteProxyUrl}${quote}`;
1165
+ }
1166
+ );
1167
+ const moduleBlobUrl = URL.createObjectURL(
1168
+ new Blob([content], { type: "text/javascript" })
1169
+ );
1170
+ const wrapperContent = [
1171
+ `import*as m from${JSON.stringify(moduleBlobUrl)};`,
1172
+ `globalThis.__rc_module_registry__=globalThis.__rc_module_registry__||{};`,
1173
+ `globalThis.__rc_module_registry__[${JSON.stringify(absoluteSrc)}]=m;`
1174
+ ].join("");
1175
+ const wrapperBlobUrl = URL.createObjectURL(
1176
+ new Blob([wrapperContent], { type: "text/javascript" })
1177
+ );
1178
+ const scriptEl = document.createElement("script");
1179
+ scriptEl.type = "module";
1180
+ scriptEl.src = wrapperBlobUrl;
1181
+ try {
1182
+ await new Promise((resolve, reject) => {
1183
+ scriptEl.onload = () => resolve();
1184
+ scriptEl.onerror = () => reject(new Error(`Failed to load module for ${absoluteSrc}`));
1185
+ document.head.appendChild(scriptEl);
1186
+ });
1187
+ } finally {
1188
+ scriptEl.remove();
1189
+ URL.revokeObjectURL(moduleBlobUrl);
1190
+ URL.revokeObjectURL(wrapperBlobUrl);
1191
+ }
1192
+ const registry = globalThis.__rc_module_registry__;
1193
+ const mod = registry?.[absoluteSrc] ?? {};
1194
+ if (registry)
1195
+ delete registry[absoluteSrc];
1196
+ return mod;
1197
+ }
1198
+ function resolveScriptSrc(script, url) {
1199
+ const rawSrc = typeof script.getAttribute === "function" ? script.getAttribute("src") ?? script.src : script.src;
1200
+ if (!rawSrc && script.textContent) {
1201
+ return URL.createObjectURL(
1202
+ new Blob(
1203
+ [script.textContent.replace(/import\.meta\.url/g, JSON.stringify(url))],
1204
+ { type: "text/javascript" }
1205
+ )
1206
+ );
1207
+ }
1208
+ return rawSrc;
1209
+ }
1210
+ async function importScriptMod(absoluteSrc) {
1211
+ try {
1212
+ return await import(
1213
+ /* @vite-ignore */
1214
+ /* webpackIgnore: true */
1215
+ absoluteSrc
1216
+ );
1217
+ } catch (importError) {
1218
+ if (absoluteSrc.startsWith("blob:"))
1219
+ throw importError;
1220
+ logWarn(
1221
+ "StaticLoader",
1222
+ `Direct import of ${absoluteSrc} failed, attempting via protected RC proxy fallback.`
1223
+ );
1224
+ return importViaProxy(absoluteSrc);
1225
+ }
1226
+ }
1056
1227
  async function loadStaticRemoteComponent(scripts, url) {
1057
1228
  const self = globalThis;
1058
1229
  if (self.__remote_script_entrypoint_mount__?.[url.href]) {
@@ -1064,26 +1235,9 @@ async function loadStaticRemoteComponent(scripts, url) {
1064
1235
  const mountUnmountSets = await Promise.all(
1065
1236
  scripts.map(async (script) => {
1066
1237
  try {
1067
- let src = typeof script.getAttribute === "function" ? script.getAttribute("src") ?? script.src : script.src;
1068
- if (!src && script.textContent) {
1069
- const blob = new Blob(
1070
- [
1071
- script.textContent.replace(
1072
- /import\.meta\.url/g,
1073
- JSON.stringify(url)
1074
- )
1075
- ],
1076
- {
1077
- type: "text/javascript"
1078
- }
1079
- );
1080
- src = URL.createObjectURL(blob);
1081
- }
1082
- const mod = await import(
1083
- /* @vite-ignore */
1084
- /* webpackIgnore: true */
1085
- new URL(src, url).href
1086
- );
1238
+ const src = resolveScriptSrc(script, url);
1239
+ const absoluteSrc = new URL(src, url).href;
1240
+ const mod = await importScriptMod(absoluteSrc);
1087
1241
  if (src.startsWith("blob:")) {
1088
1242
  URL.revokeObjectURL(src);
1089
1243
  }
@@ -1148,6 +1302,7 @@ var init_static_loader = __esm({
1148
1302
  "src/shared/client/static-loader.ts"() {
1149
1303
  "use strict";
1150
1304
  init_logger();
1305
+ init_protected_rc_fallback();
1151
1306
  }
1152
1307
  });
1153
1308
 
@@ -1389,35 +1544,18 @@ async function webpackRuntime(bundle, shared, remoteShared) {
1389
1544
  default: { createFromReadableStream }
1390
1545
  } = await import("react-server-dom-webpack/client.browser");
1391
1546
  async function preloadScripts(scripts, url, remoteBundle, _) {
1392
- await Promise.all(
1393
- scripts.map((script) => {
1394
- return new Promise((resolve2, reject) => {
1395
- const newScript = document.createElement("script");
1396
- newScript.onload = () => {
1397
- resolve2();
1398
- newScript.remove();
1399
- };
1400
- newScript.onerror = () => {
1401
- reject(
1402
- new RemoteComponentsError(
1403
- `Failed to load <script src="${script.src}"> for Remote Component. Check the URL is correct.`
1404
- )
1405
- );
1406
- newScript.remove();
1407
- };
1408
- const scriptSrc = script.getAttribute("src") || script.getAttribute("data-src");
1409
- if (scriptSrc) {
1410
- newScript.src = new URL(
1411
- scriptSrc.replace(/\/_next\/\[(?:.+)\](?:%20| )/, "/_next/"),
1412
- url
1413
- ).href;
1414
- }
1415
- newScript.async = true;
1416
- document.body.appendChild(newScript);
1417
- script.parentElement?.removeChild(script);
1418
- });
1419
- })
1420
- );
1547
+ const scriptSrcs = scripts.flatMap((script) => {
1548
+ const scriptSrc = script.getAttribute("src") || script.getAttribute("data-src");
1549
+ script.parentElement?.removeChild(script);
1550
+ if (!scriptSrc)
1551
+ return [];
1552
+ return [
1553
+ {
1554
+ src: new URL(scriptSrc.replace(NEXT_BUNDLE_PATH_RE, "/_next/"), url).href
1555
+ }
1556
+ ];
1557
+ });
1558
+ await loadScripts(scriptSrcs);
1421
1559
  const hostShared = {
1422
1560
  ...sharedPolyfills(shared),
1423
1561
  ...self.__remote_component_host_shared_modules__,
@@ -1461,6 +1599,8 @@ var init_webpack = __esm({
1461
1599
  "src/html/host/runtime/webpack.ts"() {
1462
1600
  "use strict";
1463
1601
  init_polyfill();
1602
+ init_script_loader();
1603
+ init_webpack_patterns();
1464
1604
  init_error();
1465
1605
  init_next_client_pages_loader();
1466
1606
  init_shared_modules();
@@ -1692,20 +1832,22 @@ function createRSCStream(rscName, data) {
1692
1832
  });
1693
1833
  }
1694
1834
 
1695
- // src/shared/client/script-loader.ts
1696
- init_error();
1697
-
1698
1835
  // src/shared/client/component-loader.ts
1836
+ init_script_loader();
1699
1837
  init_webpack_adapter();
1700
1838
 
1701
1839
  // src/shared/client/remote-component.ts
1702
1840
  init_const();
1841
+ init_script_loader();
1703
1842
  init_static_loader();
1704
1843
  init_webpack_adapter();
1705
1844
 
1706
1845
  // src/html/host/index.tsx
1707
1846
  init_error();
1708
1847
 
1848
+ // src/shared/ssr/fetch-with-hooks.ts
1849
+ init_fetch_with_protected_rc_fallback();
1850
+
1709
1851
  // src/shared/ssr/fetch-headers.ts
1710
1852
  function remoteFetchHeaders() {
1711
1853
  return {
@@ -1723,7 +1865,6 @@ function remoteFetchHeaders() {
1723
1865
  }
1724
1866
 
1725
1867
  // src/shared/ssr/fetch-with-hooks.ts
1726
- init_fetch_with_protected_rc_fallback();
1727
1868
  async function fetchWithHooks(url, additionalInit, options = {}) {
1728
1869
  const {
1729
1870
  onRequest,
@@ -1889,7 +2030,6 @@ if (typeof HTMLElement !== "undefined") {
1889
2030
  if (this.getAttribute("src")) {
1890
2031
  this.load().catch((e) => {
1891
2032
  if (isAbortError(e)) {
1892
- this.isLoading = false;
1893
2033
  return;
1894
2034
  }
1895
2035
  logError("HtmlHost", "Error loading remote component.", e);
@@ -1915,7 +2055,6 @@ if (typeof HTMLElement !== "undefined") {
1915
2055
  this.root = newRoot;
1916
2056
  this.load().catch((e) => {
1917
2057
  if (isAbortError(e)) {
1918
- this.isLoading = false;
1919
2058
  return;
1920
2059
  }
1921
2060
  logError("HtmlHost", "Error reloading remote component.", e);
@@ -1938,7 +2077,14 @@ if (typeof HTMLElement !== "undefined") {
1938
2077
  });
1939
2078
  });
1940
2079
  if (this.isLoading) {
1941
- return;
2080
+ this.abortController?.abort();
2081
+ this.isLoading = false;
2082
+ if (this.root && !this.reactRoot) {
2083
+ this.root.innerHTML = "";
2084
+ this.fouc = null;
2085
+ this.fallbackSlot = document.createElement("slot");
2086
+ this.root.appendChild(this.fallbackSlot);
2087
+ }
1942
2088
  }
1943
2089
  if (!this.root) {
1944
2090
  this.root = this.attachShadow({
@@ -1949,28 +2095,16 @@ if (typeof HTMLElement !== "undefined") {
1949
2095
  }
1950
2096
  this.name = this.getAttribute("name") || "__vercel_remote_component";
1951
2097
  this.bundle = "default";
1952
- if (this.hasAttribute("src") || this.querySelector("div#__REMOTE_COMPONENT__") || this.hasAttribute("data-ssr")) {
1953
- this.load().catch((e) => {
1954
- if (isAbortError(e)) {
1955
- this.isLoading = false;
1956
- return;
1957
- }
1958
- logError("HtmlHost", "Error loading remote component.", e);
1959
- const errorEvent = new Event("error", {
1960
- bubbles: true,
1961
- composed: true
1962
- });
1963
- Object.assign(errorEvent, {
1964
- error: e,
1965
- src: this.getAttribute("src")
1966
- });
1967
- this.dispatchEvent(errorEvent);
1968
- });
1969
- }
1970
2098
  this.isLoading = true;
1971
2099
  const src = this.getAttribute("src");
1972
2100
  this.abortController = new AbortController();
1973
2101
  const signal = this.abortController.signal;
2102
+ const isCurrentLoad = () => !signal.aborted && this.getAttribute("src") === src;
2103
+ const abandonLoad = () => {
2104
+ if (this.abortController?.signal === signal && this.isLoading) {
2105
+ this.isLoading = false;
2106
+ }
2107
+ };
1974
2108
  const beforeLoadEvent = new Event("beforeload", {
1975
2109
  bubbles: true,
1976
2110
  composed: true
@@ -1993,11 +2127,19 @@ if (typeof HTMLElement !== "undefined") {
1993
2127
  const fetchInit = {
1994
2128
  credentials: this.getAttribute("credentials") || "same-origin"
1995
2129
  };
1996
- const res = await fetchWithHooks(url, fetchInit, {
1997
- onRequest: this.onRequest,
1998
- onResponse: this.onResponse,
1999
- abortController: this.abortController
2000
- });
2130
+ let res;
2131
+ try {
2132
+ res = await fetchWithHooks(url, fetchInit, {
2133
+ onRequest: this.onRequest,
2134
+ onResponse: this.onResponse,
2135
+ abortController: this.abortController
2136
+ });
2137
+ } catch (e) {
2138
+ if (isAbortError(e)) {
2139
+ return abandonLoad();
2140
+ }
2141
+ throw e;
2142
+ }
2001
2143
  if (!res || !res.ok) {
2002
2144
  let error = failedToFetchRemoteComponentError(
2003
2145
  url.href,
@@ -2028,7 +2170,17 @@ if (typeof HTMLElement !== "undefined") {
2028
2170
  }
2029
2171
  throw error;
2030
2172
  }
2031
- html = await res.text();
2173
+ try {
2174
+ html = await res.text();
2175
+ } catch (e) {
2176
+ if (isAbortError(e)) {
2177
+ return abandonLoad();
2178
+ }
2179
+ throw e;
2180
+ }
2181
+ if (!isCurrentLoad()) {
2182
+ return abandonLoad();
2183
+ }
2032
2184
  }
2033
2185
  const parser = new DOMParser();
2034
2186
  const doc = parser.parseFromString(html, "text/html");
@@ -2111,6 +2263,9 @@ if (typeof HTMLElement !== "undefined") {
2111
2263
  }
2112
2264
  })
2113
2265
  );
2266
+ if (!isCurrentLoad()) {
2267
+ return abandonLoad();
2268
+ }
2114
2269
  }
2115
2270
  }
2116
2271
  this.root.innerHTML = "";
@@ -2155,6 +2310,9 @@ if (typeof HTMLElement !== "undefined") {
2155
2310
  remoteComponentSrc,
2156
2311
  root: this.root
2157
2312
  });
2313
+ if (!isCurrentLoad()) {
2314
+ return abandonLoad();
2315
+ }
2158
2316
  }
2159
2317
  applyOriginToNodes(doc, url ?? new URL(location.href));
2160
2318
  if (!this.reactRoot) {
@@ -2238,6 +2396,9 @@ if (typeof HTMLElement !== "undefined") {
2238
2396
  },
2239
2397
  remoteShared
2240
2398
  );
2399
+ if (!isCurrentLoad()) {
2400
+ return abandonLoad();
2401
+ }
2241
2402
  const scripts = isRemoteComponent ? component.querySelectorAll("script") : doc.querySelectorAll(
2242
2403
  "script[src],script[data-src],script[data-remote-component-entrypoint]"
2243
2404
  );
@@ -2253,6 +2414,9 @@ if (typeof HTMLElement !== "undefined") {
2253
2414
  this.bundle ?? "default",
2254
2415
  this.name ?? "__vercel_remote_component"
2255
2416
  );
2417
+ if (!isCurrentLoad()) {
2418
+ return abandonLoad();
2419
+ }
2256
2420
  if (isRemoteComponent) {
2257
2421
  Array.from(component.children).forEach((child) => {
2258
2422
  if (child.tagName === "SCRIPT") {
@@ -2313,7 +2477,9 @@ if (typeof HTMLElement !== "undefined") {
2313
2477
  logError("HtmlHost", "Error attaching styles.", e);
2314
2478
  });
2315
2479
  }
2316
- this.isLoading = false;
2480
+ if (isCurrentLoad()) {
2481
+ this.isLoading = false;
2482
+ }
2317
2483
  const loadEvent = new Event("load", {
2318
2484
  bubbles: true,
2319
2485
  composed: true
@@ -2366,7 +2532,9 @@ if (typeof HTMLElement !== "undefined") {
2366
2532
  logError("HtmlHost", "Error attaching styles.", e);
2367
2533
  });
2368
2534
  }
2369
- remoteComponent.isLoading = false;
2535
+ if (isCurrentLoad()) {
2536
+ remoteComponent.isLoading = false;
2537
+ }
2370
2538
  const loadEvent = new Event("load", {
2371
2539
  bubbles: true,
2372
2540
  composed: true
@@ -2383,7 +2551,9 @@ if (typeof HTMLElement !== "undefined") {
2383
2551
  (0, import_react.startTransition)(() => {
2384
2552
  root.render(/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(RemoteComponentFromNext, { initial: false }));
2385
2553
  doCleanup();
2386
- this.isLoading = false;
2554
+ if (isCurrentLoad()) {
2555
+ this.isLoading = false;
2556
+ }
2387
2557
  });
2388
2558
  return;
2389
2559
  }
@@ -2427,7 +2597,9 @@ if (typeof HTMLElement !== "undefined") {
2427
2597
  Object.assign(loadEvent, { src: this.getAttribute("src") });
2428
2598
  this.dispatchEvent(loadEvent);
2429
2599
  }
2430
- this.isLoading = false;
2600
+ if (isCurrentLoad()) {
2601
+ this.isLoading = false;
2602
+ }
2431
2603
  }
2432
2604
  }
2433
2605
  customElements.define("remote-component", RemoteComponent);