remote-components 0.0.51 → 0.1.2

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 (135) hide show
  1. package/dist/{component-loader-1838f572.d.ts → component-loader-21865da3.d.ts} +142 -16
  2. package/dist/host-config-58cdccea.d.ts +87 -0
  3. package/dist/html/host.cjs +589 -377
  4. package/dist/html/host.cjs.map +1 -1
  5. package/dist/html/host.d.ts +2 -0
  6. package/dist/html/host.js +588 -377
  7. package/dist/html/host.js.map +1 -1
  8. package/dist/html/remote.cjs +65 -57
  9. package/dist/html/remote.cjs.map +1 -1
  10. package/dist/html/remote.js +65 -57
  11. package/dist/html/remote.js.map +1 -1
  12. package/dist/internal/next/host/app-router-client.cjs +21 -10
  13. package/dist/internal/next/host/app-router-client.cjs.map +1 -1
  14. package/dist/internal/next/host/app-router-client.d.ts +36 -14
  15. package/dist/internal/next/host/app-router-client.js +21 -10
  16. package/dist/internal/next/host/app-router-client.js.map +1 -1
  17. package/dist/internal/next/remote/render-server.cjs.map +1 -1
  18. package/dist/internal/next/remote/render-server.d.ts +13 -14
  19. package/dist/internal/next/remote/render-server.js.map +1 -1
  20. package/dist/internal/react/context.cjs +43 -0
  21. package/dist/internal/react/context.cjs.map +1 -0
  22. package/dist/internal/react/context.d.ts +20 -0
  23. package/dist/internal/react/context.js +18 -0
  24. package/dist/internal/react/context.js.map +1 -0
  25. package/dist/internal/react/hooks/use-resolve-client-url.cjs +39 -0
  26. package/dist/internal/react/hooks/use-resolve-client-url.cjs.map +1 -0
  27. package/dist/internal/react/hooks/use-resolve-client-url.d.ts +5 -0
  28. package/dist/internal/react/hooks/use-resolve-client-url.js +15 -0
  29. package/dist/internal/react/hooks/use-resolve-client-url.js.map +1 -0
  30. package/dist/internal/shared/client/apply-origin.cjs +10 -5
  31. package/dist/internal/shared/client/apply-origin.cjs.map +1 -1
  32. package/dist/internal/shared/client/apply-origin.d.ts +3 -1
  33. package/dist/internal/shared/client/apply-origin.js +10 -5
  34. package/dist/internal/shared/client/apply-origin.js.map +1 -1
  35. package/dist/internal/shared/client/default-resolve-client-url.cjs +32 -0
  36. package/dist/internal/shared/client/default-resolve-client-url.cjs.map +1 -0
  37. package/dist/internal/shared/client/default-resolve-client-url.d.ts +5 -0
  38. package/dist/internal/shared/client/default-resolve-client-url.js +10 -0
  39. package/dist/internal/shared/client/default-resolve-client-url.js.map +1 -0
  40. package/dist/internal/shared/client/protected-rc-fallback.cjs +11 -2
  41. package/dist/internal/shared/client/protected-rc-fallback.cjs.map +1 -1
  42. package/dist/internal/shared/client/protected-rc-fallback.d.ts +2 -1
  43. package/dist/internal/shared/client/protected-rc-fallback.js +9 -1
  44. package/dist/internal/shared/client/protected-rc-fallback.js.map +1 -1
  45. package/dist/internal/shared/client/proxy-through-host.cjs +65 -0
  46. package/dist/internal/shared/client/proxy-through-host.cjs.map +1 -0
  47. package/dist/internal/shared/client/proxy-through-host.d.ts +62 -0
  48. package/dist/internal/shared/client/proxy-through-host.js +40 -0
  49. package/dist/internal/shared/client/proxy-through-host.js.map +1 -0
  50. package/dist/internal/shared/client/remote-component.cjs +121 -137
  51. package/dist/internal/shared/client/remote-component.cjs.map +1 -1
  52. package/dist/internal/shared/client/remote-component.d.ts +7 -5
  53. package/dist/internal/shared/client/remote-component.js +120 -137
  54. package/dist/internal/shared/client/remote-component.js.map +1 -1
  55. package/dist/internal/shared/constants.cjs +3 -0
  56. package/dist/internal/shared/constants.cjs.map +1 -1
  57. package/dist/internal/shared/constants.d.ts +2 -1
  58. package/dist/internal/shared/constants.js +2 -0
  59. package/dist/internal/shared/constants.js.map +1 -1
  60. package/dist/internal/shared/contract/host-state.cjs +38 -0
  61. package/dist/internal/shared/contract/host-state.cjs.map +1 -0
  62. package/dist/internal/shared/contract/host-state.d.ts +53 -0
  63. package/dist/internal/shared/contract/host-state.js +14 -0
  64. package/dist/internal/shared/contract/host-state.js.map +1 -0
  65. package/dist/internal/shared/contract/resolve-name-from-src.cjs +40 -0
  66. package/dist/internal/shared/contract/resolve-name-from-src.cjs.map +1 -0
  67. package/dist/internal/shared/contract/resolve-name-from-src.d.ts +13 -0
  68. package/dist/internal/shared/contract/resolve-name-from-src.js +16 -0
  69. package/dist/internal/shared/contract/resolve-name-from-src.js.map +1 -0
  70. package/dist/internal/shared/error.cjs +70 -0
  71. package/dist/internal/shared/error.cjs.map +1 -1
  72. package/dist/internal/shared/error.d.ts +3 -1
  73. package/dist/internal/shared/error.js +71 -0
  74. package/dist/internal/shared/error.js.map +1 -1
  75. package/dist/internal/shared/ssr/dom-flight.d.ts +1 -1
  76. package/dist/internal/shared/ssr/fetch-remote-component.cjs.map +1 -1
  77. package/dist/internal/shared/ssr/fetch-remote-component.d.ts +1 -1
  78. package/dist/internal/shared/ssr/fetch-remote-component.js.map +1 -1
  79. package/dist/internal/shared/ssr/fetch-with-hooks.cjs +7 -2
  80. package/dist/internal/shared/ssr/fetch-with-hooks.cjs.map +1 -1
  81. package/dist/internal/shared/ssr/fetch-with-hooks.d.ts +1 -1
  82. package/dist/internal/shared/ssr/fetch-with-hooks.js +7 -2
  83. package/dist/internal/shared/ssr/fetch-with-hooks.js.map +1 -1
  84. package/dist/internal/shared/utils/logger.cjs +26 -10
  85. package/dist/internal/shared/utils/logger.cjs.map +1 -1
  86. package/dist/internal/shared/utils/logger.d.ts +6 -1
  87. package/dist/internal/shared/utils/logger.js +24 -9
  88. package/dist/internal/shared/utils/logger.js.map +1 -1
  89. package/dist/next/config.cjs +2 -2
  90. package/dist/next/config.cjs.map +1 -1
  91. package/dist/next/config.js +2 -2
  92. package/dist/next/config.js.map +1 -1
  93. package/dist/next/host/app-router-server.cjs.map +1 -1
  94. package/dist/next/host/app-router-server.d.ts +11 -41
  95. package/dist/next/host/app-router-server.js.map +1 -1
  96. package/dist/next/host/client/index.cjs +467 -298
  97. package/dist/next/host/client/index.cjs.map +1 -1
  98. package/dist/next/host/client/index.d.ts +3 -1
  99. package/dist/next/host/client/index.js +445 -277
  100. package/dist/next/host/client/index.js.map +1 -1
  101. package/dist/next/host/pages-router-client.cjs +15 -2
  102. package/dist/next/host/pages-router-client.cjs.map +1 -1
  103. package/dist/next/host/pages-router-client.d.ts +14 -26
  104. package/dist/next/host/pages-router-client.js +15 -2
  105. package/dist/next/host/pages-router-client.js.map +1 -1
  106. package/dist/next/host/pages-router-server.cjs +2 -0
  107. package/dist/next/host/pages-router-server.cjs.map +1 -1
  108. package/dist/next/host/pages-router-server.d.ts +17 -31
  109. package/dist/next/host/pages-router-server.js +2 -0
  110. package/dist/next/host/pages-router-server.js.map +1 -1
  111. package/dist/next/index.cjs.map +1 -1
  112. package/dist/next/index.d.ts +13 -39
  113. package/dist/next/index.js.map +1 -1
  114. package/dist/next/proxy.cjs +33 -6
  115. package/dist/next/proxy.cjs.map +1 -1
  116. package/dist/next/proxy.js +33 -6
  117. package/dist/next/proxy.js.map +1 -1
  118. package/dist/next/remote/server.d.ts +4 -0
  119. package/dist/proxy-through-host-a676a522.d.ts +52 -0
  120. package/dist/react/index.cjs +486 -298
  121. package/dist/react/index.cjs.map +1 -1
  122. package/dist/react/index.d.ts +27 -40
  123. package/dist/react/index.js +463 -277
  124. package/dist/react/index.js.map +1 -1
  125. package/dist/shared/host/proxy.cjs +32 -6
  126. package/dist/shared/host/proxy.cjs.map +1 -1
  127. package/dist/shared/host/proxy.js +36 -7
  128. package/dist/shared/host/proxy.js.map +1 -1
  129. package/dist/{types-cbe44b51.d.ts → types-2b26a246.d.ts} +23 -6
  130. package/package.json +1 -1
  131. package/dist/internal/shared/client/fetch-with-protected-rc-fallback.cjs +0 -65
  132. package/dist/internal/shared/client/fetch-with-protected-rc-fallback.cjs.map +0 -1
  133. package/dist/internal/shared/client/fetch-with-protected-rc-fallback.d.ts +0 -13
  134. package/dist/internal/shared/client/fetch-with-protected-rc-fallback.js +0 -41
  135. package/dist/internal/shared/client/fetch-with-protected-rc-fallback.js.map +0 -1
@@ -30,6 +30,51 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
30
  ));
31
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
32
 
33
+ // src/shared/constants.ts
34
+ var RC_PROTECTED_REMOTE_FETCH_PATHNAME, CORS_DOCS_URL;
35
+ var init_constants = __esm({
36
+ "src/shared/constants.ts"() {
37
+ "use strict";
38
+ RC_PROTECTED_REMOTE_FETCH_PATHNAME = "/rc-fetch-protected-remote";
39
+ CORS_DOCS_URL = "https://vercel.com/docs/remote-components/concepts/cors-external-urls#accessing-cross-site-protected-remote-components";
40
+ }
41
+ });
42
+
43
+ // src/shared/client/protected-rc-fallback.ts
44
+ function generateProtectedRcFallbackSrc(url) {
45
+ return `${RC_PROTECTED_REMOTE_FETCH_PATHNAME}?url=${encodeURIComponent(url)}`;
46
+ }
47
+ function isProxiedUrl(url) {
48
+ try {
49
+ return new URL(url, location.href).pathname === RC_PROTECTED_REMOTE_FETCH_PATHNAME;
50
+ } catch {
51
+ return false;
52
+ }
53
+ }
54
+ var init_protected_rc_fallback = __esm({
55
+ "src/shared/client/protected-rc-fallback.ts"() {
56
+ "use strict";
57
+ init_constants();
58
+ }
59
+ });
60
+
61
+ // src/shared/utils/abort.ts
62
+ function isAbortError(error) {
63
+ if (error instanceof DOMException && error.name === "AbortError") {
64
+ return true;
65
+ }
66
+ if (error !== null && typeof error === "object" && "name" in error && error.name === "AbortError" && "message" in error && typeof error.message === "string") {
67
+ const e = error;
68
+ return typeof e.code === "number" || e.constructor?.name === "DOMException";
69
+ }
70
+ return false;
71
+ }
72
+ var init_abort = __esm({
73
+ "src/shared/utils/abort.ts"() {
74
+ "use strict";
75
+ }
76
+ });
77
+
33
78
  // src/shared/error.ts
34
79
  function multipleRemoteComponentsError(url) {
35
80
  return new RemoteComponentsError(
@@ -42,10 +87,76 @@ function failedToFetchRemoteComponentError(url, { status, statusText }, help = "
42
87
  { cause: new Error(`${status} ${statusText}`) }
43
88
  );
44
89
  }
90
+ async function errorFromFailedFetch(originalUrl, resolvedUrl, res) {
91
+ const isProxied = isProxiedUrl(resolvedUrl.href);
92
+ if (isProxied && res) {
93
+ const body = await res.text().catch(() => "");
94
+ return failedProxyFetchError(
95
+ originalUrl,
96
+ resolvedUrl.href,
97
+ res.status,
98
+ body
99
+ );
100
+ }
101
+ const fallback = failedToFetchRemoteComponentError(
102
+ originalUrl,
103
+ res ?? { status: 0, statusText: "No Response" }
104
+ );
105
+ if (!res)
106
+ return fallback;
107
+ try {
108
+ const body = await res.text();
109
+ const parser = new DOMParser();
110
+ const doc = parser.parseFromString(body, "text/html");
111
+ const errorTemplate = doc.querySelector(
112
+ "template[data-next-error-message]"
113
+ );
114
+ const errorMessage = errorTemplate?.getAttribute("data-next-error-message");
115
+ if (errorMessage) {
116
+ const error = new RemoteComponentsError(errorMessage);
117
+ const errorStack = errorTemplate?.getAttribute("data-next-error-stack");
118
+ if (errorStack) {
119
+ error.stack = errorStack;
120
+ }
121
+ return error;
122
+ }
123
+ } catch (parseError) {
124
+ if (isAbortError(parseError))
125
+ throw parseError;
126
+ }
127
+ return fallback;
128
+ }
129
+ function failedProxyFetchError(originalUrl, proxyUrl, status, responseBody) {
130
+ if (status === 404) {
131
+ return new RemoteComponentsError(
132
+ `Could not proxy the request to "${originalUrl}" \u2014 the proxy endpoint "${RC_PROTECTED_REMOTE_FETCH_PATHNAME}" returned 404.
133
+
134
+ The host server needs middleware or a route that handles "${RC_PROTECTED_REMOTE_FETCH_PATHNAME}".
135
+
136
+ Proxying requires two pieces:
137
+ 1. resolveClientUrl={proxyClientRequestsThroughHost} on <RemoteComponent>
138
+ 2. Middleware or a route for "${RC_PROTECTED_REMOTE_FETCH_PATHNAME}" on the host server
139
+
140
+ Docs: ${CORS_DOCS_URL}`
141
+ );
142
+ }
143
+ if (status === 403) {
144
+ const detail = responseBody ? ` ${responseBody}` : "";
145
+ return new RemoteComponentsError(
146
+ `Proxied request to "${proxyUrl}" was forbidden.${detail} See: ${CORS_DOCS_URL}`
147
+ );
148
+ }
149
+ return new RemoteComponentsError(
150
+ `Proxied request for "${originalUrl}" via "${proxyUrl}" failed with status ${status}. See: ${CORS_DOCS_URL}`
151
+ );
152
+ }
45
153
  var RemoteComponentsError;
46
154
  var init_error = __esm({
47
155
  "src/shared/error.ts"() {
48
156
  "use strict";
157
+ init_protected_rc_fallback();
158
+ init_constants();
159
+ init_abort();
49
160
  RemoteComponentsError = class extends Error {
50
161
  code = "REMOTE_COMPONENTS_ERROR";
51
162
  constructor(message, options) {
@@ -56,15 +167,39 @@ var init_error = __esm({
56
167
  }
57
168
  });
58
169
 
170
+ // src/shared/utils/index.ts
171
+ function escapeString(str) {
172
+ return str.replace(/[^a-z0-9]/g, "_");
173
+ }
174
+ var init_utils = __esm({
175
+ "src/shared/utils/index.ts"() {
176
+ "use strict";
177
+ }
178
+ });
179
+
180
+ // src/shared/client/const.ts
181
+ function getBundleKey(bundle) {
182
+ return escapeString(bundle);
183
+ }
184
+ var DEFAULT_ROUTE, RUNTIME_WEBPACK, RUNTIME_TURBOPACK, RUNTIME_SCRIPT, REMOTE_COMPONENT_REGEX;
185
+ var init_const = __esm({
186
+ "src/shared/client/const.ts"() {
187
+ "use strict";
188
+ init_utils();
189
+ DEFAULT_ROUTE = "/";
190
+ RUNTIME_WEBPACK = "webpack";
191
+ RUNTIME_TURBOPACK = "turbopack";
192
+ RUNTIME_SCRIPT = "script";
193
+ REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
194
+ }
195
+ });
196
+
59
197
  // src/shared/utils/logger.ts
60
198
  function logDebug(location2, message) {
61
199
  if (DEBUG) {
62
200
  console.debug(`[${PREFIX}:${location2}]: ${message}`);
63
201
  }
64
202
  }
65
- function logInfo(location2, message) {
66
- console.info(`[${PREFIX}:${location2}]: ${message}`);
67
- }
68
203
  function logWarn(location2, message) {
69
204
  console.warn(`[${PREFIX}:${location2}]: ${message}`);
70
205
  }
@@ -75,10 +210,24 @@ function logError(location2, message, cause) {
75
210
  })
76
211
  );
77
212
  }
213
+ function warnCrossOriginFetchError(logLocation, url) {
214
+ try {
215
+ const parsed = typeof url === "string" ? new URL(url) : url;
216
+ if (typeof location === "undefined" || parsed.origin === location.origin) {
217
+ return;
218
+ }
219
+ logWarn(
220
+ logLocation,
221
+ `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}`
222
+ );
223
+ } catch {
224
+ }
225
+ }
78
226
  var PREFIX, DEBUG;
79
227
  var init_logger = __esm({
80
228
  "src/shared/utils/logger.ts"() {
81
229
  "use strict";
230
+ init_constants();
82
231
  init_error();
83
232
  PREFIX = "remote-components";
84
233
  DEBUG = typeof window !== "undefined" && localStorage.getItem("RC_DEBUG") === "true";
@@ -270,26 +419,6 @@ var init_shared_modules = __esm({
270
419
  }
271
420
  });
272
421
 
273
- // src/shared/constants.ts
274
- var RC_PROTECTED_REMOTE_FETCH_PATHNAME;
275
- var init_constants = __esm({
276
- "src/shared/constants.ts"() {
277
- "use strict";
278
- RC_PROTECTED_REMOTE_FETCH_PATHNAME = "/rc-fetch-protected-remote";
279
- }
280
- });
281
-
282
- // src/shared/client/protected-rc-fallback.ts
283
- function generateProtectedRcFallbackSrc(url) {
284
- return `${RC_PROTECTED_REMOTE_FETCH_PATHNAME}?url=${encodeURIComponent(url)}`;
285
- }
286
- var init_protected_rc_fallback = __esm({
287
- "src/shared/client/protected-rc-fallback.ts"() {
288
- "use strict";
289
- init_constants();
290
- }
291
- });
292
-
293
422
  // src/shared/client/webpack-patterns.ts
294
423
  var NEXT_BUNDLE_PATH_RE;
295
424
  var init_webpack_patterns = __esm({
@@ -300,7 +429,7 @@ var init_webpack_patterns = __esm({
300
429
  });
301
430
 
302
431
  // src/shared/client/script-loader.ts
303
- async function loadScripts(scripts) {
432
+ async function loadScripts(scripts, resolveClientUrl) {
304
433
  await Promise.all(
305
434
  scripts.map((script) => {
306
435
  return new Promise((resolve, reject) => {
@@ -309,42 +438,29 @@ async function loadScripts(scripts) {
309
438
  script.src.replace(NEXT_BUNDLE_PATH_RE, "/_next/"),
310
439
  location.origin
311
440
  ).href;
312
- const loadScriptWithProtectedRcFallback = (src, isFallback = false) => {
313
- const newScript = document.createElement("script");
314
- newScript.onload = () => {
315
- if (isFallback) {
316
- logInfo(
317
- "ScriptLoader",
318
- `Successfully loaded <script src="${newSrc}"> using fallback.`
319
- );
320
- }
321
- resolve();
322
- };
323
- newScript.onerror = () => {
324
- if (!isFallback) {
325
- const fallbackSrc = generateProtectedRcFallbackSrc(newSrc);
326
- logWarn(
327
- "ScriptLoader",
328
- `Failed to load <script src="${newSrc}"> for Remote Component. Trying fallback with ${RC_PROTECTED_REMOTE_FETCH_PATHNAME} (withRemoteComponentsHost)...`
329
- );
330
- loadScriptWithProtectedRcFallback(fallbackSrc, true);
331
- } else {
332
- logError(
333
- "ScriptLoader",
334
- `Failed to load fallback for <script src="${newSrc}"> for Remote Component.`
335
- );
336
- reject(
337
- new RemoteComponentsError(
338
- `Failed to load <script src="${newSrc}"> for Remote Component. Check the URL is correct.`
339
- )
340
- );
341
- }
342
- };
343
- newScript.src = src;
344
- newScript.async = true;
345
- document.head.appendChild(newScript);
441
+ const resolvedSrc = resolveClientUrl?.(newSrc) ?? newSrc;
442
+ const newScript = document.createElement("script");
443
+ newScript.onload = () => resolve();
444
+ newScript.onerror = () => {
445
+ const isProxied = isProxiedUrl(resolvedSrc);
446
+ if (isProxied) {
447
+ reject(
448
+ new RemoteComponentsError(
449
+ `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}`
450
+ )
451
+ );
452
+ } else {
453
+ warnCrossOriginFetchError("ScriptLoader", newSrc);
454
+ reject(
455
+ new RemoteComponentsError(
456
+ `Failed to load <script src="${newSrc}"> for Remote Component. Check the URL is correct.`
457
+ )
458
+ );
459
+ }
346
460
  };
347
- loadScriptWithProtectedRcFallback(newSrc);
461
+ newScript.src = resolvedSrc;
462
+ newScript.async = true;
463
+ document.head.appendChild(newScript);
348
464
  });
349
465
  })
350
466
  );
@@ -360,92 +476,6 @@ var init_script_loader = __esm({
360
476
  }
361
477
  });
362
478
 
363
- // src/shared/utils/index.ts
364
- function escapeString(str) {
365
- return str.replace(/[^a-z0-9]/g, "_");
366
- }
367
- var init_utils = __esm({
368
- "src/shared/utils/index.ts"() {
369
- "use strict";
370
- }
371
- });
372
-
373
- // src/shared/client/const.ts
374
- function getBundleKey(bundle) {
375
- return escapeString(bundle);
376
- }
377
- var RUNTIME_WEBPACK, RUNTIME_TURBOPACK, REMOTE_COMPONENT_REGEX;
378
- var init_const = __esm({
379
- "src/shared/client/const.ts"() {
380
- "use strict";
381
- init_utils();
382
- RUNTIME_WEBPACK = "webpack";
383
- RUNTIME_TURBOPACK = "turbopack";
384
- REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
385
- }
386
- });
387
-
388
- // src/shared/utils/abort.ts
389
- function isAbortError(error) {
390
- if (error instanceof DOMException && error.name === "AbortError") {
391
- return true;
392
- }
393
- if (error !== null && typeof error === "object" && "name" in error && error.name === "AbortError" && "message" in error && typeof error.message === "string") {
394
- const e = error;
395
- return typeof e.code === "number" || e.constructor?.name === "DOMException";
396
- }
397
- return false;
398
- }
399
- var init_abort = __esm({
400
- "src/shared/utils/abort.ts"() {
401
- "use strict";
402
- }
403
- });
404
-
405
- // src/shared/client/fetch-with-protected-rc-fallback.ts
406
- async function fetchWithProtectedRcFallback(url, init) {
407
- try {
408
- const res = await fetch(url, init);
409
- return res;
410
- } catch (error) {
411
- if (isAbortError(error)) {
412
- throw error;
413
- }
414
- const parsedUrl = new URL(url);
415
- if (typeof document === "object" && typeof document.location === "object" && document.location.origin !== parsedUrl.origin) {
416
- logWarn(
417
- "FetchRemoteComponent",
418
- "Request failed due to CORS, attempting to fetch it via the withRemoteComponentsHost proxy."
419
- );
420
- const proxiedRes = await fetch(
421
- generateProtectedRcFallbackSrc(parsedUrl.href),
422
- init?.signal ? { signal: init.signal } : void 0
423
- );
424
- if (proxiedRes.status === 200) {
425
- logInfo(
426
- "FetchRemoteComponent",
427
- `Successfully fetched ${parsedUrl.href} with fallback withRemoteComponentsHost proxy`
428
- );
429
- return proxiedRes;
430
- } else {
431
- logError(
432
- "FetchRemoteComponent",
433
- `Could not proxy remote: [response status ${proxiedRes.status}] ${await proxiedRes.text()}`
434
- );
435
- }
436
- }
437
- throw error;
438
- }
439
- }
440
- var init_fetch_with_protected_rc_fallback = __esm({
441
- "src/shared/client/fetch-with-protected-rc-fallback.ts"() {
442
- "use strict";
443
- init_protected_rc_fallback();
444
- init_abort();
445
- init_logger();
446
- }
447
- });
448
-
449
479
  // src/shared/client/turbopack-patterns.ts
450
480
  var REMOTE_SHARED_MARKER_RE, REMOTE_SHARED_ASSIGNMENT_RE, ASYNC_MODULE_LOADER_RE, ASYNC_MODULE_RESOLVE_RE, ASYNC_MODULE_ALL_RE, TURBOPACK_GLOBAL_RE;
451
481
  var init_turbopack_patterns = __esm({
@@ -461,7 +491,7 @@ var init_turbopack_patterns = __esm({
461
491
  });
462
492
 
463
493
  // src/shared/client/chunk-loader.ts
464
- function createChunkLoader(runtime) {
494
+ function createChunkLoader(runtime, resolveClientUrl) {
465
495
  return function __turbopack_chunk_load__(chunkId, scriptBundle) {
466
496
  logDebug("ChunkLoader", `Loading chunk: "${chunkId}"`);
467
497
  const self = globalThis;
@@ -501,9 +531,10 @@ function createChunkLoader(runtime) {
501
531
  logDebug("ChunkLoader", `Returning cached promise for: "${url}"`);
502
532
  return self.__remote_components_turbopack_chunk_loader_promise__[url];
503
533
  }
504
- logDebug("ChunkLoader", `Fetching chunk from: "${url}"`);
534
+ const resolvedUrl = resolveClientUrl?.(url) ?? url;
535
+ logDebug("ChunkLoader", `Fetching chunk from: "${resolvedUrl}"`);
505
536
  self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise((resolve, reject) => {
506
- fetchWithProtectedRcFallback(url).then((res) => res.text()).then((code) => {
537
+ fetch(resolvedUrl).then((res) => res.text()).then((code) => {
507
538
  const hasTurbopack = TURBOPACK_GLOBAL_RE.test(code);
508
539
  if (hasTurbopack) {
509
540
  return handleTurbopackChunk(code, bundle ?? "", url);
@@ -516,7 +547,19 @@ function createChunkLoader(runtime) {
516
547
  "ChunkLoader",
517
548
  `First 500 chars of chunk: ${code.slice(0, 500)}`
518
549
  );
519
- }).then(resolve).catch(reject);
550
+ }).then(resolve).catch((error) => {
551
+ const isProxied = isProxiedUrl(resolvedUrl);
552
+ if (isProxied) {
553
+ reject(
554
+ new RemoteComponentsError(
555
+ `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}`
556
+ )
557
+ );
558
+ } else {
559
+ warnCrossOriginFetchError("ChunkLoader", url);
560
+ reject(error);
561
+ }
562
+ });
520
563
  });
521
564
  return self.__remote_components_turbopack_chunk_loader_promise__[url];
522
565
  };
@@ -661,10 +704,11 @@ async function handleTurbopackChunk(code, bundle, url) {
661
704
  var init_chunk_loader = __esm({
662
705
  "src/shared/client/chunk-loader.ts"() {
663
706
  "use strict";
707
+ init_constants();
664
708
  init_error();
665
709
  init_logger();
666
710
  init_const();
667
- init_fetch_with_protected_rc_fallback();
711
+ init_protected_rc_fallback();
668
712
  init_turbopack_patterns();
669
713
  }
670
714
  });
@@ -1035,7 +1079,7 @@ var init_shared_modules2 = __esm({
1035
1079
  });
1036
1080
 
1037
1081
  // src/shared/client/webpack-adapter.ts
1038
- async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location.href), bundle, shared = {}, remoteShared = {}) {
1082
+ async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location.href), bundle, shared = {}, remoteShared = {}, resolveClientUrl) {
1039
1083
  const self = globalThis;
1040
1084
  if (!self.__remote_bundle_url__) {
1041
1085
  self.__remote_bundle_url__ = {};
@@ -1047,7 +1091,7 @@ async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location
1047
1091
  self.__original_webpack_chunk_load__ = self.__webpack_chunk_load__;
1048
1092
  self.__original_webpack_require__ = self.__webpack_require__;
1049
1093
  }
1050
- self.__webpack_chunk_load__ = createChunkLoader(runtime);
1094
+ self.__webpack_chunk_load__ = createChunkLoader(runtime, resolveClientUrl);
1051
1095
  self.__webpack_require__ = createModuleRequire(runtime);
1052
1096
  self.__webpack_require_type__ = runtime;
1053
1097
  if (self.__remote_webpack_require__ && runtime === RUNTIME_TURBOPACK) {
@@ -1141,27 +1185,21 @@ var init_webpack_adapter = __esm({
1141
1185
  });
1142
1186
 
1143
1187
  // src/shared/client/static-loader.ts
1144
- async function importViaProxy(absoluteSrc) {
1145
- const proxyUrl = new URL(
1146
- generateProtectedRcFallbackSrc(absoluteSrc),
1147
- location.href
1148
- ).href;
1149
- const response = await fetch(proxyUrl);
1188
+ async function importViaCallback(absoluteSrc, resolveClientUrl) {
1189
+ const resolvedUrl = resolveClientUrl(absoluteSrc) ?? absoluteSrc;
1190
+ const fetchUrl = new URL(resolvedUrl, location.href).href;
1191
+ const response = await fetch(fetchUrl);
1150
1192
  if (!response.ok)
1151
- throw new Error(`Proxy fetch failed: ${response.status}`);
1152
- logInfo(
1153
- "StaticLoader",
1154
- `Successfully loaded ${absoluteSrc} via protected RC proxy fallback.`
1155
- );
1193
+ throw new Error(`Proxied fetch failed: ${response.status}`);
1156
1194
  const content = (await response.text()).replace(/import\.meta\.url/g, JSON.stringify(absoluteSrc)).replace(
1157
1195
  /\b(from|import)\s*(["'])(\.\.?\/[^"']+)\2/g,
1158
1196
  (_, keyword, quote, relativePath) => {
1159
1197
  const absoluteImportUrl = new URL(relativePath, absoluteSrc).href;
1160
- const absoluteProxyUrl = new URL(
1161
- generateProtectedRcFallbackSrc(absoluteImportUrl),
1198
+ const resolvedImportUrl = new URL(
1199
+ resolveClientUrl(absoluteImportUrl) ?? absoluteImportUrl,
1162
1200
  location.href
1163
1201
  ).href;
1164
- return `${keyword} ${quote}${absoluteProxyUrl}${quote}`;
1202
+ return `${keyword} ${quote}${resolvedImportUrl}${quote}`;
1165
1203
  }
1166
1204
  );
1167
1205
  const moduleBlobUrl = URL.createObjectURL(
@@ -1195,6 +1233,20 @@ async function importViaProxy(absoluteSrc) {
1195
1233
  delete registry[absoluteSrc];
1196
1234
  return mod;
1197
1235
  }
1236
+ async function importDirectly(absoluteSrc) {
1237
+ try {
1238
+ return await import(
1239
+ /* @vite-ignore */
1240
+ /* webpackIgnore: true */
1241
+ absoluteSrc
1242
+ );
1243
+ } catch (importError) {
1244
+ if (!absoluteSrc.startsWith("blob:")) {
1245
+ warnCrossOriginFetchError("StaticLoader", absoluteSrc);
1246
+ }
1247
+ throw importError;
1248
+ }
1249
+ }
1198
1250
  function resolveScriptSrc(script, url) {
1199
1251
  const rawSrc = typeof script.getAttribute === "function" ? script.getAttribute("src") ?? script.src : script.src;
1200
1252
  if (!rawSrc && script.textContent) {
@@ -1207,24 +1259,7 @@ function resolveScriptSrc(script, url) {
1207
1259
  }
1208
1260
  return rawSrc;
1209
1261
  }
1210
- async function importScriptMod(absoluteSrc) {
1211
- try {
1212
- return await import(
1213
- /* @vite-ignore */
1214
- /* webpackIgnore: true */
1215
- absoluteSrc
1216
- );
1217
- } catch (importError) {
1218
- if (absoluteSrc.startsWith("blob:"))
1219
- throw importError;
1220
- logWarn(
1221
- "StaticLoader",
1222
- `Direct import of ${absoluteSrc} failed, attempting via protected RC proxy fallback.`
1223
- );
1224
- return importViaProxy(absoluteSrc);
1225
- }
1226
- }
1227
- async function loadStaticRemoteComponent(scripts, url) {
1262
+ async function loadStaticRemoteComponent(scripts, url, resolveClientUrl) {
1228
1263
  const self = globalThis;
1229
1264
  if (self.__remote_script_entrypoint_mount__?.[url.href]) {
1230
1265
  self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
@@ -1237,7 +1272,7 @@ async function loadStaticRemoteComponent(scripts, url) {
1237
1272
  try {
1238
1273
  const src = resolveScriptSrc(script, url);
1239
1274
  const absoluteSrc = new URL(src, url).href;
1240
- const mod = await importScriptMod(absoluteSrc);
1275
+ const mod = resolveClientUrl ? await importViaCallback(absoluteSrc, resolveClientUrl) : await importDirectly(absoluteSrc);
1241
1276
  if (src.startsWith("blob:")) {
1242
1277
  URL.revokeObjectURL(src);
1243
1278
  }
@@ -1302,7 +1337,6 @@ var init_static_loader = __esm({
1302
1337
  "src/shared/client/static-loader.ts"() {
1303
1338
  "use strict";
1304
1339
  init_logger();
1305
- init_protected_rc_fallback();
1306
1340
  }
1307
1341
  });
1308
1342
 
@@ -1512,7 +1546,7 @@ var webpack_exports = {};
1512
1546
  __export(webpack_exports, {
1513
1547
  webpackRuntime: () => webpackRuntime
1514
1548
  });
1515
- async function webpackRuntime(bundle, shared, remoteShared) {
1549
+ async function webpackRuntime(bundle, shared, remoteShared, resolveClientUrl) {
1516
1550
  const self = globalThis;
1517
1551
  if (!self.__DISABLE_WEBPACK_EXEC__) {
1518
1552
  self.__DISABLE_WEBPACK_EXEC__ = {};
@@ -1555,7 +1589,7 @@ async function webpackRuntime(bundle, shared, remoteShared) {
1555
1589
  }
1556
1590
  ];
1557
1591
  });
1558
- await loadScripts(scriptSrcs);
1592
+ await loadScripts(scriptSrcs, resolveClientUrl);
1559
1593
  const hostShared = {
1560
1594
  ...sharedPolyfills(shared),
1561
1595
  ...self.__remote_component_host_shared_modules__,
@@ -1612,7 +1646,7 @@ var turbopack_exports = {};
1612
1646
  __export(turbopack_exports, {
1613
1647
  turbopackRuntime: () => turbopackRuntime
1614
1648
  });
1615
- async function turbopackRuntime(url, bundle, shared, remoteShared) {
1649
+ async function turbopackRuntime(url, bundle, shared, remoteShared, resolveClientUrl) {
1616
1650
  const self = globalThis;
1617
1651
  const hostShared = {
1618
1652
  ...sharedPolyfills(shared),
@@ -1626,7 +1660,8 @@ async function turbopackRuntime(url, bundle, shared, remoteShared) {
1626
1660
  url,
1627
1661
  bundle,
1628
1662
  hostShared,
1629
- remoteShared
1663
+ remoteShared,
1664
+ resolveClientUrl
1630
1665
  );
1631
1666
  const {
1632
1667
  default: { createFromReadableStream }
@@ -1640,7 +1675,8 @@ async function turbopackRuntime(url, bundle, shared, remoteShared) {
1640
1675
  url,
1641
1676
  bundle,
1642
1677
  hostShared,
1643
- remoteShared
1678
+ remoteShared,
1679
+ resolveClientUrl
1644
1680
  );
1645
1681
  }
1646
1682
  return {
@@ -1666,7 +1702,7 @@ var script_exports = {};
1666
1702
  __export(script_exports, {
1667
1703
  scriptRuntime: () => scriptRuntime
1668
1704
  });
1669
- function scriptRuntime() {
1705
+ function scriptRuntime(resolveClientUrl) {
1670
1706
  const self = globalThis;
1671
1707
  return {
1672
1708
  self,
@@ -1676,7 +1712,7 @@ function scriptRuntime() {
1676
1712
  Component: null,
1677
1713
  App: null
1678
1714
  }),
1679
- preloadScripts: loadStaticRemoteComponent
1715
+ preloadScripts: (scripts, url) => loadStaticRemoteComponent(scripts, url, resolveClientUrl)
1680
1716
  };
1681
1717
  }
1682
1718
  var init_script = __esm({
@@ -1689,6 +1725,7 @@ var init_script = __esm({
1689
1725
  // src/html/host/index.tsx
1690
1726
  var host_exports = {};
1691
1727
  __export(host_exports, {
1728
+ proxyClientRequestsThroughHost: () => proxyClientRequestsThroughHost,
1692
1729
  registerSharedModules: () => registerSharedModules
1693
1730
  });
1694
1731
  module.exports = __toCommonJS(host_exports);
@@ -1707,7 +1744,7 @@ var tagNames = [
1707
1744
  "script",
1708
1745
  "link"
1709
1746
  ];
1710
- function applyOriginToNodes(doc, url) {
1747
+ function applyOriginToNodes(doc, url, resolveClientUrl) {
1711
1748
  if (url.origin !== location.origin) {
1712
1749
  const nodes = doc.querySelectorAll(
1713
1750
  tagNames.map(
@@ -1716,12 +1753,15 @@ function applyOriginToNodes(doc, url) {
1716
1753
  );
1717
1754
  nodes.forEach((node) => {
1718
1755
  if (node.hasAttribute("src") && /^[./]+\/?/.test(node.getAttribute("src") ?? "")) {
1719
- node.src = new URL(node.getAttribute("src") ?? "/", url).href;
1756
+ const absoluteSrc = new URL(node.getAttribute("src") ?? "/", url).href;
1757
+ const isScript = node.tagName.toLowerCase() === "script";
1758
+ node.src = isScript ? absoluteSrc : resolveClientUrl?.(absoluteSrc) ?? absoluteSrc;
1720
1759
  }
1721
1760
  if (node.hasAttribute("href") && /^[./]+\/?/.test(node.getAttribute("href") ?? "")) {
1761
+ const absoluteHref = new URL(node.getAttribute("href") ?? "/", url).href;
1722
1762
  node.setAttribute(
1723
1763
  "href",
1724
- new URL(node.getAttribute("href") ?? "/", url).href
1764
+ resolveClientUrl?.(absoluteHref) ?? absoluteHref
1725
1765
  );
1726
1766
  }
1727
1767
  if (node.hasAttribute("srcset")) {
@@ -1730,7 +1770,8 @@ function applyOriginToNodes(doc, url) {
1730
1770
  if (!urlPart)
1731
1771
  return entry;
1732
1772
  const absoluteUrl = new URL(urlPart, url).href;
1733
- return descriptor ? `${absoluteUrl} ${descriptor}` : absoluteUrl;
1773
+ const resolvedUrl = resolveClientUrl?.(absoluteUrl) ?? absoluteUrl;
1774
+ return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl;
1734
1775
  }).join(", ");
1735
1776
  if (srcSet) {
1736
1777
  node.setAttribute("srcset", srcSet);
@@ -1742,7 +1783,8 @@ function applyOriginToNodes(doc, url) {
1742
1783
  if (!urlPart)
1743
1784
  return entry;
1744
1785
  const absoluteUrl = new URL(urlPart, url).href;
1745
- return descriptor ? `${absoluteUrl} ${descriptor}` : absoluteUrl;
1786
+ const resolvedUrl = resolveClientUrl?.(absoluteUrl) ?? absoluteUrl;
1787
+ return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl;
1746
1788
  }).join(", ");
1747
1789
  if (srcSet) {
1748
1790
  node.setAttribute("imagesrcset", srcSet);
@@ -1752,6 +1794,144 @@ function applyOriginToNodes(doc, url) {
1752
1794
  }
1753
1795
  }
1754
1796
 
1797
+ // src/shared/client/proxy-through-host.ts
1798
+ init_protected_rc_fallback();
1799
+ function withRemoteSrc(resolveClientUrl, remoteSrc) {
1800
+ const remoteOrigin = parseOrigin(remoteSrc);
1801
+ return (url) => {
1802
+ const urlOrigin = parseOrigin(url);
1803
+ if (remoteOrigin && urlOrigin && urlOrigin !== remoteOrigin) {
1804
+ return void 0;
1805
+ }
1806
+ return resolveClientUrl(remoteSrc, url);
1807
+ };
1808
+ }
1809
+ function parseOrigin(url) {
1810
+ try {
1811
+ return new URL(url).origin;
1812
+ } catch {
1813
+ return void 0;
1814
+ }
1815
+ }
1816
+ var proxyClientRequestsThroughHost = (remoteSrc, url) => {
1817
+ if (typeof location === "undefined") {
1818
+ return void 0;
1819
+ }
1820
+ const remoteOrigin = new URL(remoteSrc, location.href).origin;
1821
+ if (remoteOrigin === location.origin) {
1822
+ return void 0;
1823
+ }
1824
+ try {
1825
+ const parsed = new URL(url, location.href);
1826
+ if (parsed.origin === remoteOrigin) {
1827
+ return generateProtectedRcFallbackSrc(url);
1828
+ }
1829
+ } catch {
1830
+ }
1831
+ return void 0;
1832
+ };
1833
+
1834
+ // src/shared/client/default-resolve-client-url.ts
1835
+ function bindResolveClientUrl(prop, remoteSrc) {
1836
+ return prop ? withRemoteSrc(prop, remoteSrc) : void 0;
1837
+ }
1838
+
1839
+ // src/shared/client/parse-remote-html.ts
1840
+ init_error();
1841
+ init_const();
1842
+ function validateSingleComponent(doc, name, url) {
1843
+ if (doc.querySelectorAll("div[data-bundle][data-route]").length > 1 && !doc.querySelector(`div[data-bundle][data-route][id^="${name}"]`) || doc.querySelectorAll("remote-component:not([src])").length > 1 && !doc.querySelector(`remote-component[name="${name}"]`)) {
1844
+ throw multipleRemoteComponentsError(url);
1845
+ }
1846
+ }
1847
+ function findComponentElement(doc, name) {
1848
+ return doc.querySelector(`div[data-bundle][data-route][id^="${name}"]`) ?? doc.querySelector("div[data-bundle][data-route]") ?? doc.querySelector("div#__next") ?? doc.querySelector(`remote-component[name="${name}"]:not([src])`) ?? doc.querySelector("remote-component:not([src])");
1849
+ }
1850
+ function parseNextData(doc) {
1851
+ return JSON.parse(
1852
+ (doc.querySelector("#__NEXT_DATA__") ?? doc.querySelector("#__REMOTE_NEXT_DATA__"))?.textContent ?? "null"
1853
+ );
1854
+ }
1855
+ function resolveComponentName(component, nextData, fallbackName) {
1856
+ const isRemoteComponent = component?.tagName.toLowerCase() === "remote-component";
1857
+ const name = component?.getAttribute("id")?.replace(/_ssr$/, "") || isRemoteComponent && component?.getAttribute("name") || (nextData ? "__next" : fallbackName);
1858
+ return { name, isRemoteComponent };
1859
+ }
1860
+ function extractComponentMetadata(component, nextData, name, url) {
1861
+ return {
1862
+ name,
1863
+ bundle: component?.getAttribute("data-bundle") || nextData?.props.__REMOTE_COMPONENT__?.bundle || "default",
1864
+ route: component?.getAttribute("data-route") ?? nextData?.page ?? (url.pathname || DEFAULT_ROUTE),
1865
+ runtime: component?.getAttribute("data-runtime") ?? (nextData?.props.__REMOTE_COMPONENT__?.runtime || RUNTIME_SCRIPT)
1866
+ };
1867
+ }
1868
+ function extractRemoteShared(doc, name, nextData) {
1869
+ const remoteSharedEl = doc.querySelector(
1870
+ `#${name}_shared[data-remote-components-shared]`
1871
+ );
1872
+ const remoteShared = nextData?.props.__REMOTE_COMPONENT__?.shared ?? (JSON.parse(remoteSharedEl?.textContent ?? "{}") ?? {});
1873
+ remoteSharedEl?.remove();
1874
+ return remoteShared;
1875
+ }
1876
+ function validateComponentFound(component, rsc, nextData, isRemoteComponent, url, name) {
1877
+ if (!component || !(rsc || nextData || isRemoteComponent)) {
1878
+ throw new RemoteComponentsError(
1879
+ `Remote Component not found on ${url}.${name !== "__vercel_remote_component" ? ` The name for the <RemoteComponent> is "${name}". Check <RemoteComponent> usage.` : ""} Did you forget to wrap the content in <RemoteComponent>?`
1880
+ );
1881
+ }
1882
+ }
1883
+ function extractLinks(doc, component) {
1884
+ return Array.from(doc.querySelectorAll("link[href]")).filter(
1885
+ (link) => !component.contains(link)
1886
+ );
1887
+ }
1888
+ function extractScripts(doc, component, isRemoteComponent) {
1889
+ return Array.from(
1890
+ (isRemoteComponent ? component : doc).querySelectorAll(
1891
+ "script[src],script[data-src]"
1892
+ )
1893
+ );
1894
+ }
1895
+ function parseRemoteComponentDocument(doc, name, url) {
1896
+ validateSingleComponent(doc, name, url.href);
1897
+ const component = findComponentElement(doc, name);
1898
+ const nextData = parseNextData(doc);
1899
+ const { name: resolvedName, isRemoteComponent } = resolveComponentName(
1900
+ component,
1901
+ nextData,
1902
+ name
1903
+ );
1904
+ const rsc = doc.querySelector(`#${resolvedName}_rsc`);
1905
+ const metadata = extractComponentMetadata(
1906
+ component,
1907
+ nextData,
1908
+ resolvedName,
1909
+ url
1910
+ );
1911
+ const remoteShared = extractRemoteShared(doc, resolvedName, nextData);
1912
+ validateComponentFound(
1913
+ component,
1914
+ rsc,
1915
+ nextData,
1916
+ isRemoteComponent,
1917
+ url.href,
1918
+ resolvedName
1919
+ );
1920
+ const links = extractLinks(doc, component);
1921
+ const scripts = extractScripts(doc, component, isRemoteComponent);
1922
+ return {
1923
+ component,
1924
+ name: resolvedName,
1925
+ isRemoteComponent,
1926
+ metadata,
1927
+ nextData,
1928
+ rsc,
1929
+ remoteShared,
1930
+ links,
1931
+ scripts
1932
+ };
1933
+ }
1934
+
1755
1935
  // src/shared/client/remote-component.ts
1756
1936
  init_error();
1757
1937
 
@@ -1842,11 +2022,37 @@ init_script_loader();
1842
2022
  init_static_loader();
1843
2023
  init_webpack_adapter();
1844
2024
 
2025
+ // src/shared/contract/host-state.ts
2026
+ function createHostState() {
2027
+ return {
2028
+ stage: "idle",
2029
+ prevSrc: void 0,
2030
+ prevUrl: void 0,
2031
+ prevName: void 0,
2032
+ prevIsRemoteComponent: false,
2033
+ abortController: void 0
2034
+ };
2035
+ }
2036
+
2037
+ // src/shared/contract/resolve-name-from-src.ts
2038
+ function resolveNameFromSrc(src, defaultName) {
2039
+ if (!src) {
2040
+ return defaultName;
2041
+ }
2042
+ const hash = typeof src === "string" ? src : src.hash;
2043
+ const hashIndex = hash.indexOf("#");
2044
+ if (hashIndex < 0) {
2045
+ return defaultName;
2046
+ }
2047
+ const name = hash.slice(hashIndex + 1);
2048
+ return name || defaultName;
2049
+ }
2050
+
1845
2051
  // src/html/host/index.tsx
1846
2052
  init_error();
1847
2053
 
1848
2054
  // src/shared/ssr/fetch-with-hooks.ts
1849
- init_fetch_with_protected_rc_fallback();
2055
+ init_logger();
1850
2056
 
1851
2057
  // src/shared/ssr/fetch-headers.ts
1852
2058
  function remoteFetchHeaders() {
@@ -1884,7 +2090,12 @@ async function fetchWithHooks(url, additionalInit, options = {}) {
1884
2090
  };
1885
2091
  let res = await onRequest?.(url, init, hookOptions);
1886
2092
  if (!res) {
1887
- res = await fetchWithProtectedRcFallback(url, init);
2093
+ try {
2094
+ res = await fetch(url, init);
2095
+ } catch (error) {
2096
+ warnCrossOriginFetchError("FetchRemoteComponent", url);
2097
+ throw error;
2098
+ }
1888
2099
  }
1889
2100
  const transformedRes = await onResponse?.(url, res, hookOptions);
1890
2101
  if (transformedRes) {
@@ -1893,6 +2104,15 @@ async function fetchWithHooks(url, additionalInit, options = {}) {
1893
2104
  return res;
1894
2105
  }
1895
2106
 
2107
+ // src/shared/ssr/get-client-or-server-url.ts
2108
+ function getClientOrServerUrl(src, serverFallback) {
2109
+ const fallback = typeof location !== "undefined" ? location.href : serverFallback;
2110
+ if (!src) {
2111
+ return new URL(fallback);
2112
+ }
2113
+ return typeof src === "string" ? new URL(src, fallback) : src;
2114
+ }
2115
+
1896
2116
  // src/html/host/index.tsx
1897
2117
  init_utils();
1898
2118
  init_abort();
@@ -1907,7 +2127,8 @@ async function attachStyles({
1907
2127
  signal,
1908
2128
  baseUrl,
1909
2129
  remoteComponentSrc,
1910
- root
2130
+ root,
2131
+ resolveClientUrl
1911
2132
  }) {
1912
2133
  const appendedLinks = [];
1913
2134
  let abortReject = null;
@@ -1942,9 +2163,13 @@ async function attachStyles({
1942
2163
  });
1943
2164
  for (const attr of link.attributes) {
1944
2165
  if (attr.name === "href") {
2166
+ const absoluteHref = new URL(
2167
+ attr.value,
2168
+ baseUrl ?? location.origin
2169
+ ).href;
1945
2170
  newLink.setAttribute(
1946
2171
  attr.name,
1947
- new URL(attr.value, baseUrl ?? location.origin).href
2172
+ resolveClientUrl?.(absoluteHref) ?? absoluteHref
1948
2173
  );
1949
2174
  } else {
1950
2175
  newLink.setAttribute(attr.name, attr.value);
@@ -1978,7 +2203,7 @@ async function attachStyles({
1978
2203
 
1979
2204
  // src/html/host/runtime/index.ts
1980
2205
  init_error();
1981
- async function getRuntime(type, url, bundle, shared, remoteShared) {
2206
+ async function getRuntime(type, url, bundle, shared, remoteShared, resolveClientUrl) {
1982
2207
  if (typeof globalThis.process === "undefined") {
1983
2208
  globalThis.process = {
1984
2209
  env: {}
@@ -1986,13 +2211,24 @@ async function getRuntime(type, url, bundle, shared, remoteShared) {
1986
2211
  }
1987
2212
  if (type === "webpack") {
1988
2213
  const { webpackRuntime: webpackRuntime2 } = await Promise.resolve().then(() => (init_webpack(), webpack_exports));
1989
- return webpackRuntime2(bundle ?? "default", shared, remoteShared);
2214
+ return webpackRuntime2(
2215
+ bundle ?? "default",
2216
+ shared,
2217
+ remoteShared,
2218
+ resolveClientUrl
2219
+ );
1990
2220
  } else if (type === "turbopack") {
1991
2221
  const { turbopackRuntime: turbopackRuntime2 } = await Promise.resolve().then(() => (init_turbopack(), turbopack_exports));
1992
- return turbopackRuntime2(url, bundle, shared, remoteShared);
2222
+ return turbopackRuntime2(
2223
+ url,
2224
+ bundle,
2225
+ shared,
2226
+ remoteShared,
2227
+ resolveClientUrl
2228
+ );
1993
2229
  } else if (type === "script") {
1994
2230
  const { scriptRuntime: scriptRuntime2 } = await Promise.resolve().then(() => (init_script(), script_exports));
1995
- return scriptRuntime2();
2231
+ return scriptRuntime2(resolveClientUrl);
1996
2232
  }
1997
2233
  throw new RemoteComponentsError(
1998
2234
  `Remote Components runtime "${type}" is not supported. Supported runtimes are "webpack", "turbopack", and "script".`
@@ -2008,41 +2244,76 @@ if (typeof HTMLElement !== "undefined") {
2008
2244
  fallbackSlot;
2009
2245
  __next = null;
2010
2246
  fouc = null;
2011
- isLoading = false;
2012
- prevIsRemoteComponent = false;
2013
- prevUrl;
2014
- prevSrc = null;
2015
- prevName;
2247
+ hostState = createHostState();
2016
2248
  root = null;
2017
2249
  reactRoot;
2018
2250
  onRequest;
2019
2251
  onResponse;
2020
- /** Current AbortController for the loading operation - can be used to cancel loading via controller.abort() */
2021
- abortController;
2252
+ resolveClientUrl;
2253
+ // -- HostConfig property accessors (attribute-reflected) --
2254
+ get src() {
2255
+ return this.getAttribute("src") ?? void 0;
2256
+ }
2257
+ set src(value) {
2258
+ if (value == null) {
2259
+ this.removeAttribute("src");
2260
+ } else {
2261
+ this.setAttribute("src", String(value));
2262
+ }
2263
+ }
2264
+ /** Always `true` — the HTML host always isolates via Shadow DOM. */
2265
+ get isolate() {
2266
+ return true;
2267
+ }
2268
+ get mode() {
2269
+ const attr = this.getAttribute("mode");
2270
+ return attr === "closed" ? "closed" : "open";
2271
+ }
2272
+ set mode(value) {
2273
+ if (value) {
2274
+ this.setAttribute("mode", value);
2275
+ }
2276
+ }
2277
+ get reset() {
2278
+ return this.getAttribute("reset") !== null;
2279
+ }
2280
+ set reset(value) {
2281
+ if (value) {
2282
+ this.setAttribute("reset", "");
2283
+ } else {
2284
+ this.removeAttribute("reset");
2285
+ }
2286
+ }
2287
+ get credentials() {
2288
+ return this.getAttribute("credentials") || "same-origin";
2289
+ }
2290
+ set credentials(value) {
2291
+ if (value) {
2292
+ this.setAttribute("credentials", value);
2293
+ } else {
2294
+ this.removeAttribute("credentials");
2295
+ }
2296
+ }
2022
2297
  static get observedAttributes() {
2023
2298
  return ["src", "name", "mode"];
2024
2299
  }
2025
- // watch for src attribute changes
2026
- // this is required to reload the remote component when the src attribute is added later
2027
- // this is for rendering the custom element using React
2300
+ dispatchLifecycleEvent(type, detail) {
2301
+ const event = new Event(type, { bubbles: true, composed: true });
2302
+ if (detail) {
2303
+ Object.assign(event, detail);
2304
+ }
2305
+ this.dispatchEvent(event);
2306
+ }
2028
2307
  attributeChangedCallback(name, oldValue, newValue) {
2029
2308
  if ((name === "src" || name === "name") && oldValue !== newValue) {
2030
- if (this.getAttribute("src")) {
2309
+ if (this.src) {
2031
2310
  this.load().catch((e) => {
2032
2311
  if (isAbortError(e)) {
2033
2312
  return;
2034
2313
  }
2035
2314
  logError("HtmlHost", "Error loading remote component.", e);
2036
- const errorEvent = new Event("error", {
2037
- bubbles: true,
2038
- composed: true
2039
- });
2040
- Object.assign(errorEvent, {
2041
- error: e,
2042
- src: this.getAttribute("src")
2043
- });
2044
- this.dispatchEvent(errorEvent);
2045
- this.isLoading = false;
2315
+ this.dispatchLifecycleEvent("error", { error: e, src: this.src });
2316
+ this.hostState.stage = "error";
2046
2317
  });
2047
2318
  }
2048
2319
  } else if (name === "mode" && oldValue !== newValue && this.root) {
@@ -2058,15 +2329,7 @@ if (typeof HTMLElement !== "undefined") {
2058
2329
  return;
2059
2330
  }
2060
2331
  logError("HtmlHost", "Error reloading remote component.", e);
2061
- const errorEvent = new Event("error", {
2062
- bubbles: true,
2063
- composed: true
2064
- });
2065
- Object.assign(errorEvent, {
2066
- error: e,
2067
- src: this.getAttribute("src")
2068
- });
2069
- this.dispatchEvent(errorEvent);
2332
+ this.dispatchLifecycleEvent("error", { error: e, src: this.src });
2070
2333
  });
2071
2334
  }
2072
2335
  }
@@ -2076,9 +2339,9 @@ if (typeof HTMLElement !== "undefined") {
2076
2339
  resolve(void 0);
2077
2340
  });
2078
2341
  });
2079
- if (this.isLoading) {
2080
- this.abortController?.abort();
2081
- this.isLoading = false;
2342
+ if (this.hostState.stage === "loading") {
2343
+ this.hostState.abortController?.abort();
2344
+ this.hostState.stage = "idle";
2082
2345
  if (this.root && !this.reactRoot) {
2083
2346
  this.root.innerHTML = "";
2084
2347
  this.fouc = null;
@@ -2088,29 +2351,24 @@ if (typeof HTMLElement !== "undefined") {
2088
2351
  }
2089
2352
  if (!this.root) {
2090
2353
  this.root = this.attachShadow({
2091
- mode: this.getAttribute("mode") === "closed" ? "closed" : "open"
2354
+ mode: this.mode === "closed" ? "closed" : "open"
2092
2355
  });
2093
2356
  this.fallbackSlot = document.createElement("slot");
2094
2357
  this.root.appendChild(this.fallbackSlot);
2095
2358
  }
2096
2359
  this.name = this.getAttribute("name") || "__vercel_remote_component";
2097
2360
  this.bundle = "default";
2098
- this.isLoading = true;
2099
- const src = this.getAttribute("src");
2100
- this.abortController = new AbortController();
2101
- const signal = this.abortController.signal;
2102
- const isCurrentLoad = () => !signal.aborted && this.getAttribute("src") === src;
2361
+ this.hostState.stage = "loading";
2362
+ const src = this.src;
2363
+ this.hostState.abortController = new AbortController();
2364
+ const signal = this.hostState.abortController.signal;
2365
+ const isCurrentLoad = () => !signal.aborted && this.src === src;
2103
2366
  const abandonLoad = () => {
2104
- if (this.abortController?.signal === signal && this.isLoading) {
2105
- this.isLoading = false;
2367
+ if (this.hostState.abortController?.signal === signal && this.hostState.stage === "loading") {
2368
+ this.hostState.stage = "idle";
2106
2369
  }
2107
2370
  };
2108
- const beforeLoadEvent = new Event("beforeload", {
2109
- bubbles: true,
2110
- composed: true
2111
- });
2112
- Object.assign(beforeLoadEvent, { src });
2113
- this.dispatchEvent(beforeLoadEvent);
2371
+ this.dispatchLifecycleEvent("beforeload", { src });
2114
2372
  const remoteComponentChild = this.querySelector("div#__REMOTE_COMPONENT__") || this.querySelector("div[data-bundle][data-route]");
2115
2373
  if (!src && !remoteComponentChild) {
2116
2374
  throw new RemoteComponentsError('"src" attribute is required');
@@ -2118,21 +2376,27 @@ if (typeof HTMLElement !== "undefined") {
2118
2376
  let url = null;
2119
2377
  let html = this.innerHTML;
2120
2378
  if (src) {
2121
- url = new URL(src, window.location.href);
2122
- if (url.hash) {
2123
- this.name = url.hash.slice(1);
2124
- }
2379
+ url = getClientOrServerUrl(src, window.location.href);
2380
+ this.name = resolveNameFromSrc(
2381
+ src,
2382
+ this.name ?? "__vercel_remote_component"
2383
+ );
2125
2384
  }
2385
+ const resolveClientUrl = url ? bindResolveClientUrl(this.resolveClientUrl, url.href) : void 0;
2126
2386
  if (!remoteComponentChild && url) {
2127
2387
  const fetchInit = {
2128
- credentials: this.getAttribute("credentials") || "same-origin"
2388
+ credentials: this.credentials || "same-origin"
2129
2389
  };
2390
+ const resolvedUrl = new URL(
2391
+ resolveClientUrl?.(url.href) ?? url.href,
2392
+ window.location.href
2393
+ );
2130
2394
  let res;
2131
2395
  try {
2132
- res = await fetchWithHooks(url, fetchInit, {
2396
+ res = await fetchWithHooks(resolvedUrl, fetchInit, {
2133
2397
  onRequest: this.onRequest,
2134
2398
  onResponse: this.onResponse,
2135
- abortController: this.abortController
2399
+ abortController: this.hostState.abortController
2136
2400
  });
2137
2401
  } catch (e) {
2138
2402
  if (isAbortError(e)) {
@@ -2141,34 +2405,7 @@ if (typeof HTMLElement !== "undefined") {
2141
2405
  throw e;
2142
2406
  }
2143
2407
  if (!res || !res.ok) {
2144
- let error = failedToFetchRemoteComponentError(
2145
- url.href,
2146
- res ?? new Response(null, { status: 0 })
2147
- );
2148
- try {
2149
- const body = res ? await res.text() : "";
2150
- const parser2 = new DOMParser();
2151
- const doc2 = parser2.parseFromString(body, "text/html");
2152
- const errorTemplate = doc2.querySelector(
2153
- "template[data-next-error-message]"
2154
- );
2155
- const errorMessage = errorTemplate?.getAttribute(
2156
- "data-next-error-message"
2157
- );
2158
- const errorStack = errorTemplate?.getAttribute(
2159
- "data-next-error-stack"
2160
- );
2161
- if (errorMessage) {
2162
- error = new RemoteComponentsError(errorMessage);
2163
- if (errorStack) {
2164
- error.stack = errorStack;
2165
- }
2166
- }
2167
- } catch (parseError) {
2168
- if (isAbortError(parseError))
2169
- throw parseError;
2170
- }
2171
- throw error;
2408
+ throw await errorFromFailedFetch(url.href, resolvedUrl, res);
2172
2409
  }
2173
2410
  try {
2174
2411
  html = await res.text();
@@ -2184,29 +2421,27 @@ if (typeof HTMLElement !== "undefined") {
2184
2421
  }
2185
2422
  const parser = new DOMParser();
2186
2423
  const doc = parser.parseFromString(html, "text/html");
2187
- if (doc.querySelectorAll("div[data-bundle][data-route]").length > 1 && !doc.querySelector(
2188
- `div[data-bundle][data-route][id^="${this.name}"]`
2189
- ) || doc.querySelectorAll("remote-component").length > 1 && !doc.querySelector(`remote-component[name="${this.name}"]`)) {
2190
- throw multipleRemoteComponentsError(
2191
- url?.href ?? (this.getAttribute("src") || "unknown")
2192
- );
2193
- }
2194
- const component = doc.querySelector(`div[data-bundle][data-route][id^="${this.name}"]`) ?? // fallback to the first element with the data-bundle and data-route attributes when not using a named remote component
2195
- doc.querySelector("div[data-bundle][data-route]") ?? // fallback to Next.js Pages Router
2196
- doc.querySelector("div#__next") ?? // fallback to a <remote-component> element
2197
- doc.querySelector(`remote-component[name="${this.name}"]:not([src])`) ?? doc.querySelector("remote-component:not([src])");
2198
- const nextData = JSON.parse(
2199
- (doc.querySelector("#__NEXT_DATA__") ?? doc.querySelector("#__REMOTE_NEXT_DATA__"))?.textContent ?? "null"
2424
+ const parsed = parseRemoteComponentDocument(
2425
+ doc,
2426
+ this.name ?? "__vercel_remote_component",
2427
+ url ?? new URL(window.location.href)
2200
2428
  );
2201
- const isRemoteComponent = component?.tagName.toLowerCase() === "remote-component";
2429
+ const {
2430
+ component,
2431
+ name: resolvedName,
2432
+ isRemoteComponent,
2433
+ metadata: parsedMetadata,
2434
+ nextData,
2435
+ rsc,
2436
+ remoteShared
2437
+ } = parsed;
2202
2438
  if (nextData && nextData.buildId === "development" && !this.reactRoot) {
2203
2439
  this.fouc = document.createElement("style");
2204
2440
  this.fouc.textContent = `:host { display: none; }`;
2205
2441
  this.root.appendChild(this.fouc);
2206
2442
  }
2207
- this.name = component?.getAttribute("id")?.replace(/_ssr$/, "") || isRemoteComponent && component.getAttribute("name") || (nextData ? "__next" : this.name);
2208
- const rsc = doc.querySelector(`#${this.name}_rsc`);
2209
- this.bundle = component?.getAttribute("data-bundle") || nextData?.props.__REMOTE_COMPONENT__?.bundle || this.bundle;
2443
+ this.name = resolvedName;
2444
+ this.bundle = parsedMetadata.bundle;
2210
2445
  if (url) {
2211
2446
  const self2 = globalThis;
2212
2447
  if (!self2.__remote_bundle_url__) {
@@ -2214,38 +2449,28 @@ if (typeof HTMLElement !== "undefined") {
2214
2449
  }
2215
2450
  self2.__remote_bundle_url__[this.bundle ?? "default"] = url;
2216
2451
  }
2217
- const metadata = document.createElement("script");
2218
- metadata.type = "application/json";
2219
- metadata.setAttribute("data-remote-component", "");
2452
+ const metadataEl = document.createElement("script");
2453
+ metadataEl.type = "application/json";
2454
+ metadataEl.setAttribute("data-remote-component", "");
2220
2455
  const metadataObj = {
2221
2456
  name: this.name,
2222
2457
  bundle: this.bundle,
2223
- route: component?.getAttribute("data-route") ?? nextData?.page ?? url?.pathname ?? "/",
2224
- runtime: component?.getAttribute("data-runtime") ?? nextData?.props.__REMOTE_COMPONENT__?.runtime ?? "script"
2458
+ route: parsedMetadata.route,
2459
+ runtime: parsedMetadata.runtime
2225
2460
  };
2226
- metadata.textContent = JSON.stringify(metadataObj);
2461
+ metadataEl.textContent = JSON.stringify(metadataObj);
2227
2462
  if (this.previousElementSibling?.getAttribute("data-remote-component") !== null) {
2228
2463
  this.previousElementSibling?.remove();
2229
2464
  }
2230
- this.parentElement?.insertBefore(metadata, this);
2231
- const remoteSharedEl = doc.querySelector(
2232
- `#${this.name}_shared[data-remote-components-shared]`
2233
- );
2234
- const remoteShared = nextData?.props.__REMOTE_COMPONENT__?.shared ?? (JSON.parse(remoteSharedEl?.textContent ?? "{}") ?? {});
2235
- remoteSharedEl?.parentElement?.removeChild(remoteSharedEl);
2465
+ this.parentElement?.insertBefore(metadataEl, this);
2236
2466
  if ("__remote_components_missing_shared__" in remoteShared) {
2237
2467
  throw new RemoteComponentsError(
2238
2468
  remoteShared.__remote_components_missing_shared__
2239
2469
  );
2240
2470
  }
2241
- if (!component || !(rsc || nextData || isRemoteComponent)) {
2242
- throw new RemoteComponentsError(
2243
- `Remote Component not found on ${src}.${this.name !== "__vercel_remote_component" ? ` The name for the <RemoteComponent> is "${this.name}". Check <RemoteComponent> usage.` : ""} Did you forget to wrap the content in <RemoteComponent>?`
2244
- );
2245
- }
2246
- if (this.prevIsRemoteComponent) {
2247
- if (this.prevUrl) {
2248
- const prevUrl = this.prevUrl;
2471
+ if (this.hostState.prevIsRemoteComponent) {
2472
+ if (this.hostState.prevUrl) {
2473
+ const prevUrl = this.hostState.prevUrl;
2249
2474
  const self2 = globalThis;
2250
2475
  if (self2.__remote_script_entrypoint_unmount__?.[prevUrl.href]) {
2251
2476
  await Promise.all(
@@ -2270,26 +2495,21 @@ if (typeof HTMLElement !== "undefined") {
2270
2495
  }
2271
2496
  this.root.innerHTML = "";
2272
2497
  }
2273
- if (this.prevSrc !== null) {
2274
- const changeEvent = new Event("change", {
2275
- bubbles: true,
2276
- composed: true
2277
- });
2278
- Object.assign(changeEvent, {
2279
- previousSrc: this.prevSrc,
2498
+ if (this.hostState.prevSrc !== void 0) {
2499
+ this.dispatchLifecycleEvent("change", {
2500
+ previousSrc: this.hostState.prevSrc,
2280
2501
  nextSrc: src,
2281
- previousName: this.prevName,
2502
+ previousName: this.hostState.prevName,
2282
2503
  nextName: this.name
2283
2504
  });
2284
- this.dispatchEvent(changeEvent);
2285
2505
  }
2286
- this.prevUrl = url ?? new URL(location.href);
2287
- this.prevIsRemoteComponent = isRemoteComponent;
2288
- this.prevSrc = src;
2289
- this.prevName = this.name;
2506
+ this.hostState.prevUrl = url ?? new URL(window.location.href);
2507
+ this.hostState.prevIsRemoteComponent = isRemoteComponent;
2508
+ this.hostState.prevSrc = src;
2509
+ this.hostState.prevName = this.name;
2290
2510
  const removable = Array.from(this.childNodes);
2291
2511
  const links = doc.querySelectorAll("link[href]");
2292
- const remoteComponentSrc = this.getAttribute("src");
2512
+ const remoteComponentSrc = this.src ? String(this.src) : null;
2293
2513
  const doAttachStyles = () => attachStyles({
2294
2514
  doc,
2295
2515
  component,
@@ -2298,7 +2518,8 @@ if (typeof HTMLElement !== "undefined") {
2298
2518
  // Effects run after load, no abort needed
2299
2519
  baseUrl: url?.href,
2300
2520
  remoteComponentSrc,
2301
- root: this.root ?? null
2521
+ root: this.root ?? null,
2522
+ resolveClientUrl
2302
2523
  });
2303
2524
  if (!this.reactRoot) {
2304
2525
  await attachStyles({
@@ -2308,22 +2529,31 @@ if (typeof HTMLElement !== "undefined") {
2308
2529
  signal,
2309
2530
  baseUrl: url?.href,
2310
2531
  remoteComponentSrc,
2311
- root: this.root
2532
+ root: this.root,
2533
+ resolveClientUrl
2312
2534
  });
2313
2535
  if (!isCurrentLoad()) {
2314
2536
  return abandonLoad();
2315
2537
  }
2316
2538
  }
2317
- applyOriginToNodes(doc, url ?? new URL(location.href));
2539
+ applyOriginToNodes(
2540
+ doc,
2541
+ url ?? new URL(window.location.href),
2542
+ resolveClientUrl
2543
+ );
2318
2544
  if (!this.reactRoot) {
2319
2545
  Array.from(component.children).forEach((el) => {
2320
2546
  if (!isRemoteComponent && el.tagName.toLowerCase() === "script") {
2321
2547
  const newScript = document.createElement("script");
2322
2548
  for (const attr of el.attributes) {
2323
2549
  if (attr.name === "src") {
2550
+ const absoluteSrc = new URL(
2551
+ attr.value,
2552
+ url ?? window.location.origin
2553
+ ).href;
2324
2554
  newScript.setAttribute(
2325
2555
  attr.name,
2326
- new URL(attr.value, url ?? location.origin).href
2556
+ resolveClientUrl?.(absoluteSrc) ?? absoluteSrc
2327
2557
  );
2328
2558
  } else {
2329
2559
  newScript.setAttribute(attr.name, attr.value);
@@ -2353,7 +2583,7 @@ if (typeof HTMLElement !== "undefined") {
2353
2583
  }
2354
2584
  this.fallbackSlot?.remove();
2355
2585
  const applyReset = () => {
2356
- if (this.getAttribute("reset") !== null && !this.root?.querySelector("link[data-remote-components-reset]")) {
2586
+ if (this.reset && !this.root?.querySelector("link[data-remote-components-reset]")) {
2357
2587
  const allInitial = document.createElement("link");
2358
2588
  allInitial.setAttribute("data-remote-components-reset", "");
2359
2589
  const css = `:host { all: initial; }`;
@@ -2371,7 +2601,7 @@ if (typeof HTMLElement !== "undefined") {
2371
2601
  allInitial.removeAttribute("onload");
2372
2602
  };
2373
2603
  this.root?.prepend(allInitial);
2374
- } else if (this.getAttribute("reset") === null && this.root?.querySelector("link[data-remote-components-reset]")) {
2604
+ } else if (!this.reset && this.root?.querySelector("link[data-remote-components-reset]")) {
2375
2605
  this.root.querySelector("link[data-remote-components-reset]")?.remove();
2376
2606
  }
2377
2607
  };
@@ -2385,7 +2615,7 @@ if (typeof HTMLElement !== "undefined") {
2385
2615
  preloadScripts
2386
2616
  } = await getRuntime(
2387
2617
  metadataObj.runtime,
2388
- url ?? new URL(location.href),
2618
+ url ?? new URL(window.location.href),
2389
2619
  this.bundle,
2390
2620
  {
2391
2621
  react: async () => (await import("react")).default,
@@ -2394,7 +2624,8 @@ if (typeof HTMLElement !== "undefined") {
2394
2624
  "react-dom": async () => (await import("react-dom")).default,
2395
2625
  "react-dom/client": async () => (await import("react-dom/client")).default
2396
2626
  },
2397
- remoteShared
2627
+ remoteShared,
2628
+ resolveClientUrl
2398
2629
  );
2399
2630
  if (!isCurrentLoad()) {
2400
2631
  return abandonLoad();
@@ -2478,14 +2709,9 @@ if (typeof HTMLElement !== "undefined") {
2478
2709
  });
2479
2710
  }
2480
2711
  if (isCurrentLoad()) {
2481
- this.isLoading = false;
2712
+ this.hostState.stage = "loaded";
2482
2713
  }
2483
- const loadEvent = new Event("load", {
2484
- bubbles: true,
2485
- composed: true
2486
- });
2487
- Object.assign(loadEvent, { src: this.getAttribute("src") });
2488
- this.dispatchEvent(loadEvent);
2714
+ this.dispatchLifecycleEvent("load", { src: this.src });
2489
2715
  }, [initial, name]);
2490
2716
  return Component;
2491
2717
  };
@@ -2519,7 +2745,7 @@ if (typeof HTMLElement !== "undefined") {
2519
2745
  } else if (nextData) {
2520
2746
  const { Component, App } = nextClientPagesLoader2(
2521
2747
  this.bundle ?? "default",
2522
- nextData.page,
2748
+ nextData.page ?? "/",
2523
2749
  this.root
2524
2750
  );
2525
2751
  if (Component) {
@@ -2533,16 +2759,11 @@ if (typeof HTMLElement !== "undefined") {
2533
2759
  });
2534
2760
  }
2535
2761
  if (isCurrentLoad()) {
2536
- remoteComponent.isLoading = false;
2762
+ remoteComponent.hostState.stage = "loaded";
2537
2763
  }
2538
- const loadEvent = new Event("load", {
2539
- bubbles: true,
2540
- composed: true
2541
- });
2542
- Object.assign(loadEvent, {
2543
- src: remoteComponent.getAttribute("src")
2764
+ remoteComponent.dispatchLifecycleEvent("load", {
2765
+ src: remoteComponent.src
2544
2766
  });
2545
- remoteComponent.dispatchEvent(loadEvent);
2546
2767
  }, [initial, remoteComponent]);
2547
2768
  return NextApp ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(NextApp, { Component: NextComponent, ...nextData.props }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(NextComponent, { ...nextData.props });
2548
2769
  })(App, Component, this);
@@ -2552,7 +2773,7 @@ if (typeof HTMLElement !== "undefined") {
2552
2773
  root.render(/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(RemoteComponentFromNext, { initial: false }));
2553
2774
  doCleanup();
2554
2775
  if (isCurrentLoad()) {
2555
- this.isLoading = false;
2776
+ this.hostState.stage = "loaded";
2556
2777
  }
2557
2778
  });
2558
2779
  return;
@@ -2583,22 +2804,12 @@ if (typeof HTMLElement !== "undefined") {
2583
2804
  }
2584
2805
  })
2585
2806
  );
2586
- const loadEvent = new Event("load", {
2587
- bubbles: true,
2588
- composed: true
2589
- });
2590
- Object.assign(loadEvent, { src: this.getAttribute("src") });
2591
- this.dispatchEvent(loadEvent);
2807
+ this.dispatchLifecycleEvent("load", { src: this.src });
2592
2808
  } else {
2593
- const loadEvent = new Event("load", {
2594
- bubbles: true,
2595
- composed: true
2596
- });
2597
- Object.assign(loadEvent, { src: this.getAttribute("src") });
2598
- this.dispatchEvent(loadEvent);
2809
+ this.dispatchLifecycleEvent("load", { src: this.src });
2599
2810
  }
2600
2811
  if (isCurrentLoad()) {
2601
- this.isLoading = false;
2812
+ this.hostState.stage = "loaded";
2602
2813
  }
2603
2814
  }
2604
2815
  }
@@ -2617,6 +2828,7 @@ function registerSharedModules(modules = {}) {
2617
2828
  }
2618
2829
  // Annotate the CommonJS export names for ESM import in node:
2619
2830
  0 && (module.exports = {
2831
+ proxyClientRequestsThroughHost,
2620
2832
  registerSharedModules
2621
2833
  });
2622
2834
  //# sourceMappingURL=host.cjs.map