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
@@ -22,9 +22,19 @@ function logDebug(location2, message) {
22
22
  console.debug(`[${PREFIX}:${location2}]: ${message}`);
23
23
  }
24
24
  }
25
+ function logInfo(location2, message) {
26
+ console.info(`[${PREFIX}:${location2}]: ${message}`);
27
+ }
25
28
  function logWarn(location2, message) {
26
29
  console.warn(`[${PREFIX}:${location2}]: ${message}`);
27
30
  }
31
+ function logError(location2, message, cause) {
32
+ console.error(
33
+ new RemoteComponentsError(`[${PREFIX}:${location2}]: ${message}`, {
34
+ cause
35
+ })
36
+ );
37
+ }
28
38
 
29
39
  // src/shared/webpack/next-client-pages-loader.ts
30
40
  function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
@@ -294,6 +304,34 @@ async function loadScripts(scripts) {
294
304
  );
295
305
  }
296
306
 
307
+ // src/shared/ssr/fetch-with-protected-rc-fallback.ts
308
+ var RC_PROTECTED_REMOTE_FETCH_PATHNAME = "/rc-fetch-protected-remote";
309
+ async function fetchWithProtectedRcFallback(url, init) {
310
+ try {
311
+ const res = await fetch(url, init);
312
+ return res;
313
+ } catch (error) {
314
+ if (typeof document === "object" && typeof document.location === "object" && document.location.origin !== new URL(url).origin) {
315
+ logInfo(
316
+ "FetchRemoteComponent",
317
+ "Request failed due to CORS, attempting to fetch it via the withRemoteComponentsHost proxy."
318
+ );
319
+ const proxiedRes = await fetch(
320
+ `${RC_PROTECTED_REMOTE_FETCH_PATHNAME}?url=${url}`
321
+ );
322
+ if (proxiedRes.status === 200) {
323
+ return proxiedRes;
324
+ } else {
325
+ logError(
326
+ "FetchRemoteComponent",
327
+ `Could not proxy remote: [response status ${proxiedRes.status}] ${await proxiedRes.text()}`
328
+ );
329
+ }
330
+ }
331
+ throw error;
332
+ }
333
+ }
334
+
297
335
  // src/shared/utils/index.ts
298
336
  function escapeString(str) {
299
337
  return str.replace(/[^a-z0-9]/g, "_");
@@ -352,10 +390,19 @@ function createChunkLoader(runtime) {
352
390
  }
353
391
  logDebug("ChunkLoader", `Fetching chunk from: "${url}"`);
354
392
  self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise((resolve, reject) => {
355
- fetch(url).then((res) => res.text()).then((code) => {
356
- if (code.includes("globalThis.TURBOPACK")) {
393
+ fetchWithProtectedRcFallback(url).then((res) => res.text()).then((code) => {
394
+ const hasTurbopack = code.includes("globalThis.TURBOPACK") || code.includes("self.TURBOPACK");
395
+ if (hasTurbopack) {
357
396
  return handleTurbopackChunk(code, bundle ?? "", url);
358
397
  }
398
+ logDebug(
399
+ "ChunkLoader",
400
+ `Chunk does not contain globalThis.TURBOPACK or self.TURBOPACK: "${url}"`
401
+ );
402
+ logDebug(
403
+ "ChunkLoader",
404
+ `First 500 chars of chunk: ${code.slice(0, 500)}`
405
+ );
359
406
  }).then(resolve).catch(reject);
360
407
  });
361
408
  return self.__remote_components_turbopack_chunk_loader_promise__[url];
@@ -377,7 +424,7 @@ async function handleTurbopackChunk(code, bundle, url) {
377
424
  const self = globalThis;
378
425
  const bundleKey = getBundleKey(bundle);
379
426
  logDebug("ChunkLoader", `Bundle key: "${bundleKey}"`);
380
- const transformedCode = code.replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${bundleKey}`).replace(
427
+ const transformedCode = code.replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${bundleKey}`).replace(/self\.TURBOPACK(?!_)/g, `self.TURBOPACK_${bundleKey}`).replace(
381
428
  /TURBOPACK_WORKER_LOCATION/g,
382
429
  `TURBOPACK_WORKER_LOCATION_${bundleKey}`
383
430
  ).replace(
@@ -396,6 +443,30 @@ async function handleTurbopackChunk(code, bundle, url) {
396
443
  )
397
444
  ).href}$1$2.js.map`
398
445
  );
446
+ if (!self[`TURBOPACK_${bundleKey}`]) {
447
+ const chunkData = [];
448
+ const turbopackObject = {
449
+ push: (item) => {
450
+ logDebug(
451
+ "ChunkLoader",
452
+ `TURBOPACK_${bundleKey}.push() called with item type: ${Array.isArray(item) ? "array" : typeof item}`
453
+ );
454
+ if (Array.isArray(item)) {
455
+ chunkData.push(item);
456
+ logDebug(
457
+ "ChunkLoader",
458
+ `TURBOPACK_${bundleKey} now has ${chunkData.length} chunks`
459
+ );
460
+ } else {
461
+ chunkData.push([item]);
462
+ }
463
+ return chunkData.length;
464
+ },
465
+ // Store chunks for later access
466
+ __chunks__: chunkData
467
+ };
468
+ self[`TURBOPACK_${bundleKey}`] = turbopackObject;
469
+ }
399
470
  logDebug("ChunkLoader", `Creating blob script for: "${url}"`);
400
471
  await new Promise((scriptResolve, scriptReject) => {
401
472
  const blob = new Blob([transformedCode], {
@@ -408,6 +479,21 @@ async function handleTurbopackChunk(code, bundle, url) {
408
479
  script.async = true;
409
480
  script.onload = () => {
410
481
  URL.revokeObjectURL(scriptUrl);
482
+ logDebug(
483
+ "ChunkLoader",
484
+ `Script loaded successfully for bundle "${bundle}"`
485
+ );
486
+ const turbopackBundle = self[`TURBOPACK_${bundleKey}`];
487
+ logDebug(
488
+ "ChunkLoader",
489
+ `TURBOPACK_${bundleKey} type: ${typeof turbopackBundle}, isArray: ${Array.isArray(turbopackBundle)}, keys: ${turbopackBundle ? Object.keys(turbopackBundle).slice(0, 10).join(", ") : "none"}`
490
+ );
491
+ if (turbopackBundle && typeof turbopackBundle === "object") {
492
+ logDebug(
493
+ "ChunkLoader",
494
+ `TURBOPACK_${bundleKey} length/size: ${Array.isArray(turbopackBundle) ? turbopackBundle.length : Object.keys(turbopackBundle).length}`
495
+ );
496
+ }
411
497
  scriptResolve(void 0);
412
498
  script.remove();
413
499
  };
@@ -454,14 +540,23 @@ async function handleTurbopackChunk(code, bundle, url) {
454
540
  }
455
541
  }
456
542
 
543
+ // src/shared/client/turbopack-patterns.ts
544
+ var REMOTE_SHARED_MARKER_RE = /(?:self|[a-z])\.TURBOPACK_REMOTE_SHARED/;
545
+ var REMOTE_SHARED_ASSIGNMENT_RE = /\.TURBOPACK_REMOTE_SHARED=await (?:__turbopack_context__|e)\.A\((?<sharedModuleId>[0-9]+)\)/;
546
+ var ASYNC_MODULE_LOADER_RE = /(?:__turbopack_context__|e)\.A\((?<asyncSharedModuleId>[0-9]+)\)/;
547
+ var ASYNC_MODULE_RESOLVE_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<inner>parentImport|e)=>Promise\.resolve\(\)\.then\(\(\)=>\k<inner>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
548
+ 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]+)\)\)\)\}/;
549
+ var NUMERIC_MODULE_ID_RE = /^[0-9]+$/;
550
+
457
551
  // src/shared/client/turbopack-module.ts
458
552
  function handleTurbopackModule(bundle, moduleId, id) {
459
553
  const self = globalThis;
460
554
  const bundleKey = getBundleKey(bundle);
461
- const modules = self[`TURBOPACK_${bundleKey}`];
462
- const moduleInit = findModuleInit(modules, moduleId);
463
- const exports = {};
464
- const moduleExports = { exports };
555
+ let modules = self[`TURBOPACK_${bundleKey}`];
556
+ if (modules && typeof modules === "object" && "__chunks__" in modules) {
557
+ const chunks = modules.__chunks__;
558
+ modules = chunks.flat();
559
+ }
465
560
  if (!self.__remote_components_turbopack_modules__) {
466
561
  self.__remote_components_turbopack_modules__ = {};
467
562
  }
@@ -471,6 +566,12 @@ function handleTurbopackModule(bundle, moduleId, id) {
471
566
  if (self.__remote_components_turbopack_modules__[bundle][moduleId]) {
472
567
  return self.__remote_components_turbopack_modules__[bundle][moduleId];
473
568
  }
569
+ if (!modules) {
570
+ logError("TurbopackModule", `TURBOPACK_${bundleKey} is undefined`);
571
+ }
572
+ const moduleInit = findModuleInit(modules, moduleId);
573
+ const exports = {};
574
+ const moduleExports = { exports };
474
575
  if (typeof moduleInit !== "function") {
475
576
  throw new Error(
476
577
  `Module ${id} not found in bundle ${bundle} with id ${moduleId}`
@@ -483,7 +584,7 @@ function handleTurbopackModule(bundle, moduleId, id) {
483
584
  if (!self.__remote_components_turbopack_global__[bundle]) {
484
585
  self.__remote_components_turbopack_global__[bundle] = {};
485
586
  }
486
- const allModules = modules?.flat() ?? [];
587
+ const allModules = Array.isArray(modules) ? modules.flat() : modules ? Object.values(modules) : [];
487
588
  moduleInit(
488
589
  createTurbopackContext(
489
590
  bundle,
@@ -503,9 +604,26 @@ function handleTurbopackModule(bundle, moduleId, id) {
503
604
  return moduleExports.exports;
504
605
  }
505
606
  function findModuleInit(modules, moduleId) {
607
+ if (modules && !Array.isArray(modules) && typeof modules === "object") {
608
+ const normalizedId = NUMERIC_MODULE_ID_RE.test(moduleId) ? Number(moduleId) : moduleId;
609
+ if (normalizedId in modules) {
610
+ return modules[normalizedId];
611
+ }
612
+ if (typeof normalizedId === "number" && String(normalizedId) in modules) {
613
+ return modules[String(normalizedId)];
614
+ }
615
+ const matchingKey = Object.keys(modules).find(
616
+ (key) => typeof key === "string" && key.includes(String(moduleId))
617
+ );
618
+ if (matchingKey) {
619
+ return modules[matchingKey];
620
+ }
621
+ logError("TurbopackModule", `No match found for module ID: ${moduleId}`);
622
+ return void 0;
623
+ }
506
624
  const allModules = modules?.flat() ?? [];
507
625
  if (typeof allModules[1] === "string" || typeof allModules[1] === "number") {
508
- const normalizedId = /^[0-9]+$/.test(moduleId) ? Number(moduleId) : moduleId;
626
+ const normalizedId = NUMERIC_MODULE_ID_RE.test(moduleId) ? Number(moduleId) : moduleId;
509
627
  let moduleIdIndex = allModules.indexOf(normalizedId);
510
628
  if (moduleIdIndex === -1) {
511
629
  moduleIdIndex = allModules.findIndex(
@@ -671,13 +789,6 @@ function createTurbopackContext(bundle, exports, moduleExports, allModules, modu
671
789
  };
672
790
  }
673
791
 
674
- // src/shared/client/turbopack-patterns.ts
675
- var REMOTE_SHARED_MARKER_RE = /(?:self|[a-z])\.TURBOPACK_REMOTE_SHARED/;
676
- var REMOTE_SHARED_ASSIGNMENT_RE = /\.TURBOPACK_REMOTE_SHARED=await (?:__turbopack_context__|e)\.A\((?<sharedModuleId>[0-9]+)\)/;
677
- var ASYNC_MODULE_LOADER_RE = /(?:__turbopack_context__|e)\.A\((?<asyncSharedModuleId>[0-9]+)\)/;
678
- var ASYNC_MODULE_RESOLVE_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<inner>parentImport|e)=>Promise\.resolve\(\)\.then\(\(\)=>\k<inner>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
679
- 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]+)\)\)\)\}/;
680
-
681
792
  // src/shared/client/shared-modules.ts
682
793
  async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {}) {
683
794
  const self = globalThis;
@@ -686,10 +797,14 @@ async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {
686
797
  self.__remote_shared_modules__[bundle] = {};
687
798
  }
688
799
  const bundleKey = getBundleKey(bundle);
689
- const modules = self[`TURBOPACK_${bundleKey}`];
800
+ let modules = self[`TURBOPACK_${bundleKey}`];
801
+ if (modules && typeof modules === "object" && "__chunks__" in modules) {
802
+ const chunks = modules.__chunks__;
803
+ modules = chunks.flat();
804
+ }
690
805
  let sharedModuleInitializer = null;
691
- if (modules && Array.isArray(modules)) {
692
- const allModules = modules.flat();
806
+ if (modules) {
807
+ const allModules = Array.isArray(modules) ? modules.flat() : Object.entries(modules).flat();
693
808
  const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {
694
809
  if (typeof idOrFunc !== "function") {
695
810
  return false;
@@ -722,6 +837,11 @@ async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {
722
837
  if (self.__remote_shared_modules__?.[bundle]) {
723
838
  if (hostShared[module]) {
724
839
  self.__remote_shared_modules__[bundle][id] = await hostShared[module](bundle);
840
+ } else {
841
+ logError(
842
+ "SharedModules",
843
+ `Host shared module "${module}" not found for ID ${id}`
844
+ );
725
845
  }
726
846
  }
727
847
  })
@@ -732,9 +852,13 @@ async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {
732
852
  Object.entries(remoteShared).map(async ([id, module]) => {
733
853
  if (self.__remote_shared_modules__?.[bundle]) {
734
854
  if (hostShared[module]) {
735
- self.__remote_shared_modules__[bundle][id.replace("[app-ssr]", "[app-client]")] = await hostShared[module](bundle);
855
+ const normalizedId = id.replace("[app-ssr]", "[app-client]");
856
+ self.__remote_shared_modules__[bundle][normalizedId] = await hostShared[module](bundle);
736
857
  } else {
737
- console.error(`Shared module "${module}" not found for "${bundle}".`);
858
+ logError(
859
+ "SharedModules",
860
+ `Shared module "${module}" not found for "${bundle}"`
861
+ );
738
862
  }
739
863
  }
740
864
  })
@@ -746,7 +870,15 @@ function extractSharedModuleIds(shared, bundleKey, self) {
746
870
  if (asyncSharedModuleId) {
747
871
  const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);
748
872
  let asyncSharedModule;
749
- const newAllModules = self[`TURBOPACK_${bundleKey}`]?.flat() ?? [];
873
+ let turbopackModules = self[`TURBOPACK_${bundleKey}`];
874
+ if (turbopackModules && typeof turbopackModules === "object" && "__chunks__" in turbopackModules) {
875
+ const chunks = turbopackModules.__chunks__;
876
+ turbopackModules = chunks.flat();
877
+ }
878
+ const newAllModules = Array.isArray(turbopackModules) ? turbopackModules.flat() : turbopackModules ? Object.entries(turbopackModules).flatMap(([key2, value2]) => [
879
+ key2,
880
+ value2
881
+ ]) : [];
750
882
  const asyncSharedModuleIdIndex = newAllModules.indexOf(
751
883
  asyncSharedModuleIdNumber
752
884
  );
@@ -1110,11 +1242,10 @@ async function loadStaticRemoteComponent(scripts, url) {
1110
1242
  unmount: mod.unmount || mod.default?.unmount
1111
1243
  };
1112
1244
  } catch (e) {
1113
- console.error(
1114
- new RemoteComponentsError(
1115
- `Error loading remote component script from "${script.src || url.href}".`,
1116
- { cause: e }
1117
- )
1245
+ logError(
1246
+ "StaticLoader",
1247
+ `Error loading remote component script from "${script.src || url.href}".`,
1248
+ e
1118
1249
  );
1119
1250
  return {
1120
1251
  mount: void 0,