remote-components 0.0.45 → 0.0.47

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 +202 -54
  2. package/dist/html/host.cjs.map +1 -1
  3. package/dist/html/host.js +202 -54
  4. package/dist/html/host.js.map +1 -1
  5. package/dist/html/remote.cjs +28 -28
  6. package/dist/html/remote.cjs.map +1 -1
  7. package/dist/html/remote.js +28 -28
  8. package/dist/html/remote.js.map +1 -1
  9. package/dist/internal/next/host/app-router-client.cjs +10 -5
  10. package/dist/internal/next/host/app-router-client.cjs.map +1 -1
  11. package/dist/internal/next/host/app-router-client.js +10 -5
  12. package/dist/internal/next/host/app-router-client.js.map +1 -1
  13. package/dist/internal/next/host/app-router-compat.cjs +9 -4
  14. package/dist/internal/next/host/app-router-compat.cjs.map +1 -1
  15. package/dist/internal/next/host/app-router-compat.js +9 -4
  16. package/dist/internal/next/host/app-router-compat.js.map +1 -1
  17. package/dist/internal/shared/client/polyfill.cjs +3 -1
  18. package/dist/internal/shared/client/polyfill.cjs.map +1 -1
  19. package/dist/internal/shared/client/polyfill.js +3 -1
  20. package/dist/internal/shared/client/polyfill.js.map +1 -1
  21. package/dist/internal/shared/client/remote-component.cjs +158 -27
  22. package/dist/internal/shared/client/remote-component.cjs.map +1 -1
  23. package/dist/internal/shared/client/remote-component.js +158 -27
  24. package/dist/internal/shared/client/remote-component.js.map +1 -1
  25. package/dist/internal/shared/ssr/fetch-remote-component.cjs +4 -6
  26. package/dist/internal/shared/ssr/fetch-remote-component.cjs.map +1 -1
  27. package/dist/internal/shared/ssr/fetch-remote-component.js +4 -6
  28. package/dist/internal/shared/ssr/fetch-remote-component.js.map +1 -1
  29. package/dist/internal/shared/ssr/fetch-with-hooks.cjs +9 -2
  30. package/dist/internal/shared/ssr/fetch-with-hooks.cjs.map +1 -1
  31. package/dist/internal/shared/ssr/fetch-with-hooks.d.ts +6 -1
  32. package/dist/internal/shared/ssr/fetch-with-hooks.js +9 -2
  33. package/dist/internal/shared/ssr/fetch-with-hooks.js.map +1 -1
  34. package/dist/internal/shared/ssr/fetch-with-protected-rc-fallback.cjs +57 -0
  35. package/dist/internal/shared/ssr/fetch-with-protected-rc-fallback.cjs.map +1 -0
  36. package/dist/internal/shared/ssr/fetch-with-protected-rc-fallback.d.ts +11 -0
  37. package/dist/internal/shared/ssr/fetch-with-protected-rc-fallback.js +32 -0
  38. package/dist/internal/shared/ssr/fetch-with-protected-rc-fallback.js.map +1 -0
  39. package/dist/internal/shared/utils/logger.cjs +55 -0
  40. package/dist/internal/shared/utils/logger.cjs.map +1 -0
  41. package/dist/internal/shared/utils/logger.d.ts +7 -0
  42. package/dist/internal/shared/utils/logger.js +28 -0
  43. package/dist/internal/shared/utils/logger.js.map +1 -0
  44. package/dist/next/config.cjs +22 -1
  45. package/dist/next/config.cjs.map +1 -1
  46. package/dist/next/config.js +22 -1
  47. package/dist/next/config.js.map +1 -1
  48. package/dist/next/host/client/index.cjs +207 -68
  49. package/dist/next/host/client/index.cjs.map +1 -1
  50. package/dist/next/host/client/index.js +207 -68
  51. package/dist/next/host/client/index.js.map +1 -1
  52. package/dist/next/proxy.cjs +128 -19
  53. package/dist/next/proxy.cjs.map +1 -1
  54. package/dist/next/proxy.d.ts +34 -6
  55. package/dist/next/proxy.js +125 -18
  56. package/dist/next/proxy.js.map +1 -1
  57. package/dist/react/index.cjs +199 -64
  58. package/dist/react/index.cjs.map +1 -1
  59. package/dist/react/index.js +199 -64
  60. package/dist/react/index.js.map +1 -1
  61. package/dist/shared/host/proxy.cjs +79 -0
  62. package/dist/shared/host/proxy.cjs.map +1 -0
  63. package/dist/shared/host/proxy.d.ts +29 -0
  64. package/dist/shared/host/proxy.js +54 -0
  65. package/dist/shared/host/proxy.js.map +1 -0
  66. package/dist/shared/remote/proxy.cjs +71 -0
  67. package/dist/shared/remote/proxy.cjs.map +1 -0
  68. package/dist/shared/remote/proxy.d.ts +38 -0
  69. package/dist/shared/remote/proxy.js +45 -0
  70. package/dist/shared/remote/proxy.js.map +1 -0
  71. package/package.json +10 -1
@@ -71,9 +71,19 @@ function logDebug(location2, message) {
71
71
  console.debug(`[${PREFIX}:${location2}]: ${message}`);
72
72
  }
73
73
  }
74
+ function logInfo(location2, message) {
75
+ console.info(`[${PREFIX}:${location2}]: ${message}`);
76
+ }
74
77
  function logWarn(location2, message) {
75
78
  console.warn(`[${PREFIX}:${location2}]: ${message}`);
76
79
  }
80
+ function logError(location2, message, cause) {
81
+ console.error(
82
+ new RemoteComponentsError(`[${PREFIX}:${location2}]: ${message}`, {
83
+ cause
84
+ })
85
+ );
86
+ }
77
87
 
78
88
  // src/shared/webpack/next-client-pages-loader.ts
79
89
  function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
@@ -343,6 +353,34 @@ async function loadScripts(scripts) {
343
353
  );
344
354
  }
345
355
 
356
+ // src/shared/ssr/fetch-with-protected-rc-fallback.ts
357
+ var RC_PROTECTED_REMOTE_FETCH_PATHNAME = "/rc-fetch-protected-remote";
358
+ async function fetchWithProtectedRcFallback(url, init) {
359
+ try {
360
+ const res = await fetch(url, init);
361
+ return res;
362
+ } catch (error) {
363
+ if (typeof document === "object" && typeof document.location === "object" && document.location.origin !== new URL(url).origin) {
364
+ logInfo(
365
+ "FetchRemoteComponent",
366
+ "Request failed due to CORS, attempting to fetch it via the withRemoteComponentsHost proxy."
367
+ );
368
+ const proxiedRes = await fetch(
369
+ `${RC_PROTECTED_REMOTE_FETCH_PATHNAME}?url=${url}`
370
+ );
371
+ if (proxiedRes.status === 200) {
372
+ return proxiedRes;
373
+ } else {
374
+ logError(
375
+ "FetchRemoteComponent",
376
+ `Could not proxy remote: [response status ${proxiedRes.status}] ${await proxiedRes.text()}`
377
+ );
378
+ }
379
+ }
380
+ throw error;
381
+ }
382
+ }
383
+
346
384
  // src/shared/utils/index.ts
347
385
  function escapeString(str) {
348
386
  return str.replace(/[^a-z0-9]/g, "_");
@@ -401,10 +439,19 @@ function createChunkLoader(runtime) {
401
439
  }
402
440
  logDebug("ChunkLoader", `Fetching chunk from: "${url}"`);
403
441
  self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise((resolve, reject) => {
404
- fetch(url).then((res) => res.text()).then((code) => {
405
- if (code.includes("globalThis.TURBOPACK")) {
442
+ fetchWithProtectedRcFallback(url).then((res) => res.text()).then((code) => {
443
+ const hasTurbopack = code.includes("globalThis.TURBOPACK") || code.includes("self.TURBOPACK");
444
+ if (hasTurbopack) {
406
445
  return handleTurbopackChunk(code, bundle ?? "", url);
407
446
  }
447
+ logDebug(
448
+ "ChunkLoader",
449
+ `Chunk does not contain globalThis.TURBOPACK or self.TURBOPACK: "${url}"`
450
+ );
451
+ logDebug(
452
+ "ChunkLoader",
453
+ `First 500 chars of chunk: ${code.slice(0, 500)}`
454
+ );
408
455
  }).then(resolve).catch(reject);
409
456
  });
410
457
  return self.__remote_components_turbopack_chunk_loader_promise__[url];
@@ -426,7 +473,7 @@ async function handleTurbopackChunk(code, bundle, url) {
426
473
  const self = globalThis;
427
474
  const bundleKey = getBundleKey(bundle);
428
475
  logDebug("ChunkLoader", `Bundle key: "${bundleKey}"`);
429
- const transformedCode = code.replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${bundleKey}`).replace(
476
+ const transformedCode = code.replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${bundleKey}`).replace(/self\.TURBOPACK(?!_)/g, `self.TURBOPACK_${bundleKey}`).replace(
430
477
  /TURBOPACK_WORKER_LOCATION/g,
431
478
  `TURBOPACK_WORKER_LOCATION_${bundleKey}`
432
479
  ).replace(
@@ -445,6 +492,30 @@ async function handleTurbopackChunk(code, bundle, url) {
445
492
  )
446
493
  ).href}$1$2.js.map`
447
494
  );
495
+ if (!self[`TURBOPACK_${bundleKey}`]) {
496
+ const chunkData = [];
497
+ const turbopackObject = {
498
+ push: (item) => {
499
+ logDebug(
500
+ "ChunkLoader",
501
+ `TURBOPACK_${bundleKey}.push() called with item type: ${Array.isArray(item) ? "array" : typeof item}`
502
+ );
503
+ if (Array.isArray(item)) {
504
+ chunkData.push(item);
505
+ logDebug(
506
+ "ChunkLoader",
507
+ `TURBOPACK_${bundleKey} now has ${chunkData.length} chunks`
508
+ );
509
+ } else {
510
+ chunkData.push([item]);
511
+ }
512
+ return chunkData.length;
513
+ },
514
+ // Store chunks for later access
515
+ __chunks__: chunkData
516
+ };
517
+ self[`TURBOPACK_${bundleKey}`] = turbopackObject;
518
+ }
448
519
  logDebug("ChunkLoader", `Creating blob script for: "${url}"`);
449
520
  await new Promise((scriptResolve, scriptReject) => {
450
521
  const blob = new Blob([transformedCode], {
@@ -457,6 +528,21 @@ async function handleTurbopackChunk(code, bundle, url) {
457
528
  script.async = true;
458
529
  script.onload = () => {
459
530
  URL.revokeObjectURL(scriptUrl);
531
+ logDebug(
532
+ "ChunkLoader",
533
+ `Script loaded successfully for bundle "${bundle}"`
534
+ );
535
+ const turbopackBundle = self[`TURBOPACK_${bundleKey}`];
536
+ logDebug(
537
+ "ChunkLoader",
538
+ `TURBOPACK_${bundleKey} type: ${typeof turbopackBundle}, isArray: ${Array.isArray(turbopackBundle)}, keys: ${turbopackBundle ? Object.keys(turbopackBundle).slice(0, 10).join(", ") : "none"}`
539
+ );
540
+ if (turbopackBundle && typeof turbopackBundle === "object") {
541
+ logDebug(
542
+ "ChunkLoader",
543
+ `TURBOPACK_${bundleKey} length/size: ${Array.isArray(turbopackBundle) ? turbopackBundle.length : Object.keys(turbopackBundle).length}`
544
+ );
545
+ }
460
546
  scriptResolve(void 0);
461
547
  script.remove();
462
548
  };
@@ -503,14 +589,23 @@ async function handleTurbopackChunk(code, bundle, url) {
503
589
  }
504
590
  }
505
591
 
592
+ // src/shared/client/turbopack-patterns.ts
593
+ var REMOTE_SHARED_MARKER_RE = /(?:self|[a-z])\.TURBOPACK_REMOTE_SHARED/;
594
+ var REMOTE_SHARED_ASSIGNMENT_RE = /\.TURBOPACK_REMOTE_SHARED=await (?:__turbopack_context__|e)\.A\((?<sharedModuleId>[0-9]+)\)/;
595
+ var ASYNC_MODULE_LOADER_RE = /(?:__turbopack_context__|e)\.A\((?<asyncSharedModuleId>[0-9]+)\)/;
596
+ var ASYNC_MODULE_RESOLVE_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<inner>parentImport|e)=>Promise\.resolve\(\)\.then\(\(\)=>\k<inner>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
597
+ var 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]+)\)\)\)\}/;
598
+ var NUMERIC_MODULE_ID_RE = /^[0-9]+$/;
599
+
506
600
  // src/shared/client/turbopack-module.ts
507
601
  function handleTurbopackModule(bundle, moduleId, id) {
508
602
  const self = globalThis;
509
603
  const bundleKey = getBundleKey(bundle);
510
- const modules = self[`TURBOPACK_${bundleKey}`];
511
- const moduleInit = findModuleInit(modules, moduleId);
512
- const exports = {};
513
- const moduleExports = { exports };
604
+ let modules = self[`TURBOPACK_${bundleKey}`];
605
+ if (modules && typeof modules === "object" && "__chunks__" in modules) {
606
+ const chunks = modules.__chunks__;
607
+ modules = chunks.flat();
608
+ }
514
609
  if (!self.__remote_components_turbopack_modules__) {
515
610
  self.__remote_components_turbopack_modules__ = {};
516
611
  }
@@ -520,6 +615,12 @@ function handleTurbopackModule(bundle, moduleId, id) {
520
615
  if (self.__remote_components_turbopack_modules__[bundle][moduleId]) {
521
616
  return self.__remote_components_turbopack_modules__[bundle][moduleId];
522
617
  }
618
+ if (!modules) {
619
+ logError("TurbopackModule", `TURBOPACK_${bundleKey} is undefined`);
620
+ }
621
+ const moduleInit = findModuleInit(modules, moduleId);
622
+ const exports = {};
623
+ const moduleExports = { exports };
523
624
  if (typeof moduleInit !== "function") {
524
625
  throw new Error(
525
626
  `Module ${id} not found in bundle ${bundle} with id ${moduleId}`
@@ -532,7 +633,7 @@ function handleTurbopackModule(bundle, moduleId, id) {
532
633
  if (!self.__remote_components_turbopack_global__[bundle]) {
533
634
  self.__remote_components_turbopack_global__[bundle] = {};
534
635
  }
535
- const allModules = modules?.flat() ?? [];
636
+ const allModules = Array.isArray(modules) ? modules.flat() : modules ? Object.values(modules) : [];
536
637
  moduleInit(
537
638
  createTurbopackContext(
538
639
  bundle,
@@ -552,9 +653,26 @@ function handleTurbopackModule(bundle, moduleId, id) {
552
653
  return moduleExports.exports;
553
654
  }
554
655
  function findModuleInit(modules, moduleId) {
656
+ if (modules && !Array.isArray(modules) && typeof modules === "object") {
657
+ const normalizedId = NUMERIC_MODULE_ID_RE.test(moduleId) ? Number(moduleId) : moduleId;
658
+ if (normalizedId in modules) {
659
+ return modules[normalizedId];
660
+ }
661
+ if (typeof normalizedId === "number" && String(normalizedId) in modules) {
662
+ return modules[String(normalizedId)];
663
+ }
664
+ const matchingKey = Object.keys(modules).find(
665
+ (key) => typeof key === "string" && key.includes(String(moduleId))
666
+ );
667
+ if (matchingKey) {
668
+ return modules[matchingKey];
669
+ }
670
+ logError("TurbopackModule", `No match found for module ID: ${moduleId}`);
671
+ return void 0;
672
+ }
555
673
  const allModules = modules?.flat() ?? [];
556
674
  if (typeof allModules[1] === "string" || typeof allModules[1] === "number") {
557
- const normalizedId = /^[0-9]+$/.test(moduleId) ? Number(moduleId) : moduleId;
675
+ const normalizedId = NUMERIC_MODULE_ID_RE.test(moduleId) ? Number(moduleId) : moduleId;
558
676
  let moduleIdIndex = allModules.indexOf(normalizedId);
559
677
  if (moduleIdIndex === -1) {
560
678
  moduleIdIndex = allModules.findIndex(
@@ -720,13 +838,6 @@ function createTurbopackContext(bundle, exports, moduleExports, allModules, modu
720
838
  };
721
839
  }
722
840
 
723
- // src/shared/client/turbopack-patterns.ts
724
- var REMOTE_SHARED_MARKER_RE = /(?:self|[a-z])\.TURBOPACK_REMOTE_SHARED/;
725
- var REMOTE_SHARED_ASSIGNMENT_RE = /\.TURBOPACK_REMOTE_SHARED=await (?:__turbopack_context__|e)\.A\((?<sharedModuleId>[0-9]+)\)/;
726
- var ASYNC_MODULE_LOADER_RE = /(?:__turbopack_context__|e)\.A\((?<asyncSharedModuleId>[0-9]+)\)/;
727
- var ASYNC_MODULE_RESOLVE_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<inner>parentImport|e)=>Promise\.resolve\(\)\.then\(\(\)=>\k<inner>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
728
- var 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]+)\)\)\)\}/;
729
-
730
841
  // src/shared/client/shared-modules.ts
731
842
  async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {}) {
732
843
  const self = globalThis;
@@ -735,10 +846,14 @@ async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {
735
846
  self.__remote_shared_modules__[bundle] = {};
736
847
  }
737
848
  const bundleKey = getBundleKey(bundle);
738
- const modules = self[`TURBOPACK_${bundleKey}`];
849
+ let modules = self[`TURBOPACK_${bundleKey}`];
850
+ if (modules && typeof modules === "object" && "__chunks__" in modules) {
851
+ const chunks = modules.__chunks__;
852
+ modules = chunks.flat();
853
+ }
739
854
  let sharedModuleInitializer = null;
740
- if (modules && Array.isArray(modules)) {
741
- const allModules = modules.flat();
855
+ if (modules) {
856
+ const allModules = Array.isArray(modules) ? modules.flat() : Object.entries(modules).flat();
742
857
  const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {
743
858
  if (typeof idOrFunc !== "function") {
744
859
  return false;
@@ -771,6 +886,11 @@ async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {
771
886
  if (self.__remote_shared_modules__?.[bundle]) {
772
887
  if (hostShared[module2]) {
773
888
  self.__remote_shared_modules__[bundle][id] = await hostShared[module2](bundle);
889
+ } else {
890
+ logError(
891
+ "SharedModules",
892
+ `Host shared module "${module2}" not found for ID ${id}`
893
+ );
774
894
  }
775
895
  }
776
896
  })
@@ -781,9 +901,13 @@ async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {
781
901
  Object.entries(remoteShared).map(async ([id, module2]) => {
782
902
  if (self.__remote_shared_modules__?.[bundle]) {
783
903
  if (hostShared[module2]) {
784
- self.__remote_shared_modules__[bundle][id.replace("[app-ssr]", "[app-client]")] = await hostShared[module2](bundle);
904
+ const normalizedId = id.replace("[app-ssr]", "[app-client]");
905
+ self.__remote_shared_modules__[bundle][normalizedId] = await hostShared[module2](bundle);
785
906
  } else {
786
- console.error(`Shared module "${module2}" not found for "${bundle}".`);
907
+ logError(
908
+ "SharedModules",
909
+ `Shared module "${module2}" not found for "${bundle}"`
910
+ );
787
911
  }
788
912
  }
789
913
  })
@@ -795,7 +919,15 @@ function extractSharedModuleIds(shared, bundleKey, self) {
795
919
  if (asyncSharedModuleId) {
796
920
  const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);
797
921
  let asyncSharedModule;
798
- const newAllModules = self[`TURBOPACK_${bundleKey}`]?.flat() ?? [];
922
+ let turbopackModules = self[`TURBOPACK_${bundleKey}`];
923
+ if (turbopackModules && typeof turbopackModules === "object" && "__chunks__" in turbopackModules) {
924
+ const chunks = turbopackModules.__chunks__;
925
+ turbopackModules = chunks.flat();
926
+ }
927
+ const newAllModules = Array.isArray(turbopackModules) ? turbopackModules.flat() : turbopackModules ? Object.entries(turbopackModules).flatMap(([key2, value2]) => [
928
+ key2,
929
+ value2
930
+ ]) : [];
799
931
  const asyncSharedModuleIdIndex = newAllModules.indexOf(
800
932
  asyncSharedModuleIdNumber
801
933
  );
@@ -1159,11 +1291,10 @@ async function loadStaticRemoteComponent(scripts, url) {
1159
1291
  unmount: mod.unmount || mod.default?.unmount
1160
1292
  };
1161
1293
  } catch (e) {
1162
- console.error(
1163
- new RemoteComponentsError(
1164
- `Error loading remote component script from "${script.src || url.href}".`,
1165
- { cause: e }
1166
- )
1294
+ logError(
1295
+ "StaticLoader",
1296
+ `Error loading remote component script from "${script.src || url.href}".`,
1297
+ e
1167
1298
  );
1168
1299
  return {
1169
1300
  mount: void 0,