remote-components 0.0.51 → 0.1.0

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 (108) hide show
  1. package/dist/{component-loader-1838f572.d.ts → component-loader-76eb1b8b.d.ts} +3 -1
  2. package/dist/html/host.cjs +297 -217
  3. package/dist/html/host.cjs.map +1 -1
  4. package/dist/html/host.d.ts +2 -0
  5. package/dist/html/host.js +296 -217
  6. package/dist/html/host.js.map +1 -1
  7. package/dist/html/remote.cjs +65 -57
  8. package/dist/html/remote.cjs.map +1 -1
  9. package/dist/html/remote.js +65 -57
  10. package/dist/html/remote.js.map +1 -1
  11. package/dist/internal/next/host/app-router-client.cjs +12 -3
  12. package/dist/internal/next/host/app-router-client.cjs.map +1 -1
  13. package/dist/internal/next/host/app-router-client.d.ts +11 -2
  14. package/dist/internal/next/host/app-router-client.js +12 -3
  15. package/dist/internal/next/host/app-router-client.js.map +1 -1
  16. package/dist/internal/react/context.cjs +43 -0
  17. package/dist/internal/react/context.cjs.map +1 -0
  18. package/dist/internal/react/context.d.ts +20 -0
  19. package/dist/internal/react/context.js +18 -0
  20. package/dist/internal/react/context.js.map +1 -0
  21. package/dist/internal/react/hooks/use-resolve-client-url.cjs +39 -0
  22. package/dist/internal/react/hooks/use-resolve-client-url.cjs.map +1 -0
  23. package/dist/internal/react/hooks/use-resolve-client-url.d.ts +5 -0
  24. package/dist/internal/react/hooks/use-resolve-client-url.js +15 -0
  25. package/dist/internal/react/hooks/use-resolve-client-url.js.map +1 -0
  26. package/dist/internal/shared/client/apply-origin.cjs +10 -5
  27. package/dist/internal/shared/client/apply-origin.cjs.map +1 -1
  28. package/dist/internal/shared/client/apply-origin.d.ts +3 -1
  29. package/dist/internal/shared/client/apply-origin.js +10 -5
  30. package/dist/internal/shared/client/apply-origin.js.map +1 -1
  31. package/dist/internal/shared/client/default-resolve-client-url.cjs +32 -0
  32. package/dist/internal/shared/client/default-resolve-client-url.cjs.map +1 -0
  33. package/dist/internal/shared/client/default-resolve-client-url.d.ts +5 -0
  34. package/dist/internal/shared/client/default-resolve-client-url.js +10 -0
  35. package/dist/internal/shared/client/default-resolve-client-url.js.map +1 -0
  36. package/dist/internal/shared/client/protected-rc-fallback.cjs +11 -2
  37. package/dist/internal/shared/client/protected-rc-fallback.cjs.map +1 -1
  38. package/dist/internal/shared/client/protected-rc-fallback.d.ts +2 -1
  39. package/dist/internal/shared/client/protected-rc-fallback.js +9 -1
  40. package/dist/internal/shared/client/protected-rc-fallback.js.map +1 -1
  41. package/dist/internal/shared/client/proxy-through-host.cjs +51 -0
  42. package/dist/internal/shared/client/proxy-through-host.cjs.map +1 -0
  43. package/dist/internal/shared/client/proxy-through-host.d.ts +57 -0
  44. package/dist/internal/shared/client/proxy-through-host.js +26 -0
  45. package/dist/internal/shared/client/proxy-through-host.js.map +1 -0
  46. package/dist/internal/shared/client/remote-component.cjs +121 -137
  47. package/dist/internal/shared/client/remote-component.cjs.map +1 -1
  48. package/dist/internal/shared/client/remote-component.d.ts +7 -5
  49. package/dist/internal/shared/client/remote-component.js +120 -137
  50. package/dist/internal/shared/client/remote-component.js.map +1 -1
  51. package/dist/internal/shared/constants.cjs +3 -0
  52. package/dist/internal/shared/constants.cjs.map +1 -1
  53. package/dist/internal/shared/constants.d.ts +2 -1
  54. package/dist/internal/shared/constants.js +2 -0
  55. package/dist/internal/shared/constants.js.map +1 -1
  56. package/dist/internal/shared/error.cjs +70 -0
  57. package/dist/internal/shared/error.cjs.map +1 -1
  58. package/dist/internal/shared/error.d.ts +3 -1
  59. package/dist/internal/shared/error.js +71 -0
  60. package/dist/internal/shared/error.js.map +1 -1
  61. package/dist/internal/shared/ssr/fetch-with-hooks.cjs +7 -2
  62. package/dist/internal/shared/ssr/fetch-with-hooks.cjs.map +1 -1
  63. package/dist/internal/shared/ssr/fetch-with-hooks.js +7 -2
  64. package/dist/internal/shared/ssr/fetch-with-hooks.js.map +1 -1
  65. package/dist/internal/shared/utils/logger.cjs +26 -10
  66. package/dist/internal/shared/utils/logger.cjs.map +1 -1
  67. package/dist/internal/shared/utils/logger.d.ts +6 -1
  68. package/dist/internal/shared/utils/logger.js +24 -9
  69. package/dist/internal/shared/utils/logger.js.map +1 -1
  70. package/dist/next/config.cjs +2 -2
  71. package/dist/next/config.cjs.map +1 -1
  72. package/dist/next/config.js +2 -2
  73. package/dist/next/config.js.map +1 -1
  74. package/dist/next/host/client/index.cjs +270 -209
  75. package/dist/next/host/client/index.cjs.map +1 -1
  76. package/dist/next/host/client/index.d.ts +3 -1
  77. package/dist/next/host/client/index.js +244 -184
  78. package/dist/next/host/client/index.js.map +1 -1
  79. package/dist/next/host/pages-router-client.cjs +15 -2
  80. package/dist/next/host/pages-router-client.cjs.map +1 -1
  81. package/dist/next/host/pages-router-client.d.ts +12 -1
  82. package/dist/next/host/pages-router-client.js +15 -2
  83. package/dist/next/host/pages-router-client.js.map +1 -1
  84. package/dist/next/host/pages-router-server.cjs +2 -0
  85. package/dist/next/host/pages-router-server.cjs.map +1 -1
  86. package/dist/next/host/pages-router-server.d.ts +11 -0
  87. package/dist/next/host/pages-router-server.js +2 -0
  88. package/dist/next/host/pages-router-server.js.map +1 -1
  89. package/dist/next/proxy.cjs +33 -6
  90. package/dist/next/proxy.cjs.map +1 -1
  91. package/dist/next/proxy.js +33 -6
  92. package/dist/next/proxy.js.map +1 -1
  93. package/dist/proxy-through-host-a676a522.d.ts +52 -0
  94. package/dist/react/index.cjs +289 -209
  95. package/dist/react/index.cjs.map +1 -1
  96. package/dist/react/index.d.ts +28 -4
  97. package/dist/react/index.js +262 -184
  98. package/dist/react/index.js.map +1 -1
  99. package/dist/shared/host/proxy.cjs +32 -6
  100. package/dist/shared/host/proxy.cjs.map +1 -1
  101. package/dist/shared/host/proxy.js +36 -7
  102. package/dist/shared/host/proxy.js.map +1 -1
  103. package/package.json +1 -1
  104. package/dist/internal/shared/client/fetch-with-protected-rc-fallback.cjs +0 -65
  105. package/dist/internal/shared/client/fetch-with-protected-rc-fallback.cjs.map +0 -1
  106. package/dist/internal/shared/client/fetch-with-protected-rc-fallback.d.ts +0 -13
  107. package/dist/internal/shared/client/fetch-with-protected-rc-fallback.js +0 -41
  108. package/dist/internal/shared/client/fetch-with-protected-rc-fallback.js.map +0 -1
package/dist/html/host.js CHANGED
@@ -8,6 +8,51 @@ var __export = (target, all) => {
8
8
  __defProp(target, name, { get: all[name], enumerable: true });
9
9
  };
10
10
 
11
+ // src/shared/constants.ts
12
+ var RC_PROTECTED_REMOTE_FETCH_PATHNAME, CORS_DOCS_URL;
13
+ var init_constants = __esm({
14
+ "src/shared/constants.ts"() {
15
+ "use strict";
16
+ RC_PROTECTED_REMOTE_FETCH_PATHNAME = "/rc-fetch-protected-remote";
17
+ CORS_DOCS_URL = "https://vercel.com/docs/remote-components/concepts/cors-external-urls#accessing-cross-site-protected-remote-components";
18
+ }
19
+ });
20
+
21
+ // src/shared/client/protected-rc-fallback.ts
22
+ function generateProtectedRcFallbackSrc(url) {
23
+ return `${RC_PROTECTED_REMOTE_FETCH_PATHNAME}?url=${encodeURIComponent(url)}`;
24
+ }
25
+ function isProxiedUrl(url) {
26
+ try {
27
+ return new URL(url, location.href).pathname === RC_PROTECTED_REMOTE_FETCH_PATHNAME;
28
+ } catch {
29
+ return false;
30
+ }
31
+ }
32
+ var init_protected_rc_fallback = __esm({
33
+ "src/shared/client/protected-rc-fallback.ts"() {
34
+ "use strict";
35
+ init_constants();
36
+ }
37
+ });
38
+
39
+ // src/shared/utils/abort.ts
40
+ function isAbortError(error) {
41
+ if (error instanceof DOMException && error.name === "AbortError") {
42
+ return true;
43
+ }
44
+ if (error !== null && typeof error === "object" && "name" in error && error.name === "AbortError" && "message" in error && typeof error.message === "string") {
45
+ const e = error;
46
+ return typeof e.code === "number" || e.constructor?.name === "DOMException";
47
+ }
48
+ return false;
49
+ }
50
+ var init_abort = __esm({
51
+ "src/shared/utils/abort.ts"() {
52
+ "use strict";
53
+ }
54
+ });
55
+
11
56
  // src/shared/error.ts
12
57
  function multipleRemoteComponentsError(url) {
13
58
  return new RemoteComponentsError(
@@ -20,10 +65,76 @@ function failedToFetchRemoteComponentError(url, { status, statusText }, help = "
20
65
  { cause: new Error(`${status} ${statusText}`) }
21
66
  );
22
67
  }
68
+ async function errorFromFailedFetch(originalUrl, resolvedUrl, res) {
69
+ const isProxied = isProxiedUrl(resolvedUrl.href);
70
+ if (isProxied && res) {
71
+ const body = await res.text().catch(() => "");
72
+ return failedProxyFetchError(
73
+ originalUrl,
74
+ resolvedUrl.href,
75
+ res.status,
76
+ body
77
+ );
78
+ }
79
+ const fallback = failedToFetchRemoteComponentError(
80
+ originalUrl,
81
+ res ?? { status: 0, statusText: "No Response" }
82
+ );
83
+ if (!res)
84
+ return fallback;
85
+ try {
86
+ const body = await res.text();
87
+ const parser = new DOMParser();
88
+ const doc = parser.parseFromString(body, "text/html");
89
+ const errorTemplate = doc.querySelector(
90
+ "template[data-next-error-message]"
91
+ );
92
+ const errorMessage = errorTemplate?.getAttribute("data-next-error-message");
93
+ if (errorMessage) {
94
+ const error = new RemoteComponentsError(errorMessage);
95
+ const errorStack = errorTemplate?.getAttribute("data-next-error-stack");
96
+ if (errorStack) {
97
+ error.stack = errorStack;
98
+ }
99
+ return error;
100
+ }
101
+ } catch (parseError) {
102
+ if (isAbortError(parseError))
103
+ throw parseError;
104
+ }
105
+ return fallback;
106
+ }
107
+ function failedProxyFetchError(originalUrl, proxyUrl, status, responseBody) {
108
+ if (status === 404) {
109
+ return new RemoteComponentsError(
110
+ `Could not proxy the request to "${originalUrl}" \u2014 the proxy endpoint "${RC_PROTECTED_REMOTE_FETCH_PATHNAME}" returned 404.
111
+
112
+ The host server needs middleware or a route that handles "${RC_PROTECTED_REMOTE_FETCH_PATHNAME}".
113
+
114
+ Proxying requires two pieces:
115
+ 1. resolveClientUrl={proxyClientRequestsThroughHost} on <RemoteComponent>
116
+ 2. Middleware or a route for "${RC_PROTECTED_REMOTE_FETCH_PATHNAME}" on the host server
117
+
118
+ Docs: ${CORS_DOCS_URL}`
119
+ );
120
+ }
121
+ if (status === 403) {
122
+ const detail = responseBody ? ` ${responseBody}` : "";
123
+ return new RemoteComponentsError(
124
+ `Proxied request to "${proxyUrl}" was forbidden.${detail} See: ${CORS_DOCS_URL}`
125
+ );
126
+ }
127
+ return new RemoteComponentsError(
128
+ `Proxied request for "${originalUrl}" via "${proxyUrl}" failed with status ${status}. See: ${CORS_DOCS_URL}`
129
+ );
130
+ }
23
131
  var RemoteComponentsError;
24
132
  var init_error = __esm({
25
133
  "src/shared/error.ts"() {
26
134
  "use strict";
135
+ init_protected_rc_fallback();
136
+ init_constants();
137
+ init_abort();
27
138
  RemoteComponentsError = class extends Error {
28
139
  code = "REMOTE_COMPONENTS_ERROR";
29
140
  constructor(message, options) {
@@ -40,9 +151,6 @@ function logDebug(location2, message) {
40
151
  console.debug(`[${PREFIX}:${location2}]: ${message}`);
41
152
  }
42
153
  }
43
- function logInfo(location2, message) {
44
- console.info(`[${PREFIX}:${location2}]: ${message}`);
45
- }
46
154
  function logWarn(location2, message) {
47
155
  console.warn(`[${PREFIX}:${location2}]: ${message}`);
48
156
  }
@@ -53,10 +161,24 @@ function logError(location2, message, cause) {
53
161
  })
54
162
  );
55
163
  }
164
+ function warnCrossOriginFetchError(logLocation, url) {
165
+ try {
166
+ const parsed = typeof url === "string" ? new URL(url) : url;
167
+ if (typeof location === "undefined" || parsed.origin === location.origin) {
168
+ return;
169
+ }
170
+ logWarn(
171
+ logLocation,
172
+ `Failed to fetch cross-origin resource "${parsed.href}". If this is a protected deployment, ensure withRemoteComponentsHost middleware is configured in your host and that the remote URL is included in allowedProxyUrls. See: ${CORS_DOCS_URL}`
173
+ );
174
+ } catch {
175
+ }
176
+ }
56
177
  var PREFIX, DEBUG;
57
178
  var init_logger = __esm({
58
179
  "src/shared/utils/logger.ts"() {
59
180
  "use strict";
181
+ init_constants();
60
182
  init_error();
61
183
  PREFIX = "remote-components";
62
184
  DEBUG = typeof window !== "undefined" && localStorage.getItem("RC_DEBUG") === "true";
@@ -248,26 +370,6 @@ var init_shared_modules = __esm({
248
370
  }
249
371
  });
250
372
 
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
373
  // src/shared/client/webpack-patterns.ts
272
374
  var NEXT_BUNDLE_PATH_RE;
273
375
  var init_webpack_patterns = __esm({
@@ -278,7 +380,7 @@ var init_webpack_patterns = __esm({
278
380
  });
279
381
 
280
382
  // src/shared/client/script-loader.ts
281
- async function loadScripts(scripts) {
383
+ async function loadScripts(scripts, resolveClientUrl) {
282
384
  await Promise.all(
283
385
  scripts.map((script) => {
284
386
  return new Promise((resolve, reject) => {
@@ -287,42 +389,29 @@ async function loadScripts(scripts) {
287
389
  script.src.replace(NEXT_BUNDLE_PATH_RE, "/_next/"),
288
390
  location.origin
289
391
  ).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);
392
+ const resolvedSrc = resolveClientUrl?.(newSrc) ?? newSrc;
393
+ const newScript = document.createElement("script");
394
+ newScript.onload = () => resolve();
395
+ newScript.onerror = () => {
396
+ const isProxied = isProxiedUrl(resolvedSrc);
397
+ if (isProxied) {
398
+ reject(
399
+ new RemoteComponentsError(
400
+ `Failed to load script "${newSrc}" via proxy "${resolvedSrc}". Ensure withRemoteComponentsHost middleware is configured and "${RC_PROTECTED_REMOTE_FETCH_PATHNAME}" is in the matcher. See: ${CORS_DOCS_URL}`
401
+ )
402
+ );
403
+ } else {
404
+ warnCrossOriginFetchError("ScriptLoader", newSrc);
405
+ reject(
406
+ new RemoteComponentsError(
407
+ `Failed to load <script src="${newSrc}"> for Remote Component. Check the URL is correct.`
408
+ )
409
+ );
410
+ }
324
411
  };
325
- loadScriptWithProtectedRcFallback(newSrc);
412
+ newScript.src = resolvedSrc;
413
+ newScript.async = true;
414
+ document.head.appendChild(newScript);
326
415
  });
327
416
  })
328
417
  );
@@ -363,67 +452,6 @@ var init_const = __esm({
363
452
  }
364
453
  });
365
454
 
366
- // src/shared/utils/abort.ts
367
- function isAbortError(error) {
368
- if (error instanceof DOMException && error.name === "AbortError") {
369
- return true;
370
- }
371
- if (error !== null && typeof error === "object" && "name" in error && error.name === "AbortError" && "message" in error && typeof error.message === "string") {
372
- const e = error;
373
- return typeof e.code === "number" || e.constructor?.name === "DOMException";
374
- }
375
- return false;
376
- }
377
- var init_abort = __esm({
378
- "src/shared/utils/abort.ts"() {
379
- "use strict";
380
- }
381
- });
382
-
383
- // src/shared/client/fetch-with-protected-rc-fallback.ts
384
- async function fetchWithProtectedRcFallback(url, init) {
385
- try {
386
- const res = await fetch(url, init);
387
- return res;
388
- } catch (error) {
389
- if (isAbortError(error)) {
390
- throw error;
391
- }
392
- const parsedUrl = new URL(url);
393
- if (typeof document === "object" && typeof document.location === "object" && document.location.origin !== parsedUrl.origin) {
394
- logWarn(
395
- "FetchRemoteComponent",
396
- "Request failed due to CORS, attempting to fetch it via the withRemoteComponentsHost proxy."
397
- );
398
- const proxiedRes = await fetch(
399
- generateProtectedRcFallbackSrc(parsedUrl.href),
400
- init?.signal ? { signal: init.signal } : void 0
401
- );
402
- if (proxiedRes.status === 200) {
403
- logInfo(
404
- "FetchRemoteComponent",
405
- `Successfully fetched ${parsedUrl.href} with fallback withRemoteComponentsHost proxy`
406
- );
407
- return proxiedRes;
408
- } else {
409
- logError(
410
- "FetchRemoteComponent",
411
- `Could not proxy remote: [response status ${proxiedRes.status}] ${await proxiedRes.text()}`
412
- );
413
- }
414
- }
415
- throw error;
416
- }
417
- }
418
- var init_fetch_with_protected_rc_fallback = __esm({
419
- "src/shared/client/fetch-with-protected-rc-fallback.ts"() {
420
- "use strict";
421
- init_protected_rc_fallback();
422
- init_abort();
423
- init_logger();
424
- }
425
- });
426
-
427
455
  // src/shared/client/turbopack-patterns.ts
428
456
  var REMOTE_SHARED_MARKER_RE, REMOTE_SHARED_ASSIGNMENT_RE, ASYNC_MODULE_LOADER_RE, ASYNC_MODULE_RESOLVE_RE, ASYNC_MODULE_ALL_RE, TURBOPACK_GLOBAL_RE;
429
457
  var init_turbopack_patterns = __esm({
@@ -439,7 +467,7 @@ var init_turbopack_patterns = __esm({
439
467
  });
440
468
 
441
469
  // src/shared/client/chunk-loader.ts
442
- function createChunkLoader(runtime) {
470
+ function createChunkLoader(runtime, resolveClientUrl) {
443
471
  return function __turbopack_chunk_load__(chunkId, scriptBundle) {
444
472
  logDebug("ChunkLoader", `Loading chunk: "${chunkId}"`);
445
473
  const self = globalThis;
@@ -479,9 +507,10 @@ function createChunkLoader(runtime) {
479
507
  logDebug("ChunkLoader", `Returning cached promise for: "${url}"`);
480
508
  return self.__remote_components_turbopack_chunk_loader_promise__[url];
481
509
  }
482
- logDebug("ChunkLoader", `Fetching chunk from: "${url}"`);
510
+ const resolvedUrl = resolveClientUrl?.(url) ?? url;
511
+ logDebug("ChunkLoader", `Fetching chunk from: "${resolvedUrl}"`);
483
512
  self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise((resolve, reject) => {
484
- fetchWithProtectedRcFallback(url).then((res) => res.text()).then((code) => {
513
+ fetch(resolvedUrl).then((res) => res.text()).then((code) => {
485
514
  const hasTurbopack = TURBOPACK_GLOBAL_RE.test(code);
486
515
  if (hasTurbopack) {
487
516
  return handleTurbopackChunk(code, bundle ?? "", url);
@@ -494,7 +523,19 @@ function createChunkLoader(runtime) {
494
523
  "ChunkLoader",
495
524
  `First 500 chars of chunk: ${code.slice(0, 500)}`
496
525
  );
497
- }).then(resolve).catch(reject);
526
+ }).then(resolve).catch((error) => {
527
+ const isProxied = isProxiedUrl(resolvedUrl);
528
+ if (isProxied) {
529
+ reject(
530
+ new RemoteComponentsError(
531
+ `Failed to load chunk "${url}" via proxy "${resolvedUrl}". Ensure withRemoteComponentsHost middleware is configured and "${RC_PROTECTED_REMOTE_FETCH_PATHNAME}" is in the matcher. See: ${CORS_DOCS_URL}`
532
+ )
533
+ );
534
+ } else {
535
+ warnCrossOriginFetchError("ChunkLoader", url);
536
+ reject(error);
537
+ }
538
+ });
498
539
  });
499
540
  return self.__remote_components_turbopack_chunk_loader_promise__[url];
500
541
  };
@@ -639,10 +680,11 @@ async function handleTurbopackChunk(code, bundle, url) {
639
680
  var init_chunk_loader = __esm({
640
681
  "src/shared/client/chunk-loader.ts"() {
641
682
  "use strict";
683
+ init_constants();
642
684
  init_error();
643
685
  init_logger();
644
686
  init_const();
645
- init_fetch_with_protected_rc_fallback();
687
+ init_protected_rc_fallback();
646
688
  init_turbopack_patterns();
647
689
  }
648
690
  });
@@ -1013,7 +1055,7 @@ var init_shared_modules2 = __esm({
1013
1055
  });
1014
1056
 
1015
1057
  // src/shared/client/webpack-adapter.ts
1016
- async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location.href), bundle, shared = {}, remoteShared = {}) {
1058
+ async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location.href), bundle, shared = {}, remoteShared = {}, resolveClientUrl) {
1017
1059
  const self = globalThis;
1018
1060
  if (!self.__remote_bundle_url__) {
1019
1061
  self.__remote_bundle_url__ = {};
@@ -1025,7 +1067,7 @@ async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location
1025
1067
  self.__original_webpack_chunk_load__ = self.__webpack_chunk_load__;
1026
1068
  self.__original_webpack_require__ = self.__webpack_require__;
1027
1069
  }
1028
- self.__webpack_chunk_load__ = createChunkLoader(runtime);
1070
+ self.__webpack_chunk_load__ = createChunkLoader(runtime, resolveClientUrl);
1029
1071
  self.__webpack_require__ = createModuleRequire(runtime);
1030
1072
  self.__webpack_require_type__ = runtime;
1031
1073
  if (self.__remote_webpack_require__ && runtime === RUNTIME_TURBOPACK) {
@@ -1119,27 +1161,21 @@ var init_webpack_adapter = __esm({
1119
1161
  });
1120
1162
 
1121
1163
  // 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);
1164
+ async function importViaCallback(absoluteSrc, resolveClientUrl) {
1165
+ const resolvedUrl = resolveClientUrl(absoluteSrc) ?? absoluteSrc;
1166
+ const fetchUrl = new URL(resolvedUrl, location.href).href;
1167
+ const response = await fetch(fetchUrl);
1128
1168
  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
- );
1169
+ throw new Error(`Proxied fetch failed: ${response.status}`);
1134
1170
  const content = (await response.text()).replace(/import\.meta\.url/g, JSON.stringify(absoluteSrc)).replace(
1135
1171
  /\b(from|import)\s*(["'])(\.\.?\/[^"']+)\2/g,
1136
1172
  (_, keyword, quote, relativePath) => {
1137
1173
  const absoluteImportUrl = new URL(relativePath, absoluteSrc).href;
1138
- const absoluteProxyUrl = new URL(
1139
- generateProtectedRcFallbackSrc(absoluteImportUrl),
1174
+ const resolvedImportUrl = new URL(
1175
+ resolveClientUrl(absoluteImportUrl) ?? absoluteImportUrl,
1140
1176
  location.href
1141
1177
  ).href;
1142
- return `${keyword} ${quote}${absoluteProxyUrl}${quote}`;
1178
+ return `${keyword} ${quote}${resolvedImportUrl}${quote}`;
1143
1179
  }
1144
1180
  );
1145
1181
  const moduleBlobUrl = URL.createObjectURL(
@@ -1173,6 +1209,20 @@ async function importViaProxy(absoluteSrc) {
1173
1209
  delete registry[absoluteSrc];
1174
1210
  return mod;
1175
1211
  }
1212
+ async function importDirectly(absoluteSrc) {
1213
+ try {
1214
+ return await import(
1215
+ /* @vite-ignore */
1216
+ /* webpackIgnore: true */
1217
+ absoluteSrc
1218
+ );
1219
+ } catch (importError) {
1220
+ if (!absoluteSrc.startsWith("blob:")) {
1221
+ warnCrossOriginFetchError("StaticLoader", absoluteSrc);
1222
+ }
1223
+ throw importError;
1224
+ }
1225
+ }
1176
1226
  function resolveScriptSrc(script, url) {
1177
1227
  const rawSrc = typeof script.getAttribute === "function" ? script.getAttribute("src") ?? script.src : script.src;
1178
1228
  if (!rawSrc && script.textContent) {
@@ -1185,24 +1235,7 @@ function resolveScriptSrc(script, url) {
1185
1235
  }
1186
1236
  return rawSrc;
1187
1237
  }
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
- }
1205
- async function loadStaticRemoteComponent(scripts, url) {
1238
+ async function loadStaticRemoteComponent(scripts, url, resolveClientUrl) {
1206
1239
  const self = globalThis;
1207
1240
  if (self.__remote_script_entrypoint_mount__?.[url.href]) {
1208
1241
  self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
@@ -1215,7 +1248,7 @@ async function loadStaticRemoteComponent(scripts, url) {
1215
1248
  try {
1216
1249
  const src = resolveScriptSrc(script, url);
1217
1250
  const absoluteSrc = new URL(src, url).href;
1218
- const mod = await importScriptMod(absoluteSrc);
1251
+ const mod = resolveClientUrl ? await importViaCallback(absoluteSrc, resolveClientUrl) : await importDirectly(absoluteSrc);
1219
1252
  if (src.startsWith("blob:")) {
1220
1253
  URL.revokeObjectURL(src);
1221
1254
  }
@@ -1280,7 +1313,6 @@ var init_static_loader = __esm({
1280
1313
  "src/shared/client/static-loader.ts"() {
1281
1314
  "use strict";
1282
1315
  init_logger();
1283
- init_protected_rc_fallback();
1284
1316
  }
1285
1317
  });
1286
1318
 
@@ -1492,7 +1524,7 @@ var webpack_exports = {};
1492
1524
  __export(webpack_exports, {
1493
1525
  webpackRuntime: () => webpackRuntime
1494
1526
  });
1495
- async function webpackRuntime(bundle, shared, remoteShared) {
1527
+ async function webpackRuntime(bundle, shared, remoteShared, resolveClientUrl) {
1496
1528
  const self = globalThis;
1497
1529
  if (!self.__DISABLE_WEBPACK_EXEC__) {
1498
1530
  self.__DISABLE_WEBPACK_EXEC__ = {};
@@ -1535,7 +1567,7 @@ async function webpackRuntime(bundle, shared, remoteShared) {
1535
1567
  }
1536
1568
  ];
1537
1569
  });
1538
- await loadScripts(scriptSrcs);
1570
+ await loadScripts(scriptSrcs, resolveClientUrl);
1539
1571
  const hostShared = {
1540
1572
  ...sharedPolyfills(shared),
1541
1573
  ...self.__remote_component_host_shared_modules__,
@@ -1592,7 +1624,7 @@ var turbopack_exports = {};
1592
1624
  __export(turbopack_exports, {
1593
1625
  turbopackRuntime: () => turbopackRuntime
1594
1626
  });
1595
- async function turbopackRuntime(url, bundle, shared, remoteShared) {
1627
+ async function turbopackRuntime(url, bundle, shared, remoteShared, resolveClientUrl) {
1596
1628
  const self = globalThis;
1597
1629
  const hostShared = {
1598
1630
  ...sharedPolyfills(shared),
@@ -1606,7 +1638,8 @@ async function turbopackRuntime(url, bundle, shared, remoteShared) {
1606
1638
  url,
1607
1639
  bundle,
1608
1640
  hostShared,
1609
- remoteShared
1641
+ remoteShared,
1642
+ resolveClientUrl
1610
1643
  );
1611
1644
  const {
1612
1645
  default: { createFromReadableStream }
@@ -1620,7 +1653,8 @@ async function turbopackRuntime(url, bundle, shared, remoteShared) {
1620
1653
  url,
1621
1654
  bundle,
1622
1655
  hostShared,
1623
- remoteShared
1656
+ remoteShared,
1657
+ resolveClientUrl
1624
1658
  );
1625
1659
  }
1626
1660
  return {
@@ -1646,7 +1680,7 @@ var script_exports = {};
1646
1680
  __export(script_exports, {
1647
1681
  scriptRuntime: () => scriptRuntime
1648
1682
  });
1649
- function scriptRuntime() {
1683
+ function scriptRuntime(resolveClientUrl) {
1650
1684
  const self = globalThis;
1651
1685
  return {
1652
1686
  self,
@@ -1656,7 +1690,7 @@ function scriptRuntime() {
1656
1690
  Component: null,
1657
1691
  App: null
1658
1692
  }),
1659
- preloadScripts: loadStaticRemoteComponent
1693
+ preloadScripts: (scripts, url) => loadStaticRemoteComponent(scripts, url, resolveClientUrl)
1660
1694
  };
1661
1695
  }
1662
1696
  var init_script = __esm({
@@ -1682,7 +1716,7 @@ var tagNames = [
1682
1716
  "script",
1683
1717
  "link"
1684
1718
  ];
1685
- function applyOriginToNodes(doc, url) {
1719
+ function applyOriginToNodes(doc, url, resolveClientUrl) {
1686
1720
  if (url.origin !== location.origin) {
1687
1721
  const nodes = doc.querySelectorAll(
1688
1722
  tagNames.map(
@@ -1691,12 +1725,15 @@ function applyOriginToNodes(doc, url) {
1691
1725
  );
1692
1726
  nodes.forEach((node) => {
1693
1727
  if (node.hasAttribute("src") && /^[./]+\/?/.test(node.getAttribute("src") ?? "")) {
1694
- node.src = new URL(node.getAttribute("src") ?? "/", url).href;
1728
+ const absoluteSrc = new URL(node.getAttribute("src") ?? "/", url).href;
1729
+ const isScript = node.tagName.toLowerCase() === "script";
1730
+ node.src = isScript ? absoluteSrc : resolveClientUrl?.(absoluteSrc) ?? absoluteSrc;
1695
1731
  }
1696
1732
  if (node.hasAttribute("href") && /^[./]+\/?/.test(node.getAttribute("href") ?? "")) {
1733
+ const absoluteHref = new URL(node.getAttribute("href") ?? "/", url).href;
1697
1734
  node.setAttribute(
1698
1735
  "href",
1699
- new URL(node.getAttribute("href") ?? "/", url).href
1736
+ resolveClientUrl?.(absoluteHref) ?? absoluteHref
1700
1737
  );
1701
1738
  }
1702
1739
  if (node.hasAttribute("srcset")) {
@@ -1705,7 +1742,8 @@ function applyOriginToNodes(doc, url) {
1705
1742
  if (!urlPart)
1706
1743
  return entry;
1707
1744
  const absoluteUrl = new URL(urlPart, url).href;
1708
- return descriptor ? `${absoluteUrl} ${descriptor}` : absoluteUrl;
1745
+ const resolvedUrl = resolveClientUrl?.(absoluteUrl) ?? absoluteUrl;
1746
+ return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl;
1709
1747
  }).join(", ");
1710
1748
  if (srcSet) {
1711
1749
  node.setAttribute("srcset", srcSet);
@@ -1717,7 +1755,8 @@ function applyOriginToNodes(doc, url) {
1717
1755
  if (!urlPart)
1718
1756
  return entry;
1719
1757
  const absoluteUrl = new URL(urlPart, url).href;
1720
- return descriptor ? `${absoluteUrl} ${descriptor}` : absoluteUrl;
1758
+ const resolvedUrl = resolveClientUrl?.(absoluteUrl) ?? absoluteUrl;
1759
+ return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl;
1721
1760
  }).join(", ");
1722
1761
  if (srcSet) {
1723
1762
  node.setAttribute("imagesrcset", srcSet);
@@ -1727,6 +1766,34 @@ function applyOriginToNodes(doc, url) {
1727
1766
  }
1728
1767
  }
1729
1768
 
1769
+ // src/shared/client/proxy-through-host.ts
1770
+ init_protected_rc_fallback();
1771
+ function withRemoteSrc(resolveClientUrl, remoteSrc) {
1772
+ return (url) => resolveClientUrl(remoteSrc, url);
1773
+ }
1774
+ var proxyClientRequestsThroughHost = (remoteSrc, url) => {
1775
+ if (typeof location === "undefined") {
1776
+ return void 0;
1777
+ }
1778
+ const remoteOrigin = new URL(remoteSrc, location.href).origin;
1779
+ if (remoteOrigin === location.origin) {
1780
+ return void 0;
1781
+ }
1782
+ try {
1783
+ const parsed = new URL(url, location.href);
1784
+ if (parsed.origin === remoteOrigin) {
1785
+ return generateProtectedRcFallbackSrc(url);
1786
+ }
1787
+ } catch {
1788
+ }
1789
+ return void 0;
1790
+ };
1791
+
1792
+ // src/shared/client/default-resolve-client-url.ts
1793
+ function bindResolveClientUrl(prop, remoteSrc) {
1794
+ return prop ? withRemoteSrc(prop, remoteSrc) : void 0;
1795
+ }
1796
+
1730
1797
  // src/shared/client/remote-component.ts
1731
1798
  init_error();
1732
1799
 
@@ -1821,7 +1888,7 @@ init_webpack_adapter();
1821
1888
  init_error();
1822
1889
 
1823
1890
  // src/shared/ssr/fetch-with-hooks.ts
1824
- init_fetch_with_protected_rc_fallback();
1891
+ init_logger();
1825
1892
 
1826
1893
  // src/shared/ssr/fetch-headers.ts
1827
1894
  function remoteFetchHeaders() {
@@ -1859,7 +1926,12 @@ async function fetchWithHooks(url, additionalInit, options = {}) {
1859
1926
  };
1860
1927
  let res = await onRequest?.(url, init, hookOptions);
1861
1928
  if (!res) {
1862
- res = await fetchWithProtectedRcFallback(url, init);
1929
+ try {
1930
+ res = await fetch(url, init);
1931
+ } catch (error) {
1932
+ warnCrossOriginFetchError("FetchRemoteComponent", url);
1933
+ throw error;
1934
+ }
1863
1935
  }
1864
1936
  const transformedRes = await onResponse?.(url, res, hookOptions);
1865
1937
  if (transformedRes) {
@@ -1882,7 +1954,8 @@ async function attachStyles({
1882
1954
  signal,
1883
1955
  baseUrl,
1884
1956
  remoteComponentSrc,
1885
- root
1957
+ root,
1958
+ resolveClientUrl
1886
1959
  }) {
1887
1960
  const appendedLinks = [];
1888
1961
  let abortReject = null;
@@ -1917,9 +1990,13 @@ async function attachStyles({
1917
1990
  });
1918
1991
  for (const attr of link.attributes) {
1919
1992
  if (attr.name === "href") {
1993
+ const absoluteHref = new URL(
1994
+ attr.value,
1995
+ baseUrl ?? location.origin
1996
+ ).href;
1920
1997
  newLink.setAttribute(
1921
1998
  attr.name,
1922
- new URL(attr.value, baseUrl ?? location.origin).href
1999
+ resolveClientUrl?.(absoluteHref) ?? absoluteHref
1923
2000
  );
1924
2001
  } else {
1925
2002
  newLink.setAttribute(attr.name, attr.value);
@@ -1953,7 +2030,7 @@ async function attachStyles({
1953
2030
 
1954
2031
  // src/html/host/runtime/index.ts
1955
2032
  init_error();
1956
- async function getRuntime(type, url, bundle, shared, remoteShared) {
2033
+ async function getRuntime(type, url, bundle, shared, remoteShared, resolveClientUrl) {
1957
2034
  if (typeof globalThis.process === "undefined") {
1958
2035
  globalThis.process = {
1959
2036
  env: {}
@@ -1961,13 +2038,24 @@ async function getRuntime(type, url, bundle, shared, remoteShared) {
1961
2038
  }
1962
2039
  if (type === "webpack") {
1963
2040
  const { webpackRuntime: webpackRuntime2 } = await Promise.resolve().then(() => (init_webpack(), webpack_exports));
1964
- return webpackRuntime2(bundle ?? "default", shared, remoteShared);
2041
+ return webpackRuntime2(
2042
+ bundle ?? "default",
2043
+ shared,
2044
+ remoteShared,
2045
+ resolveClientUrl
2046
+ );
1965
2047
  } else if (type === "turbopack") {
1966
2048
  const { turbopackRuntime: turbopackRuntime2 } = await Promise.resolve().then(() => (init_turbopack(), turbopack_exports));
1967
- return turbopackRuntime2(url, bundle, shared, remoteShared);
2049
+ return turbopackRuntime2(
2050
+ url,
2051
+ bundle,
2052
+ shared,
2053
+ remoteShared,
2054
+ resolveClientUrl
2055
+ );
1968
2056
  } else if (type === "script") {
1969
2057
  const { scriptRuntime: scriptRuntime2 } = await Promise.resolve().then(() => (init_script(), script_exports));
1970
- return scriptRuntime2();
2058
+ return scriptRuntime2(resolveClientUrl);
1971
2059
  }
1972
2060
  throw new RemoteComponentsError(
1973
2061
  `Remote Components runtime "${type}" is not supported. Supported runtimes are "webpack", "turbopack", and "script".`
@@ -1992,6 +2080,7 @@ if (typeof HTMLElement !== "undefined") {
1992
2080
  reactRoot;
1993
2081
  onRequest;
1994
2082
  onResponse;
2083
+ resolveClientUrl;
1995
2084
  /** Current AbortController for the loading operation - can be used to cancel loading via controller.abort() */
1996
2085
  abortController;
1997
2086
  static get observedAttributes() {
@@ -2098,13 +2187,18 @@ if (typeof HTMLElement !== "undefined") {
2098
2187
  this.name = url.hash.slice(1);
2099
2188
  }
2100
2189
  }
2190
+ const resolveClientUrl = url ? bindResolveClientUrl(this.resolveClientUrl, url.href) : void 0;
2101
2191
  if (!remoteComponentChild && url) {
2102
2192
  const fetchInit = {
2103
2193
  credentials: this.getAttribute("credentials") || "same-origin"
2104
2194
  };
2195
+ const resolvedUrl = new URL(
2196
+ resolveClientUrl?.(url.href) ?? url.href,
2197
+ window.location.href
2198
+ );
2105
2199
  let res;
2106
2200
  try {
2107
- res = await fetchWithHooks(url, fetchInit, {
2201
+ res = await fetchWithHooks(resolvedUrl, fetchInit, {
2108
2202
  onRequest: this.onRequest,
2109
2203
  onResponse: this.onResponse,
2110
2204
  abortController: this.abortController
@@ -2116,34 +2210,7 @@ if (typeof HTMLElement !== "undefined") {
2116
2210
  throw e;
2117
2211
  }
2118
2212
  if (!res || !res.ok) {
2119
- let error = failedToFetchRemoteComponentError(
2120
- url.href,
2121
- res ?? new Response(null, { status: 0 })
2122
- );
2123
- try {
2124
- const body = res ? await res.text() : "";
2125
- const parser2 = new DOMParser();
2126
- const doc2 = parser2.parseFromString(body, "text/html");
2127
- const errorTemplate = doc2.querySelector(
2128
- "template[data-next-error-message]"
2129
- );
2130
- const errorMessage = errorTemplate?.getAttribute(
2131
- "data-next-error-message"
2132
- );
2133
- const errorStack = errorTemplate?.getAttribute(
2134
- "data-next-error-stack"
2135
- );
2136
- if (errorMessage) {
2137
- error = new RemoteComponentsError(errorMessage);
2138
- if (errorStack) {
2139
- error.stack = errorStack;
2140
- }
2141
- }
2142
- } catch (parseError) {
2143
- if (isAbortError(parseError))
2144
- throw parseError;
2145
- }
2146
- throw error;
2213
+ throw await errorFromFailedFetch(url.href, resolvedUrl, res);
2147
2214
  }
2148
2215
  try {
2149
2216
  html = await res.text();
@@ -2258,7 +2325,7 @@ if (typeof HTMLElement !== "undefined") {
2258
2325
  });
2259
2326
  this.dispatchEvent(changeEvent);
2260
2327
  }
2261
- this.prevUrl = url ?? new URL(location.href);
2328
+ this.prevUrl = url ?? new URL(window.location.href);
2262
2329
  this.prevIsRemoteComponent = isRemoteComponent;
2263
2330
  this.prevSrc = src;
2264
2331
  this.prevName = this.name;
@@ -2273,7 +2340,8 @@ if (typeof HTMLElement !== "undefined") {
2273
2340
  // Effects run after load, no abort needed
2274
2341
  baseUrl: url?.href,
2275
2342
  remoteComponentSrc,
2276
- root: this.root ?? null
2343
+ root: this.root ?? null,
2344
+ resolveClientUrl
2277
2345
  });
2278
2346
  if (!this.reactRoot) {
2279
2347
  await attachStyles({
@@ -2283,22 +2351,31 @@ if (typeof HTMLElement !== "undefined") {
2283
2351
  signal,
2284
2352
  baseUrl: url?.href,
2285
2353
  remoteComponentSrc,
2286
- root: this.root
2354
+ root: this.root,
2355
+ resolveClientUrl
2287
2356
  });
2288
2357
  if (!isCurrentLoad()) {
2289
2358
  return abandonLoad();
2290
2359
  }
2291
2360
  }
2292
- applyOriginToNodes(doc, url ?? new URL(location.href));
2361
+ applyOriginToNodes(
2362
+ doc,
2363
+ url ?? new URL(window.location.href),
2364
+ resolveClientUrl
2365
+ );
2293
2366
  if (!this.reactRoot) {
2294
2367
  Array.from(component.children).forEach((el) => {
2295
2368
  if (!isRemoteComponent && el.tagName.toLowerCase() === "script") {
2296
2369
  const newScript = document.createElement("script");
2297
2370
  for (const attr of el.attributes) {
2298
2371
  if (attr.name === "src") {
2372
+ const absoluteSrc = new URL(
2373
+ attr.value,
2374
+ url ?? window.location.origin
2375
+ ).href;
2299
2376
  newScript.setAttribute(
2300
2377
  attr.name,
2301
- new URL(attr.value, url ?? location.origin).href
2378
+ resolveClientUrl?.(absoluteSrc) ?? absoluteSrc
2302
2379
  );
2303
2380
  } else {
2304
2381
  newScript.setAttribute(attr.name, attr.value);
@@ -2360,7 +2437,7 @@ if (typeof HTMLElement !== "undefined") {
2360
2437
  preloadScripts
2361
2438
  } = await getRuntime(
2362
2439
  metadataObj.runtime,
2363
- url ?? new URL(location.href),
2440
+ url ?? new URL(window.location.href),
2364
2441
  this.bundle,
2365
2442
  {
2366
2443
  react: async () => (await import("react")).default,
@@ -2369,7 +2446,8 @@ if (typeof HTMLElement !== "undefined") {
2369
2446
  "react-dom": async () => (await import("react-dom")).default,
2370
2447
  "react-dom/client": async () => (await import("react-dom/client")).default
2371
2448
  },
2372
- remoteShared
2449
+ remoteShared,
2450
+ resolveClientUrl
2373
2451
  );
2374
2452
  if (!isCurrentLoad()) {
2375
2453
  return abandonLoad();
@@ -2591,6 +2669,7 @@ function registerSharedModules(modules = {}) {
2591
2669
  });
2592
2670
  }
2593
2671
  export {
2672
+ proxyClientRequestsThroughHost,
2594
2673
  registerSharedModules
2595
2674
  };
2596
2675
  //# sourceMappingURL=host.js.map