remote-components 0.0.50 → 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 +330 -166
  2. package/dist/html/host.cjs.map +1 -1
  3. package/dist/html/host.js +330 -166
  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 +190 -100
  20. package/dist/internal/shared/client/remote-component.cjs.map +1 -1
  21. package/dist/internal/shared/client/remote-component.js +190 -100
  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 +202 -107
  42. package/dist/next/host/client/index.cjs.map +1 -1
  43. package/dist/next/host/client/index.js +202 -107
  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 +202 -107
  56. package/dist/react/index.cjs.map +1 -1
  57. package/dist/react/index.js +202 -107
  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
package/dist/html/host.js CHANGED
@@ -248,6 +248,121 @@ var init_shared_modules = __esm({
248
248
  }
249
249
  });
250
250
 
251
+ // src/shared/constants.ts
252
+ var RC_PROTECTED_REMOTE_FETCH_PATHNAME;
253
+ var init_constants = __esm({
254
+ "src/shared/constants.ts"() {
255
+ "use strict";
256
+ RC_PROTECTED_REMOTE_FETCH_PATHNAME = "/rc-fetch-protected-remote";
257
+ }
258
+ });
259
+
260
+ // src/shared/client/protected-rc-fallback.ts
261
+ function generateProtectedRcFallbackSrc(url) {
262
+ return `${RC_PROTECTED_REMOTE_FETCH_PATHNAME}?url=${encodeURIComponent(url)}`;
263
+ }
264
+ var init_protected_rc_fallback = __esm({
265
+ "src/shared/client/protected-rc-fallback.ts"() {
266
+ "use strict";
267
+ init_constants();
268
+ }
269
+ });
270
+
271
+ // src/shared/client/webpack-patterns.ts
272
+ var NEXT_BUNDLE_PATH_RE;
273
+ var init_webpack_patterns = __esm({
274
+ "src/shared/client/webpack-patterns.ts"() {
275
+ "use strict";
276
+ NEXT_BUNDLE_PATH_RE = /\/_next\/\[.+\](?:%20| )/;
277
+ }
278
+ });
279
+
280
+ // src/shared/client/script-loader.ts
281
+ async function loadScripts(scripts) {
282
+ await Promise.all(
283
+ scripts.map((script) => {
284
+ return new Promise((resolve, reject) => {
285
+ const newSrc = new URL(
286
+ // remove the remote component bundle name identifier from the script src
287
+ script.src.replace(NEXT_BUNDLE_PATH_RE, "/_next/"),
288
+ location.origin
289
+ ).href;
290
+ const loadScriptWithProtectedRcFallback = (src, isFallback = false) => {
291
+ const newScript = document.createElement("script");
292
+ newScript.onload = () => {
293
+ if (isFallback) {
294
+ logInfo(
295
+ "ScriptLoader",
296
+ `Successfully loaded <script src="${newSrc}"> using fallback.`
297
+ );
298
+ }
299
+ resolve();
300
+ };
301
+ newScript.onerror = () => {
302
+ if (!isFallback) {
303
+ const fallbackSrc = generateProtectedRcFallbackSrc(newSrc);
304
+ logWarn(
305
+ "ScriptLoader",
306
+ `Failed to load <script src="${newSrc}"> for Remote Component. Trying fallback with ${RC_PROTECTED_REMOTE_FETCH_PATHNAME} (withRemoteComponentsHost)...`
307
+ );
308
+ loadScriptWithProtectedRcFallback(fallbackSrc, true);
309
+ } else {
310
+ logError(
311
+ "ScriptLoader",
312
+ `Failed to load fallback for <script src="${newSrc}"> for Remote Component.`
313
+ );
314
+ reject(
315
+ new RemoteComponentsError(
316
+ `Failed to load <script src="${newSrc}"> for Remote Component. Check the URL is correct.`
317
+ )
318
+ );
319
+ }
320
+ };
321
+ newScript.src = src;
322
+ newScript.async = true;
323
+ document.head.appendChild(newScript);
324
+ };
325
+ loadScriptWithProtectedRcFallback(newSrc);
326
+ });
327
+ })
328
+ );
329
+ }
330
+ var init_script_loader = __esm({
331
+ "src/shared/client/script-loader.ts"() {
332
+ "use strict";
333
+ init_constants();
334
+ init_error();
335
+ init_logger();
336
+ init_protected_rc_fallback();
337
+ init_webpack_patterns();
338
+ }
339
+ });
340
+
341
+ // src/shared/utils/index.ts
342
+ function escapeString(str) {
343
+ return str.replace(/[^a-z0-9]/g, "_");
344
+ }
345
+ var init_utils = __esm({
346
+ "src/shared/utils/index.ts"() {
347
+ "use strict";
348
+ }
349
+ });
350
+
351
+ // src/shared/client/const.ts
352
+ function getBundleKey(bundle) {
353
+ return escapeString(bundle);
354
+ }
355
+ var RUNTIME_WEBPACK, RUNTIME_TURBOPACK, REMOTE_COMPONENT_REGEX;
356
+ var init_const = __esm({
357
+ "src/shared/client/const.ts"() {
358
+ "use strict";
359
+ init_utils();
360
+ RUNTIME_WEBPACK = "webpack";
361
+ RUNTIME_TURBOPACK = "turbopack";
362
+ REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
363
+ }
364
+ });
365
+
251
366
  // src/shared/utils/abort.ts
252
367
  function isAbortError(error) {
253
368
  if (error instanceof DOMException && error.name === "AbortError") {
@@ -265,7 +380,7 @@ var init_abort = __esm({
265
380
  }
266
381
  });
267
382
 
268
- // src/shared/ssr/fetch-with-protected-rc-fallback.ts
383
+ // src/shared/client/fetch-with-protected-rc-fallback.ts
269
384
  async function fetchWithProtectedRcFallback(url, init) {
270
385
  try {
271
386
  const res = await fetch(url, init);
@@ -274,16 +389,21 @@ async function fetchWithProtectedRcFallback(url, init) {
274
389
  if (isAbortError(error)) {
275
390
  throw error;
276
391
  }
277
- if (typeof document === "object" && typeof document.location === "object" && document.location.origin !== new URL(url).origin) {
278
- logInfo(
392
+ const parsedUrl = new URL(url);
393
+ if (typeof document === "object" && typeof document.location === "object" && document.location.origin !== parsedUrl.origin) {
394
+ logWarn(
279
395
  "FetchRemoteComponent",
280
396
  "Request failed due to CORS, attempting to fetch it via the withRemoteComponentsHost proxy."
281
397
  );
282
398
  const proxiedRes = await fetch(
283
- `${RC_PROTECTED_REMOTE_FETCH_PATHNAME}?url=${url}`,
399
+ generateProtectedRcFallbackSrc(parsedUrl.href),
284
400
  init?.signal ? { signal: init.signal } : void 0
285
401
  );
286
402
  if (proxiedRes.status === 200) {
403
+ logInfo(
404
+ "FetchRemoteComponent",
405
+ `Successfully fetched ${parsedUrl.href} with fallback withRemoteComponentsHost proxy`
406
+ );
287
407
  return proxiedRes;
288
408
  } else {
289
409
  logError(
@@ -295,43 +415,17 @@ async function fetchWithProtectedRcFallback(url, init) {
295
415
  throw error;
296
416
  }
297
417
  }
298
- var RC_PROTECTED_REMOTE_FETCH_PATHNAME;
299
418
  var init_fetch_with_protected_rc_fallback = __esm({
300
- "src/shared/ssr/fetch-with-protected-rc-fallback.ts"() {
419
+ "src/shared/client/fetch-with-protected-rc-fallback.ts"() {
301
420
  "use strict";
421
+ init_protected_rc_fallback();
302
422
  init_abort();
303
423
  init_logger();
304
- RC_PROTECTED_REMOTE_FETCH_PATHNAME = "/rc-fetch-protected-remote";
305
- }
306
- });
307
-
308
- // src/shared/utils/index.ts
309
- function escapeString(str) {
310
- return str.replace(/[^a-z0-9]/g, "_");
311
- }
312
- var init_utils = __esm({
313
- "src/shared/utils/index.ts"() {
314
- "use strict";
315
- }
316
- });
317
-
318
- // src/shared/client/const.ts
319
- function getBundleKey(bundle) {
320
- return escapeString(bundle);
321
- }
322
- var RUNTIME_WEBPACK, RUNTIME_TURBOPACK, REMOTE_COMPONENT_REGEX;
323
- var init_const = __esm({
324
- "src/shared/client/const.ts"() {
325
- "use strict";
326
- init_utils();
327
- RUNTIME_WEBPACK = "webpack";
328
- RUNTIME_TURBOPACK = "turbopack";
329
- REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
330
424
  }
331
425
  });
332
426
 
333
427
  // src/shared/client/turbopack-patterns.ts
334
- 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, TURBOPACK_GLOBAL_RE;
428
+ var REMOTE_SHARED_MARKER_RE, REMOTE_SHARED_ASSIGNMENT_RE, ASYNC_MODULE_LOADER_RE, ASYNC_MODULE_RESOLVE_RE, ASYNC_MODULE_ALL_RE, TURBOPACK_GLOBAL_RE;
335
429
  var init_turbopack_patterns = __esm({
336
430
  "src/shared/client/turbopack-patterns.ts"() {
337
431
  "use strict";
@@ -340,7 +434,6 @@ var init_turbopack_patterns = __esm({
340
434
  ASYNC_MODULE_LOADER_RE = /(?:__turbopack_context__|e)\.A\((?<asyncSharedModuleId>[0-9]+)\)/;
341
435
  ASYNC_MODULE_RESOLVE_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<inner>parentImport|e)=>Promise\.resolve\(\)\.then\(\(\)=>\k<inner>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
342
436
  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]+)\)\)\)\}/;
343
- NUMERIC_MODULE_ID_RE = /^[0-9]+$/;
344
437
  TURBOPACK_GLOBAL_RE = /(?:globalThis|self)\s*(?:\.TURBOPACK|\[\s*["']TURBOPACK["']\s*\])/;
345
438
  }
346
439
  });
@@ -547,9 +640,9 @@ var init_chunk_loader = __esm({
547
640
  "src/shared/client/chunk-loader.ts"() {
548
641
  "use strict";
549
642
  init_error();
550
- init_fetch_with_protected_rc_fallback();
551
643
  init_logger();
552
644
  init_const();
645
+ init_fetch_with_protected_rc_fallback();
553
646
  init_turbopack_patterns();
554
647
  }
555
648
  });
@@ -558,10 +651,14 @@ var init_chunk_loader = __esm({
558
651
  function handleTurbopackModule(bundle, moduleId, id) {
559
652
  const self = globalThis;
560
653
  const bundleKey = getBundleKey(bundle);
561
- let modules = self[`TURBOPACK_${bundleKey}`];
562
- if (modules && typeof modules === "object" && "__chunks__" in modules) {
563
- const chunks = modules.__chunks__;
564
- modules = chunks.flat();
654
+ const raw = self[`TURBOPACK_${bundleKey}`];
655
+ let modules;
656
+ if (raw && typeof raw === "object" && "__chunks__" in raw) {
657
+ modules = raw.__chunks__.flat();
658
+ } else if (Array.isArray(raw)) {
659
+ modules = raw.flat();
660
+ } else {
661
+ modules = raw;
565
662
  }
566
663
  if (!self.__remote_components_turbopack_modules__) {
567
664
  self.__remote_components_turbopack_modules__ = {};
@@ -590,13 +687,12 @@ function handleTurbopackModule(bundle, moduleId, id) {
590
687
  if (!self.__remote_components_turbopack_global__[bundle]) {
591
688
  self.__remote_components_turbopack_global__[bundle] = {};
592
689
  }
593
- const allModules = Array.isArray(modules) ? modules.flat() : modules ? Object.values(modules) : [];
594
690
  moduleInit(
595
691
  createTurbopackContext(
596
692
  bundle,
597
693
  exports,
598
694
  moduleExports,
599
- allModules,
695
+ modules,
600
696
  moduleInit,
601
697
  id,
602
698
  self
@@ -610,46 +706,27 @@ function handleTurbopackModule(bundle, moduleId, id) {
610
706
  return moduleExports.exports;
611
707
  }
612
708
  function findModuleInit(modules, moduleId) {
613
- if (modules && !Array.isArray(modules) && typeof modules === "object") {
614
- const normalizedId = NUMERIC_MODULE_ID_RE.test(moduleId) ? Number(moduleId) : moduleId;
615
- if (normalizedId in modules) {
616
- return modules[normalizedId];
617
- }
618
- if (typeof normalizedId === "number" && String(normalizedId) in modules) {
619
- return modules[String(normalizedId)];
620
- }
621
- const matchingKey = Object.keys(modules).find(
622
- (key) => typeof key === "string" && key.includes(String(moduleId))
709
+ if (!modules || typeof modules !== "object")
710
+ return;
711
+ if (!Array.isArray(modules)) {
712
+ const key = moduleId in modules ? moduleId : Object.keys(modules).find((k) => k.startsWith(moduleId));
713
+ return key !== void 0 ? modules[key] : void 0;
714
+ }
715
+ const flat = modules.flat();
716
+ let idx = flat.findIndex((e) => String(e) === String(moduleId));
717
+ if (idx < 0) {
718
+ idx = flat.findIndex(
719
+ (e) => typeof e === "string" && e.startsWith(moduleId)
623
720
  );
624
- if (matchingKey) {
625
- return modules[matchingKey];
626
- }
627
- logError("TurbopackModule", `No match found for module ID: ${moduleId}`);
628
- return void 0;
629
- }
630
- const allModules = modules?.flat() ?? [];
631
- if (typeof allModules[1] === "string" || typeof allModules[1] === "number") {
632
- const normalizedId = NUMERIC_MODULE_ID_RE.test(moduleId) ? Number(moduleId) : moduleId;
633
- let moduleIdIndex = allModules.indexOf(normalizedId);
634
- if (moduleIdIndex === -1) {
635
- moduleIdIndex = allModules.findIndex(
636
- (bundleEntry) => typeof bundleEntry === "string" && bundleEntry.startsWith(moduleId) || bundleEntry === normalizedId
637
- );
638
- }
639
- if (moduleIdIndex !== -1) {
640
- while (typeof allModules[moduleIdIndex] !== "function" && moduleIdIndex < allModules.length) {
641
- moduleIdIndex++;
642
- }
643
- return allModules[moduleIdIndex];
644
- }
645
- } else {
646
- return allModules.find(
647
- (bundleEntry) => typeof bundleEntry === "object" && bundleEntry !== null && moduleId in bundleEntry
648
- )?.[moduleId];
649
721
  }
650
- return void 0;
722
+ if (idx >= 0) {
723
+ return flat.slice(idx + 1).find((e) => typeof e === "function");
724
+ }
725
+ return flat.find(
726
+ (e) => Boolean(e && typeof e === "object" && moduleId in e)
727
+ )?.[moduleId];
651
728
  }
652
- function createTurbopackContext(bundle, exports, moduleExports, allModules, moduleInit, id, self) {
729
+ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleInit, id, self) {
653
730
  return {
654
731
  // HMR not implemented for Remote Components
655
732
  k: {
@@ -770,13 +847,17 @@ function createTurbopackContext(bundle, exports, moduleExports, allModules, modu
770
847
  (parentId) => self.__webpack_require__?.(`[${bundle}] ${parentId}`)
771
848
  );
772
849
  },
850
+ // dynamic import tracking — no-op for remote components
851
+ j() {
852
+ },
773
853
  // chunk loader
774
854
  l(url) {
775
- const moduleInitIndex = allModules.indexOf(moduleInit);
855
+ const flatModules = Array.isArray(modules) ? modules : [];
856
+ const moduleInitIndex = flatModules.indexOf(moduleInit);
776
857
  if (moduleInitIndex !== -1) {
777
- const scriptIndex = allModules.slice(0, moduleInitIndex).findLastIndex((bundleEntry) => bundleEntry instanceof Element);
858
+ const scriptIndex = flatModules.slice(0, moduleInitIndex).findLastIndex((bundleEntry) => bundleEntry instanceof Element);
778
859
  if (scriptIndex !== -1) {
779
- const script = allModules[scriptIndex];
860
+ const script = flatModules[scriptIndex];
780
861
  const scriptSrc = script.getAttribute("data-turbopack-src") || "";
781
862
  const nextIndex = scriptSrc.indexOf("/_next");
782
863
  const baseUrl = nextIndex !== -1 ? scriptSrc.slice(0, nextIndex) : "";
@@ -799,7 +880,6 @@ var init_turbopack_module = __esm({
799
880
  "use strict";
800
881
  init_logger();
801
882
  init_const();
802
- init_turbopack_patterns();
803
883
  }
804
884
  });
805
885
 
@@ -1039,6 +1119,89 @@ var init_webpack_adapter = __esm({
1039
1119
  });
1040
1120
 
1041
1121
  // src/shared/client/static-loader.ts
1122
+ async function importViaProxy(absoluteSrc) {
1123
+ const proxyUrl = new URL(
1124
+ generateProtectedRcFallbackSrc(absoluteSrc),
1125
+ location.href
1126
+ ).href;
1127
+ const response = await fetch(proxyUrl);
1128
+ if (!response.ok)
1129
+ throw new Error(`Proxy fetch failed: ${response.status}`);
1130
+ logInfo(
1131
+ "StaticLoader",
1132
+ `Successfully loaded ${absoluteSrc} via protected RC proxy fallback.`
1133
+ );
1134
+ const content = (await response.text()).replace(/import\.meta\.url/g, JSON.stringify(absoluteSrc)).replace(
1135
+ /\b(from|import)\s*(["'])(\.\.?\/[^"']+)\2/g,
1136
+ (_, keyword, quote, relativePath) => {
1137
+ const absoluteImportUrl = new URL(relativePath, absoluteSrc).href;
1138
+ const absoluteProxyUrl = new URL(
1139
+ generateProtectedRcFallbackSrc(absoluteImportUrl),
1140
+ location.href
1141
+ ).href;
1142
+ return `${keyword} ${quote}${absoluteProxyUrl}${quote}`;
1143
+ }
1144
+ );
1145
+ const moduleBlobUrl = URL.createObjectURL(
1146
+ new Blob([content], { type: "text/javascript" })
1147
+ );
1148
+ const wrapperContent = [
1149
+ `import*as m from${JSON.stringify(moduleBlobUrl)};`,
1150
+ `globalThis.__rc_module_registry__=globalThis.__rc_module_registry__||{};`,
1151
+ `globalThis.__rc_module_registry__[${JSON.stringify(absoluteSrc)}]=m;`
1152
+ ].join("");
1153
+ const wrapperBlobUrl = URL.createObjectURL(
1154
+ new Blob([wrapperContent], { type: "text/javascript" })
1155
+ );
1156
+ const scriptEl = document.createElement("script");
1157
+ scriptEl.type = "module";
1158
+ scriptEl.src = wrapperBlobUrl;
1159
+ try {
1160
+ await new Promise((resolve, reject) => {
1161
+ scriptEl.onload = () => resolve();
1162
+ scriptEl.onerror = () => reject(new Error(`Failed to load module for ${absoluteSrc}`));
1163
+ document.head.appendChild(scriptEl);
1164
+ });
1165
+ } finally {
1166
+ scriptEl.remove();
1167
+ URL.revokeObjectURL(moduleBlobUrl);
1168
+ URL.revokeObjectURL(wrapperBlobUrl);
1169
+ }
1170
+ const registry = globalThis.__rc_module_registry__;
1171
+ const mod = registry?.[absoluteSrc] ?? {};
1172
+ if (registry)
1173
+ delete registry[absoluteSrc];
1174
+ return mod;
1175
+ }
1176
+ function resolveScriptSrc(script, url) {
1177
+ const rawSrc = typeof script.getAttribute === "function" ? script.getAttribute("src") ?? script.src : script.src;
1178
+ if (!rawSrc && script.textContent) {
1179
+ return URL.createObjectURL(
1180
+ new Blob(
1181
+ [script.textContent.replace(/import\.meta\.url/g, JSON.stringify(url))],
1182
+ { type: "text/javascript" }
1183
+ )
1184
+ );
1185
+ }
1186
+ return rawSrc;
1187
+ }
1188
+ async function importScriptMod(absoluteSrc) {
1189
+ try {
1190
+ return await import(
1191
+ /* @vite-ignore */
1192
+ /* webpackIgnore: true */
1193
+ absoluteSrc
1194
+ );
1195
+ } catch (importError) {
1196
+ if (absoluteSrc.startsWith("blob:"))
1197
+ throw importError;
1198
+ logWarn(
1199
+ "StaticLoader",
1200
+ `Direct import of ${absoluteSrc} failed, attempting via protected RC proxy fallback.`
1201
+ );
1202
+ return importViaProxy(absoluteSrc);
1203
+ }
1204
+ }
1042
1205
  async function loadStaticRemoteComponent(scripts, url) {
1043
1206
  const self = globalThis;
1044
1207
  if (self.__remote_script_entrypoint_mount__?.[url.href]) {
@@ -1050,26 +1213,9 @@ async function loadStaticRemoteComponent(scripts, url) {
1050
1213
  const mountUnmountSets = await Promise.all(
1051
1214
  scripts.map(async (script) => {
1052
1215
  try {
1053
- let src = typeof script.getAttribute === "function" ? script.getAttribute("src") ?? script.src : script.src;
1054
- if (!src && script.textContent) {
1055
- const blob = new Blob(
1056
- [
1057
- script.textContent.replace(
1058
- /import\.meta\.url/g,
1059
- JSON.stringify(url)
1060
- )
1061
- ],
1062
- {
1063
- type: "text/javascript"
1064
- }
1065
- );
1066
- src = URL.createObjectURL(blob);
1067
- }
1068
- const mod = await import(
1069
- /* @vite-ignore */
1070
- /* webpackIgnore: true */
1071
- new URL(src, url).href
1072
- );
1216
+ const src = resolveScriptSrc(script, url);
1217
+ const absoluteSrc = new URL(src, url).href;
1218
+ const mod = await importScriptMod(absoluteSrc);
1073
1219
  if (src.startsWith("blob:")) {
1074
1220
  URL.revokeObjectURL(src);
1075
1221
  }
@@ -1134,6 +1280,7 @@ var init_static_loader = __esm({
1134
1280
  "src/shared/client/static-loader.ts"() {
1135
1281
  "use strict";
1136
1282
  init_logger();
1283
+ init_protected_rc_fallback();
1137
1284
  }
1138
1285
  });
1139
1286
 
@@ -1377,35 +1524,18 @@ async function webpackRuntime(bundle, shared, remoteShared) {
1377
1524
  default: { createFromReadableStream }
1378
1525
  } = await import("react-server-dom-webpack/client.browser");
1379
1526
  async function preloadScripts(scripts, url, remoteBundle, _) {
1380
- await Promise.all(
1381
- scripts.map((script) => {
1382
- return new Promise((resolve2, reject) => {
1383
- const newScript = document.createElement("script");
1384
- newScript.onload = () => {
1385
- resolve2();
1386
- newScript.remove();
1387
- };
1388
- newScript.onerror = () => {
1389
- reject(
1390
- new RemoteComponentsError(
1391
- `Failed to load <script src="${script.src}"> for Remote Component. Check the URL is correct.`
1392
- )
1393
- );
1394
- newScript.remove();
1395
- };
1396
- const scriptSrc = script.getAttribute("src") || script.getAttribute("data-src");
1397
- if (scriptSrc) {
1398
- newScript.src = new URL(
1399
- scriptSrc.replace(/\/_next\/\[(?:.+)\](?:%20| )/, "/_next/"),
1400
- url
1401
- ).href;
1402
- }
1403
- newScript.async = true;
1404
- document.body.appendChild(newScript);
1405
- script.parentElement?.removeChild(script);
1406
- });
1407
- })
1408
- );
1527
+ const scriptSrcs = scripts.flatMap((script) => {
1528
+ const scriptSrc = script.getAttribute("src") || script.getAttribute("data-src");
1529
+ script.parentElement?.removeChild(script);
1530
+ if (!scriptSrc)
1531
+ return [];
1532
+ return [
1533
+ {
1534
+ src: new URL(scriptSrc.replace(NEXT_BUNDLE_PATH_RE, "/_next/"), url).href
1535
+ }
1536
+ ];
1537
+ });
1538
+ await loadScripts(scriptSrcs);
1409
1539
  const hostShared = {
1410
1540
  ...sharedPolyfills(shared),
1411
1541
  ...self.__remote_component_host_shared_modules__,
@@ -1449,6 +1579,8 @@ var init_webpack = __esm({
1449
1579
  "src/html/host/runtime/webpack.ts"() {
1450
1580
  "use strict";
1451
1581
  init_polyfill();
1582
+ init_script_loader();
1583
+ init_webpack_patterns();
1452
1584
  init_error();
1453
1585
  init_next_client_pages_loader();
1454
1586
  init_shared_modules();
@@ -1675,20 +1807,22 @@ function createRSCStream(rscName, data) {
1675
1807
  });
1676
1808
  }
1677
1809
 
1678
- // src/shared/client/script-loader.ts
1679
- init_error();
1680
-
1681
1810
  // src/shared/client/component-loader.ts
1811
+ init_script_loader();
1682
1812
  init_webpack_adapter();
1683
1813
 
1684
1814
  // src/shared/client/remote-component.ts
1685
1815
  init_const();
1816
+ init_script_loader();
1686
1817
  init_static_loader();
1687
1818
  init_webpack_adapter();
1688
1819
 
1689
1820
  // src/html/host/index.tsx
1690
1821
  init_error();
1691
1822
 
1823
+ // src/shared/ssr/fetch-with-hooks.ts
1824
+ init_fetch_with_protected_rc_fallback();
1825
+
1692
1826
  // src/shared/ssr/fetch-headers.ts
1693
1827
  function remoteFetchHeaders() {
1694
1828
  return {
@@ -1706,7 +1840,6 @@ function remoteFetchHeaders() {
1706
1840
  }
1707
1841
 
1708
1842
  // src/shared/ssr/fetch-with-hooks.ts
1709
- init_fetch_with_protected_rc_fallback();
1710
1843
  async function fetchWithHooks(url, additionalInit, options = {}) {
1711
1844
  const {
1712
1845
  onRequest,
@@ -1872,7 +2005,6 @@ if (typeof HTMLElement !== "undefined") {
1872
2005
  if (this.getAttribute("src")) {
1873
2006
  this.load().catch((e) => {
1874
2007
  if (isAbortError(e)) {
1875
- this.isLoading = false;
1876
2008
  return;
1877
2009
  }
1878
2010
  logError("HtmlHost", "Error loading remote component.", e);
@@ -1898,7 +2030,6 @@ if (typeof HTMLElement !== "undefined") {
1898
2030
  this.root = newRoot;
1899
2031
  this.load().catch((e) => {
1900
2032
  if (isAbortError(e)) {
1901
- this.isLoading = false;
1902
2033
  return;
1903
2034
  }
1904
2035
  logError("HtmlHost", "Error reloading remote component.", e);
@@ -1921,7 +2052,14 @@ if (typeof HTMLElement !== "undefined") {
1921
2052
  });
1922
2053
  });
1923
2054
  if (this.isLoading) {
1924
- return;
2055
+ this.abortController?.abort();
2056
+ this.isLoading = false;
2057
+ if (this.root && !this.reactRoot) {
2058
+ this.root.innerHTML = "";
2059
+ this.fouc = null;
2060
+ this.fallbackSlot = document.createElement("slot");
2061
+ this.root.appendChild(this.fallbackSlot);
2062
+ }
1925
2063
  }
1926
2064
  if (!this.root) {
1927
2065
  this.root = this.attachShadow({
@@ -1932,28 +2070,16 @@ if (typeof HTMLElement !== "undefined") {
1932
2070
  }
1933
2071
  this.name = this.getAttribute("name") || "__vercel_remote_component";
1934
2072
  this.bundle = "default";
1935
- if (this.hasAttribute("src") || this.querySelector("div#__REMOTE_COMPONENT__") || this.hasAttribute("data-ssr")) {
1936
- this.load().catch((e) => {
1937
- if (isAbortError(e)) {
1938
- this.isLoading = false;
1939
- return;
1940
- }
1941
- logError("HtmlHost", "Error loading remote component.", e);
1942
- const errorEvent = new Event("error", {
1943
- bubbles: true,
1944
- composed: true
1945
- });
1946
- Object.assign(errorEvent, {
1947
- error: e,
1948
- src: this.getAttribute("src")
1949
- });
1950
- this.dispatchEvent(errorEvent);
1951
- });
1952
- }
1953
2073
  this.isLoading = true;
1954
2074
  const src = this.getAttribute("src");
1955
2075
  this.abortController = new AbortController();
1956
2076
  const signal = this.abortController.signal;
2077
+ const isCurrentLoad = () => !signal.aborted && this.getAttribute("src") === src;
2078
+ const abandonLoad = () => {
2079
+ if (this.abortController?.signal === signal && this.isLoading) {
2080
+ this.isLoading = false;
2081
+ }
2082
+ };
1957
2083
  const beforeLoadEvent = new Event("beforeload", {
1958
2084
  bubbles: true,
1959
2085
  composed: true
@@ -1976,11 +2102,19 @@ if (typeof HTMLElement !== "undefined") {
1976
2102
  const fetchInit = {
1977
2103
  credentials: this.getAttribute("credentials") || "same-origin"
1978
2104
  };
1979
- const res = await fetchWithHooks(url, fetchInit, {
1980
- onRequest: this.onRequest,
1981
- onResponse: this.onResponse,
1982
- abortController: this.abortController
1983
- });
2105
+ let res;
2106
+ try {
2107
+ res = await fetchWithHooks(url, fetchInit, {
2108
+ onRequest: this.onRequest,
2109
+ onResponse: this.onResponse,
2110
+ abortController: this.abortController
2111
+ });
2112
+ } catch (e) {
2113
+ if (isAbortError(e)) {
2114
+ return abandonLoad();
2115
+ }
2116
+ throw e;
2117
+ }
1984
2118
  if (!res || !res.ok) {
1985
2119
  let error = failedToFetchRemoteComponentError(
1986
2120
  url.href,
@@ -2011,7 +2145,17 @@ if (typeof HTMLElement !== "undefined") {
2011
2145
  }
2012
2146
  throw error;
2013
2147
  }
2014
- html = await res.text();
2148
+ try {
2149
+ html = await res.text();
2150
+ } catch (e) {
2151
+ if (isAbortError(e)) {
2152
+ return abandonLoad();
2153
+ }
2154
+ throw e;
2155
+ }
2156
+ if (!isCurrentLoad()) {
2157
+ return abandonLoad();
2158
+ }
2015
2159
  }
2016
2160
  const parser = new DOMParser();
2017
2161
  const doc = parser.parseFromString(html, "text/html");
@@ -2094,6 +2238,9 @@ if (typeof HTMLElement !== "undefined") {
2094
2238
  }
2095
2239
  })
2096
2240
  );
2241
+ if (!isCurrentLoad()) {
2242
+ return abandonLoad();
2243
+ }
2097
2244
  }
2098
2245
  }
2099
2246
  this.root.innerHTML = "";
@@ -2138,6 +2285,9 @@ if (typeof HTMLElement !== "undefined") {
2138
2285
  remoteComponentSrc,
2139
2286
  root: this.root
2140
2287
  });
2288
+ if (!isCurrentLoad()) {
2289
+ return abandonLoad();
2290
+ }
2141
2291
  }
2142
2292
  applyOriginToNodes(doc, url ?? new URL(location.href));
2143
2293
  if (!this.reactRoot) {
@@ -2221,6 +2371,9 @@ if (typeof HTMLElement !== "undefined") {
2221
2371
  },
2222
2372
  remoteShared
2223
2373
  );
2374
+ if (!isCurrentLoad()) {
2375
+ return abandonLoad();
2376
+ }
2224
2377
  const scripts = isRemoteComponent ? component.querySelectorAll("script") : doc.querySelectorAll(
2225
2378
  "script[src],script[data-src],script[data-remote-component-entrypoint]"
2226
2379
  );
@@ -2236,6 +2389,9 @@ if (typeof HTMLElement !== "undefined") {
2236
2389
  this.bundle ?? "default",
2237
2390
  this.name ?? "__vercel_remote_component"
2238
2391
  );
2392
+ if (!isCurrentLoad()) {
2393
+ return abandonLoad();
2394
+ }
2239
2395
  if (isRemoteComponent) {
2240
2396
  Array.from(component.children).forEach((child) => {
2241
2397
  if (child.tagName === "SCRIPT") {
@@ -2296,7 +2452,9 @@ if (typeof HTMLElement !== "undefined") {
2296
2452
  logError("HtmlHost", "Error attaching styles.", e);
2297
2453
  });
2298
2454
  }
2299
- this.isLoading = false;
2455
+ if (isCurrentLoad()) {
2456
+ this.isLoading = false;
2457
+ }
2300
2458
  const loadEvent = new Event("load", {
2301
2459
  bubbles: true,
2302
2460
  composed: true
@@ -2349,7 +2507,9 @@ if (typeof HTMLElement !== "undefined") {
2349
2507
  logError("HtmlHost", "Error attaching styles.", e);
2350
2508
  });
2351
2509
  }
2352
- remoteComponent.isLoading = false;
2510
+ if (isCurrentLoad()) {
2511
+ remoteComponent.isLoading = false;
2512
+ }
2353
2513
  const loadEvent = new Event("load", {
2354
2514
  bubbles: true,
2355
2515
  composed: true
@@ -2366,7 +2526,9 @@ if (typeof HTMLElement !== "undefined") {
2366
2526
  startTransition(() => {
2367
2527
  root.render(/* @__PURE__ */ jsx2(RemoteComponentFromNext, { initial: false }));
2368
2528
  doCleanup();
2369
- this.isLoading = false;
2529
+ if (isCurrentLoad()) {
2530
+ this.isLoading = false;
2531
+ }
2370
2532
  });
2371
2533
  return;
2372
2534
  }
@@ -2410,7 +2572,9 @@ if (typeof HTMLElement !== "undefined") {
2410
2572
  Object.assign(loadEvent, { src: this.getAttribute("src") });
2411
2573
  this.dispatchEvent(loadEvent);
2412
2574
  }
2413
- this.isLoading = false;
2575
+ if (isCurrentLoad()) {
2576
+ this.isLoading = false;
2577
+ }
2414
2578
  }
2415
2579
  }
2416
2580
  customElements.define("remote-component", RemoteComponent);