remote-components 0.0.50 → 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.
- package/dist/{component-loader-1838f572.d.ts → component-loader-76eb1b8b.d.ts} +3 -1
- package/dist/html/host.cjs +486 -242
- package/dist/html/host.cjs.map +1 -1
- package/dist/html/host.d.ts +2 -0
- package/dist/html/host.js +485 -242
- package/dist/html/host.js.map +1 -1
- package/dist/html/remote.cjs +123 -21
- package/dist/html/remote.cjs.map +1 -1
- package/dist/html/remote.js +123 -21
- package/dist/html/remote.js.map +1 -1
- package/dist/internal/next/host/app-router-client.cjs +12 -3
- package/dist/internal/next/host/app-router-client.cjs.map +1 -1
- package/dist/internal/next/host/app-router-client.d.ts +11 -2
- package/dist/internal/next/host/app-router-client.js +12 -3
- package/dist/internal/next/host/app-router-client.js.map +1 -1
- package/dist/internal/react/context.cjs +43 -0
- package/dist/internal/react/context.cjs.map +1 -0
- package/dist/internal/react/context.d.ts +20 -0
- package/dist/internal/react/context.js +18 -0
- package/dist/internal/react/context.js.map +1 -0
- package/dist/internal/react/hooks/use-resolve-client-url.cjs +39 -0
- package/dist/internal/react/hooks/use-resolve-client-url.cjs.map +1 -0
- package/dist/internal/react/hooks/use-resolve-client-url.d.ts +5 -0
- package/dist/internal/react/hooks/use-resolve-client-url.js +15 -0
- package/dist/internal/react/hooks/use-resolve-client-url.js.map +1 -0
- package/dist/internal/shared/client/apply-origin.cjs +10 -5
- package/dist/internal/shared/client/apply-origin.cjs.map +1 -1
- package/dist/internal/shared/client/apply-origin.d.ts +3 -1
- package/dist/internal/shared/client/apply-origin.js +10 -5
- package/dist/internal/shared/client/apply-origin.js.map +1 -1
- package/dist/internal/shared/client/default-resolve-client-url.cjs +32 -0
- package/dist/internal/shared/client/default-resolve-client-url.cjs.map +1 -0
- package/dist/internal/shared/client/default-resolve-client-url.d.ts +5 -0
- package/dist/internal/shared/client/default-resolve-client-url.js +10 -0
- package/dist/internal/shared/client/default-resolve-client-url.js.map +1 -0
- package/dist/internal/shared/client/protected-rc-fallback.cjs +41 -0
- package/dist/internal/shared/client/protected-rc-fallback.cjs.map +1 -0
- package/dist/internal/shared/client/protected-rc-fallback.d.ts +7 -0
- package/dist/internal/shared/client/protected-rc-fallback.js +16 -0
- package/dist/internal/shared/client/protected-rc-fallback.js.map +1 -0
- package/dist/internal/shared/client/proxy-through-host.cjs +51 -0
- package/dist/internal/shared/client/proxy-through-host.cjs.map +1 -0
- package/dist/internal/shared/client/proxy-through-host.d.ts +57 -0
- package/dist/internal/shared/client/proxy-through-host.js +26 -0
- package/dist/internal/shared/client/proxy-through-host.js.map +1 -0
- package/dist/internal/shared/client/remote-component.cjs +208 -134
- package/dist/internal/shared/client/remote-component.cjs.map +1 -1
- package/dist/internal/shared/client/remote-component.d.ts +7 -5
- package/dist/internal/shared/client/remote-component.js +207 -134
- package/dist/internal/shared/client/remote-component.js.map +1 -1
- package/dist/internal/shared/constants.cjs +32 -0
- package/dist/internal/shared/constants.cjs.map +1 -0
- package/dist/internal/shared/constants.d.ts +4 -0
- package/dist/internal/shared/constants.js +7 -0
- package/dist/internal/shared/constants.js.map +1 -0
- package/dist/internal/shared/error.cjs +70 -0
- package/dist/internal/shared/error.cjs.map +1 -1
- package/dist/internal/shared/error.d.ts +3 -1
- package/dist/internal/shared/error.js +71 -0
- package/dist/internal/shared/error.js.map +1 -1
- package/dist/internal/shared/ssr/fetch-remote-component.cjs +1 -2
- package/dist/internal/shared/ssr/fetch-remote-component.cjs.map +1 -1
- package/dist/internal/shared/ssr/fetch-remote-component.js +1 -2
- package/dist/internal/shared/ssr/fetch-remote-component.js.map +1 -1
- package/dist/internal/shared/ssr/fetch-with-hooks.cjs +7 -2
- package/dist/internal/shared/ssr/fetch-with-hooks.cjs.map +1 -1
- package/dist/internal/shared/ssr/fetch-with-hooks.js +7 -2
- package/dist/internal/shared/ssr/fetch-with-hooks.js.map +1 -1
- package/dist/internal/shared/utils/logger.cjs +26 -10
- package/dist/internal/shared/utils/logger.cjs.map +1 -1
- package/dist/internal/shared/utils/logger.d.ts +7 -2
- package/dist/internal/shared/utils/logger.js +24 -9
- package/dist/internal/shared/utils/logger.js.map +1 -1
- package/dist/next/config.cjs +2 -2
- package/dist/next/config.cjs.map +1 -1
- package/dist/next/config.js +2 -2
- package/dist/next/config.js.map +1 -1
- package/dist/next/host/client/index.cjs +363 -207
- package/dist/next/host/client/index.cjs.map +1 -1
- package/dist/next/host/client/index.d.ts +3 -1
- package/dist/next/host/client/index.js +336 -181
- package/dist/next/host/client/index.js.map +1 -1
- package/dist/next/host/pages-router-client.cjs +15 -2
- package/dist/next/host/pages-router-client.cjs.map +1 -1
- package/dist/next/host/pages-router-client.d.ts +12 -1
- package/dist/next/host/pages-router-client.js +15 -2
- package/dist/next/host/pages-router-client.js.map +1 -1
- package/dist/next/host/pages-router-server.cjs +3 -2
- package/dist/next/host/pages-router-server.cjs.map +1 -1
- package/dist/next/host/pages-router-server.d.ts +16 -0
- package/dist/next/host/pages-router-server.js +3 -2
- package/dist/next/host/pages-router-server.js.map +1 -1
- package/dist/next/proxy.cjs +66 -27
- package/dist/next/proxy.cjs.map +1 -1
- package/dist/next/proxy.d.ts +0 -1
- package/dist/next/proxy.js +66 -27
- package/dist/next/proxy.js.map +1 -1
- package/dist/proxy-through-host-a676a522.d.ts +52 -0
- package/dist/react/index.cjs +381 -206
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.ts +28 -4
- package/dist/react/index.js +354 -181
- package/dist/react/index.js.map +1 -1
- package/dist/shared/host/proxy.cjs +52 -23
- package/dist/shared/host/proxy.cjs.map +1 -1
- package/dist/shared/host/proxy.d.ts +0 -3
- package/dist/shared/host/proxy.js +55 -22
- package/dist/shared/host/proxy.js.map +1 -1
- package/dist/shared/remote/proxy.cjs +9 -1
- package/dist/shared/remote/proxy.cjs.map +1 -1
- package/dist/shared/remote/proxy.d.ts +7 -1
- package/dist/shared/remote/proxy.js +8 -1
- package/dist/shared/remote/proxy.js.map +1 -1
- package/package.json +1 -1
- package/dist/internal/shared/ssr/fetch-with-protected-rc-fallback.cjs +0 -62
- package/dist/internal/shared/ssr/fetch-with-protected-rc-fallback.cjs.map +0 -1
- package/dist/internal/shared/ssr/fetch-with-protected-rc-fallback.d.ts +0 -14
- package/dist/internal/shared/ssr/fetch-with-protected-rc-fallback.js +0 -37
- package/dist/internal/shared/ssr/fetch-with-protected-rc-fallback.js.map +0 -1
package/dist/react/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
useEffect,
|
|
5
5
|
useId,
|
|
6
6
|
useLayoutEffect as useLayoutEffect2,
|
|
7
|
-
useMemo,
|
|
7
|
+
useMemo as useMemo2,
|
|
8
8
|
useRef as useRef2,
|
|
9
9
|
useState as useState2
|
|
10
10
|
} from "react";
|
|
@@ -22,7 +22,7 @@ var tagNames = [
|
|
|
22
22
|
"script",
|
|
23
23
|
"link"
|
|
24
24
|
];
|
|
25
|
-
function applyOriginToNodes(doc, url) {
|
|
25
|
+
function applyOriginToNodes(doc, url, resolveClientUrl) {
|
|
26
26
|
if (url.origin !== location.origin) {
|
|
27
27
|
const nodes = doc.querySelectorAll(
|
|
28
28
|
tagNames.map(
|
|
@@ -31,12 +31,15 @@ function applyOriginToNodes(doc, url) {
|
|
|
31
31
|
);
|
|
32
32
|
nodes.forEach((node) => {
|
|
33
33
|
if (node.hasAttribute("src") && /^[./]+\/?/.test(node.getAttribute("src") ?? "")) {
|
|
34
|
-
|
|
34
|
+
const absoluteSrc = new URL(node.getAttribute("src") ?? "/", url).href;
|
|
35
|
+
const isScript = node.tagName.toLowerCase() === "script";
|
|
36
|
+
node.src = isScript ? absoluteSrc : resolveClientUrl?.(absoluteSrc) ?? absoluteSrc;
|
|
35
37
|
}
|
|
36
38
|
if (node.hasAttribute("href") && /^[./]+\/?/.test(node.getAttribute("href") ?? "")) {
|
|
39
|
+
const absoluteHref = new URL(node.getAttribute("href") ?? "/", url).href;
|
|
37
40
|
node.setAttribute(
|
|
38
41
|
"href",
|
|
39
|
-
|
|
42
|
+
resolveClientUrl?.(absoluteHref) ?? absoluteHref
|
|
40
43
|
);
|
|
41
44
|
}
|
|
42
45
|
if (node.hasAttribute("srcset")) {
|
|
@@ -45,7 +48,8 @@ function applyOriginToNodes(doc, url) {
|
|
|
45
48
|
if (!urlPart)
|
|
46
49
|
return entry;
|
|
47
50
|
const absoluteUrl = new URL(urlPart, url).href;
|
|
48
|
-
|
|
51
|
+
const resolvedUrl = resolveClientUrl?.(absoluteUrl) ?? absoluteUrl;
|
|
52
|
+
return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl;
|
|
49
53
|
}).join(", ");
|
|
50
54
|
if (srcSet) {
|
|
51
55
|
node.setAttribute("srcset", srcSet);
|
|
@@ -57,7 +61,8 @@ function applyOriginToNodes(doc, url) {
|
|
|
57
61
|
if (!urlPart)
|
|
58
62
|
return entry;
|
|
59
63
|
const absoluteUrl = new URL(urlPart, url).href;
|
|
60
|
-
|
|
64
|
+
const resolvedUrl = resolveClientUrl?.(absoluteUrl) ?? absoluteUrl;
|
|
65
|
+
return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl;
|
|
61
66
|
}).join(", ");
|
|
62
67
|
if (srcSet) {
|
|
63
68
|
node.setAttribute("imagesrcset", srcSet);
|
|
@@ -67,6 +72,34 @@ function applyOriginToNodes(doc, url) {
|
|
|
67
72
|
}
|
|
68
73
|
}
|
|
69
74
|
|
|
75
|
+
// src/shared/constants.ts
|
|
76
|
+
var RC_PROTECTED_REMOTE_FETCH_PATHNAME = "/rc-fetch-protected-remote";
|
|
77
|
+
var CORS_DOCS_URL = "https://vercel.com/docs/remote-components/concepts/cors-external-urls#accessing-cross-site-protected-remote-components";
|
|
78
|
+
|
|
79
|
+
// src/shared/client/protected-rc-fallback.ts
|
|
80
|
+
function generateProtectedRcFallbackSrc(url) {
|
|
81
|
+
return `${RC_PROTECTED_REMOTE_FETCH_PATHNAME}?url=${encodeURIComponent(url)}`;
|
|
82
|
+
}
|
|
83
|
+
function isProxiedUrl(url) {
|
|
84
|
+
try {
|
|
85
|
+
return new URL(url, location.href).pathname === RC_PROTECTED_REMOTE_FETCH_PATHNAME;
|
|
86
|
+
} catch {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/shared/utils/abort.ts
|
|
92
|
+
function isAbortError(error) {
|
|
93
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
if (error !== null && typeof error === "object" && "name" in error && error.name === "AbortError" && "message" in error && typeof error.message === "string") {
|
|
97
|
+
const e = error;
|
|
98
|
+
return typeof e.code === "number" || e.constructor?.name === "DOMException";
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
70
103
|
// src/shared/error.ts
|
|
71
104
|
var RemoteComponentsError = class extends Error {
|
|
72
105
|
code = "REMOTE_COMPONENTS_ERROR";
|
|
@@ -86,6 +119,69 @@ function failedToFetchRemoteComponentError(url, { status, statusText }, help = "
|
|
|
86
119
|
{ cause: new Error(`${status} ${statusText}`) }
|
|
87
120
|
);
|
|
88
121
|
}
|
|
122
|
+
async function errorFromFailedFetch(originalUrl, resolvedUrl, res) {
|
|
123
|
+
const isProxied = isProxiedUrl(resolvedUrl.href);
|
|
124
|
+
if (isProxied && res) {
|
|
125
|
+
const body = await res.text().catch(() => "");
|
|
126
|
+
return failedProxyFetchError(
|
|
127
|
+
originalUrl,
|
|
128
|
+
resolvedUrl.href,
|
|
129
|
+
res.status,
|
|
130
|
+
body
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
const fallback = failedToFetchRemoteComponentError(
|
|
134
|
+
originalUrl,
|
|
135
|
+
res ?? { status: 0, statusText: "No Response" }
|
|
136
|
+
);
|
|
137
|
+
if (!res)
|
|
138
|
+
return fallback;
|
|
139
|
+
try {
|
|
140
|
+
const body = await res.text();
|
|
141
|
+
const parser = new DOMParser();
|
|
142
|
+
const doc = parser.parseFromString(body, "text/html");
|
|
143
|
+
const errorTemplate = doc.querySelector(
|
|
144
|
+
"template[data-next-error-message]"
|
|
145
|
+
);
|
|
146
|
+
const errorMessage = errorTemplate?.getAttribute("data-next-error-message");
|
|
147
|
+
if (errorMessage) {
|
|
148
|
+
const error = new RemoteComponentsError(errorMessage);
|
|
149
|
+
const errorStack = errorTemplate?.getAttribute("data-next-error-stack");
|
|
150
|
+
if (errorStack) {
|
|
151
|
+
error.stack = errorStack;
|
|
152
|
+
}
|
|
153
|
+
return error;
|
|
154
|
+
}
|
|
155
|
+
} catch (parseError) {
|
|
156
|
+
if (isAbortError(parseError))
|
|
157
|
+
throw parseError;
|
|
158
|
+
}
|
|
159
|
+
return fallback;
|
|
160
|
+
}
|
|
161
|
+
function failedProxyFetchError(originalUrl, proxyUrl, status, responseBody) {
|
|
162
|
+
if (status === 404) {
|
|
163
|
+
return new RemoteComponentsError(
|
|
164
|
+
`Could not proxy the request to "${originalUrl}" \u2014 the proxy endpoint "${RC_PROTECTED_REMOTE_FETCH_PATHNAME}" returned 404.
|
|
165
|
+
|
|
166
|
+
The host server needs middleware or a route that handles "${RC_PROTECTED_REMOTE_FETCH_PATHNAME}".
|
|
167
|
+
|
|
168
|
+
Proxying requires two pieces:
|
|
169
|
+
1. resolveClientUrl={proxyClientRequestsThroughHost} on <RemoteComponent>
|
|
170
|
+
2. Middleware or a route for "${RC_PROTECTED_REMOTE_FETCH_PATHNAME}" on the host server
|
|
171
|
+
|
|
172
|
+
Docs: ${CORS_DOCS_URL}`
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
if (status === 403) {
|
|
176
|
+
const detail = responseBody ? ` ${responseBody}` : "";
|
|
177
|
+
return new RemoteComponentsError(
|
|
178
|
+
`Proxied request to "${proxyUrl}" was forbidden.${detail} See: ${CORS_DOCS_URL}`
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
return new RemoteComponentsError(
|
|
182
|
+
`Proxied request for "${originalUrl}" via "${proxyUrl}" failed with status ${status}. See: ${CORS_DOCS_URL}`
|
|
183
|
+
);
|
|
184
|
+
}
|
|
89
185
|
|
|
90
186
|
// src/shared/utils/logger.ts
|
|
91
187
|
var PREFIX = "remote-components";
|
|
@@ -95,9 +191,6 @@ function logDebug(location2, message) {
|
|
|
95
191
|
console.debug(`[${PREFIX}:${location2}]: ${message}`);
|
|
96
192
|
}
|
|
97
193
|
}
|
|
98
|
-
function logInfo(location2, message) {
|
|
99
|
-
console.info(`[${PREFIX}:${location2}]: ${message}`);
|
|
100
|
-
}
|
|
101
194
|
function logWarn(location2, message) {
|
|
102
195
|
console.warn(`[${PREFIX}:${location2}]: ${message}`);
|
|
103
196
|
}
|
|
@@ -108,6 +201,19 @@ function logError(location2, message, cause) {
|
|
|
108
201
|
})
|
|
109
202
|
);
|
|
110
203
|
}
|
|
204
|
+
function warnCrossOriginFetchError(logLocation, url) {
|
|
205
|
+
try {
|
|
206
|
+
const parsed = typeof url === "string" ? new URL(url) : url;
|
|
207
|
+
if (typeof location === "undefined" || parsed.origin === location.origin) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
logWarn(
|
|
211
|
+
logLocation,
|
|
212
|
+
`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}`
|
|
213
|
+
);
|
|
214
|
+
} catch {
|
|
215
|
+
}
|
|
216
|
+
}
|
|
111
217
|
|
|
112
218
|
// src/shared/client/polyfill.tsx
|
|
113
219
|
import { jsx } from "react/jsx-runtime";
|
|
@@ -551,28 +657,40 @@ function createRSCStream(rscName, data) {
|
|
|
551
657
|
});
|
|
552
658
|
}
|
|
553
659
|
|
|
660
|
+
// src/shared/client/webpack-patterns.ts
|
|
661
|
+
var NEXT_BUNDLE_PATH_RE = /\/_next\/\[.+\](?:%20| )/;
|
|
662
|
+
|
|
554
663
|
// src/shared/client/script-loader.ts
|
|
555
|
-
async function loadScripts(scripts) {
|
|
664
|
+
async function loadScripts(scripts, resolveClientUrl) {
|
|
556
665
|
await Promise.all(
|
|
557
666
|
scripts.map((script) => {
|
|
558
667
|
return new Promise((resolve, reject) => {
|
|
559
668
|
const newSrc = new URL(
|
|
560
669
|
// remove the remote component bundle name identifier from the script src
|
|
561
|
-
script.src.replace(
|
|
670
|
+
script.src.replace(NEXT_BUNDLE_PATH_RE, "/_next/"),
|
|
562
671
|
location.origin
|
|
563
672
|
).href;
|
|
673
|
+
const resolvedSrc = resolveClientUrl?.(newSrc) ?? newSrc;
|
|
564
674
|
const newScript = document.createElement("script");
|
|
565
|
-
newScript.onload = () =>
|
|
566
|
-
resolve();
|
|
567
|
-
};
|
|
675
|
+
newScript.onload = () => resolve();
|
|
568
676
|
newScript.onerror = () => {
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
677
|
+
const isProxied = isProxiedUrl(resolvedSrc);
|
|
678
|
+
if (isProxied) {
|
|
679
|
+
reject(
|
|
680
|
+
new RemoteComponentsError(
|
|
681
|
+
`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}`
|
|
682
|
+
)
|
|
683
|
+
);
|
|
684
|
+
} else {
|
|
685
|
+
warnCrossOriginFetchError("ScriptLoader", newSrc);
|
|
686
|
+
reject(
|
|
687
|
+
new RemoteComponentsError(
|
|
688
|
+
`Failed to load <script src="${newSrc}"> for Remote Component. Check the URL is correct.`
|
|
689
|
+
)
|
|
690
|
+
);
|
|
691
|
+
}
|
|
574
692
|
};
|
|
575
|
-
newScript.src =
|
|
693
|
+
newScript.src = resolvedSrc;
|
|
576
694
|
newScript.async = true;
|
|
577
695
|
document.head.appendChild(newScript);
|
|
578
696
|
});
|
|
@@ -580,50 +698,6 @@ async function loadScripts(scripts) {
|
|
|
580
698
|
);
|
|
581
699
|
}
|
|
582
700
|
|
|
583
|
-
// src/shared/utils/abort.ts
|
|
584
|
-
function isAbortError(error) {
|
|
585
|
-
if (error instanceof DOMException && error.name === "AbortError") {
|
|
586
|
-
return true;
|
|
587
|
-
}
|
|
588
|
-
if (error !== null && typeof error === "object" && "name" in error && error.name === "AbortError" && "message" in error && typeof error.message === "string") {
|
|
589
|
-
const e = error;
|
|
590
|
-
return typeof e.code === "number" || e.constructor?.name === "DOMException";
|
|
591
|
-
}
|
|
592
|
-
return false;
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
// src/shared/ssr/fetch-with-protected-rc-fallback.ts
|
|
596
|
-
var RC_PROTECTED_REMOTE_FETCH_PATHNAME = "/rc-fetch-protected-remote";
|
|
597
|
-
async function fetchWithProtectedRcFallback(url, init) {
|
|
598
|
-
try {
|
|
599
|
-
const res = await fetch(url, init);
|
|
600
|
-
return res;
|
|
601
|
-
} catch (error) {
|
|
602
|
-
if (isAbortError(error)) {
|
|
603
|
-
throw error;
|
|
604
|
-
}
|
|
605
|
-
if (typeof document === "object" && typeof document.location === "object" && document.location.origin !== new URL(url).origin) {
|
|
606
|
-
logInfo(
|
|
607
|
-
"FetchRemoteComponent",
|
|
608
|
-
"Request failed due to CORS, attempting to fetch it via the withRemoteComponentsHost proxy."
|
|
609
|
-
);
|
|
610
|
-
const proxiedRes = await fetch(
|
|
611
|
-
`${RC_PROTECTED_REMOTE_FETCH_PATHNAME}?url=${url}`,
|
|
612
|
-
init?.signal ? { signal: init.signal } : void 0
|
|
613
|
-
);
|
|
614
|
-
if (proxiedRes.status === 200) {
|
|
615
|
-
return proxiedRes;
|
|
616
|
-
} else {
|
|
617
|
-
logError(
|
|
618
|
-
"FetchRemoteComponent",
|
|
619
|
-
`Could not proxy remote: [response status ${proxiedRes.status}] ${await proxiedRes.text()}`
|
|
620
|
-
);
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
throw error;
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
|
|
627
701
|
// src/shared/utils/index.ts
|
|
628
702
|
function escapeString(str) {
|
|
629
703
|
return str.replace(/[^a-z0-9]/g, "_");
|
|
@@ -652,11 +726,10 @@ var REMOTE_SHARED_ASSIGNMENT_RE = /\.TURBOPACK_REMOTE_SHARED=await (?:__turbopac
|
|
|
652
726
|
var ASYNC_MODULE_LOADER_RE = /(?:__turbopack_context__|e)\.A\((?<asyncSharedModuleId>[0-9]+)\)/;
|
|
653
727
|
var ASYNC_MODULE_RESOLVE_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<inner>parentImport|e)=>Promise\.resolve\(\)\.then\(\(\)=>\k<inner>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
|
|
654
728
|
var ASYNC_MODULE_ALL_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<vCb>parentImport|t)=>Promise\.all\(\["[^"]+"\]\.map\((?<mapCb>chunk|t)=>\k<ctx>\.l\(\k<mapCb>\)\)\)\.then\(\(\)=>\k<vCb>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
|
|
655
|
-
var NUMERIC_MODULE_ID_RE = /^[0-9]+$/;
|
|
656
729
|
var TURBOPACK_GLOBAL_RE = /(?:globalThis|self)\s*(?:\.TURBOPACK|\[\s*["']TURBOPACK["']\s*\])/;
|
|
657
730
|
|
|
658
731
|
// src/shared/client/chunk-loader.ts
|
|
659
|
-
function createChunkLoader(runtime) {
|
|
732
|
+
function createChunkLoader(runtime, resolveClientUrl) {
|
|
660
733
|
return function __turbopack_chunk_load__(chunkId, scriptBundle) {
|
|
661
734
|
logDebug("ChunkLoader", `Loading chunk: "${chunkId}"`);
|
|
662
735
|
const self = globalThis;
|
|
@@ -696,9 +769,10 @@ function createChunkLoader(runtime) {
|
|
|
696
769
|
logDebug("ChunkLoader", `Returning cached promise for: "${url}"`);
|
|
697
770
|
return self.__remote_components_turbopack_chunk_loader_promise__[url];
|
|
698
771
|
}
|
|
699
|
-
|
|
772
|
+
const resolvedUrl = resolveClientUrl?.(url) ?? url;
|
|
773
|
+
logDebug("ChunkLoader", `Fetching chunk from: "${resolvedUrl}"`);
|
|
700
774
|
self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise((resolve, reject) => {
|
|
701
|
-
|
|
775
|
+
fetch(resolvedUrl).then((res) => res.text()).then((code) => {
|
|
702
776
|
const hasTurbopack = TURBOPACK_GLOBAL_RE.test(code);
|
|
703
777
|
if (hasTurbopack) {
|
|
704
778
|
return handleTurbopackChunk(code, bundle ?? "", url);
|
|
@@ -711,7 +785,19 @@ function createChunkLoader(runtime) {
|
|
|
711
785
|
"ChunkLoader",
|
|
712
786
|
`First 500 chars of chunk: ${code.slice(0, 500)}`
|
|
713
787
|
);
|
|
714
|
-
}).then(resolve).catch(
|
|
788
|
+
}).then(resolve).catch((error) => {
|
|
789
|
+
const isProxied = isProxiedUrl(resolvedUrl);
|
|
790
|
+
if (isProxied) {
|
|
791
|
+
reject(
|
|
792
|
+
new RemoteComponentsError(
|
|
793
|
+
`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}`
|
|
794
|
+
)
|
|
795
|
+
);
|
|
796
|
+
} else {
|
|
797
|
+
warnCrossOriginFetchError("ChunkLoader", url);
|
|
798
|
+
reject(error);
|
|
799
|
+
}
|
|
800
|
+
});
|
|
715
801
|
});
|
|
716
802
|
return self.__remote_components_turbopack_chunk_loader_promise__[url];
|
|
717
803
|
};
|
|
@@ -858,10 +944,14 @@ async function handleTurbopackChunk(code, bundle, url) {
|
|
|
858
944
|
function handleTurbopackModule(bundle, moduleId, id) {
|
|
859
945
|
const self = globalThis;
|
|
860
946
|
const bundleKey = getBundleKey(bundle);
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
modules =
|
|
947
|
+
const raw = self[`TURBOPACK_${bundleKey}`];
|
|
948
|
+
let modules;
|
|
949
|
+
if (raw && typeof raw === "object" && "__chunks__" in raw) {
|
|
950
|
+
modules = raw.__chunks__.flat();
|
|
951
|
+
} else if (Array.isArray(raw)) {
|
|
952
|
+
modules = raw.flat();
|
|
953
|
+
} else {
|
|
954
|
+
modules = raw;
|
|
865
955
|
}
|
|
866
956
|
if (!self.__remote_components_turbopack_modules__) {
|
|
867
957
|
self.__remote_components_turbopack_modules__ = {};
|
|
@@ -890,13 +980,12 @@ function handleTurbopackModule(bundle, moduleId, id) {
|
|
|
890
980
|
if (!self.__remote_components_turbopack_global__[bundle]) {
|
|
891
981
|
self.__remote_components_turbopack_global__[bundle] = {};
|
|
892
982
|
}
|
|
893
|
-
const allModules = Array.isArray(modules) ? modules.flat() : modules ? Object.values(modules) : [];
|
|
894
983
|
moduleInit(
|
|
895
984
|
createTurbopackContext(
|
|
896
985
|
bundle,
|
|
897
986
|
exports,
|
|
898
987
|
moduleExports,
|
|
899
|
-
|
|
988
|
+
modules,
|
|
900
989
|
moduleInit,
|
|
901
990
|
id,
|
|
902
991
|
self
|
|
@@ -910,46 +999,27 @@ function handleTurbopackModule(bundle, moduleId, id) {
|
|
|
910
999
|
return moduleExports.exports;
|
|
911
1000
|
}
|
|
912
1001
|
function findModuleInit(modules, moduleId) {
|
|
913
|
-
if (
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
1002
|
+
if (!modules || typeof modules !== "object")
|
|
1003
|
+
return;
|
|
1004
|
+
if (!Array.isArray(modules)) {
|
|
1005
|
+
const key = moduleId in modules ? moduleId : Object.keys(modules).find((k) => k.startsWith(moduleId));
|
|
1006
|
+
return key !== void 0 ? modules[key] : void 0;
|
|
1007
|
+
}
|
|
1008
|
+
const flat = modules.flat();
|
|
1009
|
+
let idx = flat.findIndex((e) => String(e) === String(moduleId));
|
|
1010
|
+
if (idx < 0) {
|
|
1011
|
+
idx = flat.findIndex(
|
|
1012
|
+
(e) => typeof e === "string" && e.startsWith(moduleId)
|
|
923
1013
|
);
|
|
924
|
-
if (matchingKey) {
|
|
925
|
-
return modules[matchingKey];
|
|
926
|
-
}
|
|
927
|
-
logError("TurbopackModule", `No match found for module ID: ${moduleId}`);
|
|
928
|
-
return void 0;
|
|
929
1014
|
}
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
const normalizedId = NUMERIC_MODULE_ID_RE.test(moduleId) ? Number(moduleId) : moduleId;
|
|
933
|
-
let moduleIdIndex = allModules.indexOf(normalizedId);
|
|
934
|
-
if (moduleIdIndex === -1) {
|
|
935
|
-
moduleIdIndex = allModules.findIndex(
|
|
936
|
-
(bundleEntry) => typeof bundleEntry === "string" && bundleEntry.startsWith(moduleId) || bundleEntry === normalizedId
|
|
937
|
-
);
|
|
938
|
-
}
|
|
939
|
-
if (moduleIdIndex !== -1) {
|
|
940
|
-
while (typeof allModules[moduleIdIndex] !== "function" && moduleIdIndex < allModules.length) {
|
|
941
|
-
moduleIdIndex++;
|
|
942
|
-
}
|
|
943
|
-
return allModules[moduleIdIndex];
|
|
944
|
-
}
|
|
945
|
-
} else {
|
|
946
|
-
return allModules.find(
|
|
947
|
-
(bundleEntry) => typeof bundleEntry === "object" && bundleEntry !== null && moduleId in bundleEntry
|
|
948
|
-
)?.[moduleId];
|
|
1015
|
+
if (idx >= 0) {
|
|
1016
|
+
return flat.slice(idx + 1).find((e) => typeof e === "function");
|
|
949
1017
|
}
|
|
950
|
-
return
|
|
1018
|
+
return flat.find(
|
|
1019
|
+
(e) => Boolean(e && typeof e === "object" && moduleId in e)
|
|
1020
|
+
)?.[moduleId];
|
|
951
1021
|
}
|
|
952
|
-
function createTurbopackContext(bundle, exports, moduleExports,
|
|
1022
|
+
function createTurbopackContext(bundle, exports, moduleExports, modules, moduleInit, id, self) {
|
|
953
1023
|
return {
|
|
954
1024
|
// HMR not implemented for Remote Components
|
|
955
1025
|
k: {
|
|
@@ -1070,13 +1140,17 @@ function createTurbopackContext(bundle, exports, moduleExports, allModules, modu
|
|
|
1070
1140
|
(parentId) => self.__webpack_require__?.(`[${bundle}] ${parentId}`)
|
|
1071
1141
|
);
|
|
1072
1142
|
},
|
|
1143
|
+
// dynamic import tracking — no-op for remote components
|
|
1144
|
+
j() {
|
|
1145
|
+
},
|
|
1073
1146
|
// chunk loader
|
|
1074
1147
|
l(url) {
|
|
1075
|
-
const
|
|
1148
|
+
const flatModules = Array.isArray(modules) ? modules : [];
|
|
1149
|
+
const moduleInitIndex = flatModules.indexOf(moduleInit);
|
|
1076
1150
|
if (moduleInitIndex !== -1) {
|
|
1077
|
-
const scriptIndex =
|
|
1151
|
+
const scriptIndex = flatModules.slice(0, moduleInitIndex).findLastIndex((bundleEntry) => bundleEntry instanceof Element);
|
|
1078
1152
|
if (scriptIndex !== -1) {
|
|
1079
|
-
const script =
|
|
1153
|
+
const script = flatModules[scriptIndex];
|
|
1080
1154
|
const scriptSrc = script.getAttribute("data-turbopack-src") || "";
|
|
1081
1155
|
const nextIndex = scriptSrc.indexOf("/_next");
|
|
1082
1156
|
const baseUrl = nextIndex !== -1 ? scriptSrc.slice(0, nextIndex) : "";
|
|
@@ -1216,7 +1290,7 @@ function getSharedModule(bundle, id) {
|
|
|
1216
1290
|
}
|
|
1217
1291
|
|
|
1218
1292
|
// src/shared/client/webpack-adapter.ts
|
|
1219
|
-
async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location.href), bundle, shared = {}, remoteShared = {}) {
|
|
1293
|
+
async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location.href), bundle, shared = {}, remoteShared = {}, resolveClientUrl) {
|
|
1220
1294
|
const self = globalThis;
|
|
1221
1295
|
if (!self.__remote_bundle_url__) {
|
|
1222
1296
|
self.__remote_bundle_url__ = {};
|
|
@@ -1228,7 +1302,7 @@ async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location
|
|
|
1228
1302
|
self.__original_webpack_chunk_load__ = self.__webpack_chunk_load__;
|
|
1229
1303
|
self.__original_webpack_require__ = self.__webpack_require__;
|
|
1230
1304
|
}
|
|
1231
|
-
self.__webpack_chunk_load__ = createChunkLoader(runtime);
|
|
1305
|
+
self.__webpack_chunk_load__ = createChunkLoader(runtime, resolveClientUrl);
|
|
1232
1306
|
self.__webpack_require__ = createModuleRequire(runtime);
|
|
1233
1307
|
self.__webpack_require_type__ = runtime;
|
|
1234
1308
|
if (self.__remote_webpack_require__ && runtime === RUNTIME_TURBOPACK) {
|
|
@@ -1323,7 +1397,8 @@ async function loadRemoteComponent({
|
|
|
1323
1397
|
scripts = [],
|
|
1324
1398
|
shared = Promise.resolve({}),
|
|
1325
1399
|
remoteShared = {},
|
|
1326
|
-
container
|
|
1400
|
+
container,
|
|
1401
|
+
resolveClientUrl
|
|
1327
1402
|
}) {
|
|
1328
1403
|
try {
|
|
1329
1404
|
if (runtime === "webpack") {
|
|
@@ -1332,7 +1407,7 @@ async function loadRemoteComponent({
|
|
|
1332
1407
|
self.__DISABLE_WEBPACK_EXEC__ = {};
|
|
1333
1408
|
}
|
|
1334
1409
|
self.__DISABLE_WEBPACK_EXEC__[bundle] = true;
|
|
1335
|
-
await loadScripts(scripts);
|
|
1410
|
+
await loadScripts(scripts, resolveClientUrl);
|
|
1336
1411
|
}
|
|
1337
1412
|
const hostShared = await shared;
|
|
1338
1413
|
logDebug(
|
|
@@ -1353,7 +1428,8 @@ async function loadRemoteComponent({
|
|
|
1353
1428
|
url,
|
|
1354
1429
|
bundle,
|
|
1355
1430
|
hostShared,
|
|
1356
|
-
remoteShared
|
|
1431
|
+
remoteShared,
|
|
1432
|
+
resolveClientUrl
|
|
1357
1433
|
);
|
|
1358
1434
|
if (bundle) {
|
|
1359
1435
|
const resolve = {
|
|
@@ -1442,6 +1518,28 @@ function loadNextPagesComponent(bundle, route, nextData, name, container) {
|
|
|
1442
1518
|
return { component };
|
|
1443
1519
|
}
|
|
1444
1520
|
|
|
1521
|
+
// src/shared/client/proxy-through-host.ts
|
|
1522
|
+
function withRemoteSrc(resolveClientUrl, remoteSrc) {
|
|
1523
|
+
return (url) => resolveClientUrl(remoteSrc, url);
|
|
1524
|
+
}
|
|
1525
|
+
var proxyClientRequestsThroughHost = (remoteSrc, url) => {
|
|
1526
|
+
if (typeof location === "undefined") {
|
|
1527
|
+
return void 0;
|
|
1528
|
+
}
|
|
1529
|
+
const remoteOrigin = new URL(remoteSrc, location.href).origin;
|
|
1530
|
+
if (remoteOrigin === location.origin) {
|
|
1531
|
+
return void 0;
|
|
1532
|
+
}
|
|
1533
|
+
try {
|
|
1534
|
+
const parsed = new URL(url, location.href);
|
|
1535
|
+
if (parsed.origin === remoteOrigin) {
|
|
1536
|
+
return generateProtectedRcFallbackSrc(url);
|
|
1537
|
+
}
|
|
1538
|
+
} catch {
|
|
1539
|
+
}
|
|
1540
|
+
return void 0;
|
|
1541
|
+
};
|
|
1542
|
+
|
|
1445
1543
|
// src/shared/client/set-attributes-from-props.ts
|
|
1446
1544
|
var DOMAttributeNames = {
|
|
1447
1545
|
acceptCharset: "accept-charset",
|
|
@@ -1485,7 +1583,81 @@ function setAttributesFromProps(el, props) {
|
|
|
1485
1583
|
}
|
|
1486
1584
|
|
|
1487
1585
|
// src/shared/client/static-loader.ts
|
|
1488
|
-
async function
|
|
1586
|
+
async function importViaCallback(absoluteSrc, resolveClientUrl) {
|
|
1587
|
+
const resolvedUrl = resolveClientUrl(absoluteSrc) ?? absoluteSrc;
|
|
1588
|
+
const fetchUrl = new URL(resolvedUrl, location.href).href;
|
|
1589
|
+
const response = await fetch(fetchUrl);
|
|
1590
|
+
if (!response.ok)
|
|
1591
|
+
throw new Error(`Proxied fetch failed: ${response.status}`);
|
|
1592
|
+
const content = (await response.text()).replace(/import\.meta\.url/g, JSON.stringify(absoluteSrc)).replace(
|
|
1593
|
+
/\b(from|import)\s*(["'])(\.\.?\/[^"']+)\2/g,
|
|
1594
|
+
(_, keyword, quote, relativePath) => {
|
|
1595
|
+
const absoluteImportUrl = new URL(relativePath, absoluteSrc).href;
|
|
1596
|
+
const resolvedImportUrl = new URL(
|
|
1597
|
+
resolveClientUrl(absoluteImportUrl) ?? absoluteImportUrl,
|
|
1598
|
+
location.href
|
|
1599
|
+
).href;
|
|
1600
|
+
return `${keyword} ${quote}${resolvedImportUrl}${quote}`;
|
|
1601
|
+
}
|
|
1602
|
+
);
|
|
1603
|
+
const moduleBlobUrl = URL.createObjectURL(
|
|
1604
|
+
new Blob([content], { type: "text/javascript" })
|
|
1605
|
+
);
|
|
1606
|
+
const wrapperContent = [
|
|
1607
|
+
`import*as m from${JSON.stringify(moduleBlobUrl)};`,
|
|
1608
|
+
`globalThis.__rc_module_registry__=globalThis.__rc_module_registry__||{};`,
|
|
1609
|
+
`globalThis.__rc_module_registry__[${JSON.stringify(absoluteSrc)}]=m;`
|
|
1610
|
+
].join("");
|
|
1611
|
+
const wrapperBlobUrl = URL.createObjectURL(
|
|
1612
|
+
new Blob([wrapperContent], { type: "text/javascript" })
|
|
1613
|
+
);
|
|
1614
|
+
const scriptEl = document.createElement("script");
|
|
1615
|
+
scriptEl.type = "module";
|
|
1616
|
+
scriptEl.src = wrapperBlobUrl;
|
|
1617
|
+
try {
|
|
1618
|
+
await new Promise((resolve, reject) => {
|
|
1619
|
+
scriptEl.onload = () => resolve();
|
|
1620
|
+
scriptEl.onerror = () => reject(new Error(`Failed to load module for ${absoluteSrc}`));
|
|
1621
|
+
document.head.appendChild(scriptEl);
|
|
1622
|
+
});
|
|
1623
|
+
} finally {
|
|
1624
|
+
scriptEl.remove();
|
|
1625
|
+
URL.revokeObjectURL(moduleBlobUrl);
|
|
1626
|
+
URL.revokeObjectURL(wrapperBlobUrl);
|
|
1627
|
+
}
|
|
1628
|
+
const registry = globalThis.__rc_module_registry__;
|
|
1629
|
+
const mod = registry?.[absoluteSrc] ?? {};
|
|
1630
|
+
if (registry)
|
|
1631
|
+
delete registry[absoluteSrc];
|
|
1632
|
+
return mod;
|
|
1633
|
+
}
|
|
1634
|
+
async function importDirectly(absoluteSrc) {
|
|
1635
|
+
try {
|
|
1636
|
+
return await import(
|
|
1637
|
+
/* @vite-ignore */
|
|
1638
|
+
/* webpackIgnore: true */
|
|
1639
|
+
absoluteSrc
|
|
1640
|
+
);
|
|
1641
|
+
} catch (importError) {
|
|
1642
|
+
if (!absoluteSrc.startsWith("blob:")) {
|
|
1643
|
+
warnCrossOriginFetchError("StaticLoader", absoluteSrc);
|
|
1644
|
+
}
|
|
1645
|
+
throw importError;
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
function resolveScriptSrc(script, url) {
|
|
1649
|
+
const rawSrc = typeof script.getAttribute === "function" ? script.getAttribute("src") ?? script.src : script.src;
|
|
1650
|
+
if (!rawSrc && script.textContent) {
|
|
1651
|
+
return URL.createObjectURL(
|
|
1652
|
+
new Blob(
|
|
1653
|
+
[script.textContent.replace(/import\.meta\.url/g, JSON.stringify(url))],
|
|
1654
|
+
{ type: "text/javascript" }
|
|
1655
|
+
)
|
|
1656
|
+
);
|
|
1657
|
+
}
|
|
1658
|
+
return rawSrc;
|
|
1659
|
+
}
|
|
1660
|
+
async function loadStaticRemoteComponent(scripts, url, resolveClientUrl) {
|
|
1489
1661
|
const self = globalThis;
|
|
1490
1662
|
if (self.__remote_script_entrypoint_mount__?.[url.href]) {
|
|
1491
1663
|
self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
|
|
@@ -1496,26 +1668,9 @@ async function loadStaticRemoteComponent(scripts, url) {
|
|
|
1496
1668
|
const mountUnmountSets = await Promise.all(
|
|
1497
1669
|
scripts.map(async (script) => {
|
|
1498
1670
|
try {
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
[
|
|
1503
|
-
script.textContent.replace(
|
|
1504
|
-
/import\.meta\.url/g,
|
|
1505
|
-
JSON.stringify(url)
|
|
1506
|
-
)
|
|
1507
|
-
],
|
|
1508
|
-
{
|
|
1509
|
-
type: "text/javascript"
|
|
1510
|
-
}
|
|
1511
|
-
);
|
|
1512
|
-
src = URL.createObjectURL(blob);
|
|
1513
|
-
}
|
|
1514
|
-
const mod = await import(
|
|
1515
|
-
/* @vite-ignore */
|
|
1516
|
-
/* webpackIgnore: true */
|
|
1517
|
-
new URL(src, url).href
|
|
1518
|
-
);
|
|
1671
|
+
const src = resolveScriptSrc(script, url);
|
|
1672
|
+
const absoluteSrc = new URL(src, url).href;
|
|
1673
|
+
const mod = resolveClientUrl ? await importViaCallback(absoluteSrc, resolveClientUrl) : await importDirectly(absoluteSrc);
|
|
1519
1674
|
if (src.startsWith("blob:")) {
|
|
1520
1675
|
URL.revokeObjectURL(src);
|
|
1521
1676
|
}
|
|
@@ -1613,7 +1768,12 @@ async function fetchWithHooks(url, additionalInit, options = {}) {
|
|
|
1613
1768
|
};
|
|
1614
1769
|
let res = await onRequest?.(url, init, hookOptions);
|
|
1615
1770
|
if (!res) {
|
|
1616
|
-
|
|
1771
|
+
try {
|
|
1772
|
+
res = await fetch(url, init);
|
|
1773
|
+
} catch (error) {
|
|
1774
|
+
warnCrossOriginFetchError("FetchRemoteComponent", url);
|
|
1775
|
+
throw error;
|
|
1776
|
+
}
|
|
1617
1777
|
}
|
|
1618
1778
|
const transformedRes = await onResponse?.(url, res, hookOptions);
|
|
1619
1779
|
if (transformedRes) {
|
|
@@ -1631,6 +1791,25 @@ function getClientOrServerUrl(src, serverFallback) {
|
|
|
1631
1791
|
return typeof src === "string" ? new URL(src, fallback) : src;
|
|
1632
1792
|
}
|
|
1633
1793
|
|
|
1794
|
+
// src/react/hooks/use-resolve-client-url.ts
|
|
1795
|
+
import { useMemo } from "react";
|
|
1796
|
+
import { useRemoteComponentsContext } from "#internal/react/context";
|
|
1797
|
+
|
|
1798
|
+
// src/shared/client/default-resolve-client-url.ts
|
|
1799
|
+
function bindResolveClientUrl(prop, remoteSrc) {
|
|
1800
|
+
return prop ? withRemoteSrc(prop, remoteSrc) : void 0;
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
// src/react/hooks/use-resolve-client-url.ts
|
|
1804
|
+
function useResolveClientUrl(prop, urlHref) {
|
|
1805
|
+
const { resolveClientUrl: contextValue } = useRemoteComponentsContext();
|
|
1806
|
+
const resolveClientUrl = prop ?? contextValue;
|
|
1807
|
+
return useMemo(
|
|
1808
|
+
() => bindResolveClientUrl(resolveClientUrl, urlHref),
|
|
1809
|
+
[resolveClientUrl, urlHref]
|
|
1810
|
+
);
|
|
1811
|
+
}
|
|
1812
|
+
|
|
1634
1813
|
// src/react/hooks/use-shadow-root.ts
|
|
1635
1814
|
import { useLayoutEffect, useRef, useState } from "react";
|
|
1636
1815
|
function useShadowRoot({
|
|
@@ -1703,6 +1882,7 @@ function getRemoteComponentHtml(html) {
|
|
|
1703
1882
|
}
|
|
1704
1883
|
|
|
1705
1884
|
// src/react/index.tsx
|
|
1885
|
+
import { RemoteComponentsProvider } from "#internal/react/context";
|
|
1706
1886
|
import { Fragment, jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
1707
1887
|
import { createElement as createElement2 } from "react";
|
|
1708
1888
|
function RemoteComponent({
|
|
@@ -1719,10 +1899,11 @@ function RemoteComponent({
|
|
|
1719
1899
|
onError,
|
|
1720
1900
|
onChange,
|
|
1721
1901
|
onRequest,
|
|
1722
|
-
onResponse
|
|
1902
|
+
onResponse,
|
|
1903
|
+
resolveClientUrl: _resolveClientUrl
|
|
1723
1904
|
}) {
|
|
1724
1905
|
const instanceId = useId();
|
|
1725
|
-
const name =
|
|
1906
|
+
const name = useMemo2(() => {
|
|
1726
1907
|
if (typeof src === "string") {
|
|
1727
1908
|
const url2 = new URL(
|
|
1728
1909
|
src,
|
|
@@ -1737,9 +1918,12 @@ function RemoteComponent({
|
|
|
1737
1918
|
return nameProp;
|
|
1738
1919
|
}, [src, nameProp]);
|
|
1739
1920
|
const [data, setData] = useState2(null);
|
|
1740
|
-
const url =
|
|
1921
|
+
const url = useMemo2(() => getClientOrServerUrl(src, DUMMY_FALLBACK), [src]);
|
|
1922
|
+
const resolveClientUrl = useResolveClientUrl(_resolveClientUrl, url.href);
|
|
1741
1923
|
const id = url.origin === (typeof location !== "undefined" ? location.origin : DUMMY_FALLBACK) ? url.pathname : url.href;
|
|
1742
|
-
const keySuffix = `${escapeString(id)}_${escapeString(
|
|
1924
|
+
const keySuffix = `${escapeString(id)}_${escapeString(
|
|
1925
|
+
data?.name ?? name
|
|
1926
|
+
)}_${escapeString(instanceId)}`;
|
|
1743
1927
|
const [remoteComponent, setRemoteComponent] = useState2(null);
|
|
1744
1928
|
const { shadowRoot, shadowRootContainerRef } = useShadowRoot({
|
|
1745
1929
|
isolate,
|
|
@@ -1814,6 +1998,9 @@ function RemoteComponent({
|
|
|
1814
1998
|
const previousSrc = prevSrcRef.current;
|
|
1815
1999
|
const previousName = prevNameRef.current;
|
|
1816
2000
|
prevSrcRef.current = src;
|
|
2001
|
+
if (previousSrc !== null) {
|
|
2002
|
+
htmlRef.current = null;
|
|
2003
|
+
}
|
|
1817
2004
|
onBeforeLoad?.(src);
|
|
1818
2005
|
startTransition(async () => {
|
|
1819
2006
|
try {
|
|
@@ -1824,41 +2011,18 @@ function RemoteComponent({
|
|
|
1824
2011
|
const fetchInit = {
|
|
1825
2012
|
credentials
|
|
1826
2013
|
};
|
|
2014
|
+
const resolvedUrl = new URL(
|
|
2015
|
+
resolveClientUrl?.(url.href) ?? url.href,
|
|
2016
|
+
location.href
|
|
2017
|
+
);
|
|
1827
2018
|
const abortController = new AbortController();
|
|
1828
|
-
const res = await fetchWithHooks(
|
|
2019
|
+
const res = await fetchWithHooks(resolvedUrl, fetchInit, {
|
|
1829
2020
|
onRequest,
|
|
1830
2021
|
onResponse,
|
|
1831
2022
|
abortController
|
|
1832
2023
|
});
|
|
1833
2024
|
if (!res || !res.ok) {
|
|
1834
|
-
|
|
1835
|
-
url.href,
|
|
1836
|
-
res ?? new Response(null, { status: 0 })
|
|
1837
|
-
);
|
|
1838
|
-
try {
|
|
1839
|
-
const body = await res.text();
|
|
1840
|
-
const parser2 = new DOMParser();
|
|
1841
|
-
const doc2 = parser2.parseFromString(body, "text/html");
|
|
1842
|
-
const errorTemplate = doc2.querySelector(
|
|
1843
|
-
"template[data-next-error-message]"
|
|
1844
|
-
);
|
|
1845
|
-
const errorMessage = errorTemplate?.getAttribute(
|
|
1846
|
-
"data-next-error-message"
|
|
1847
|
-
);
|
|
1848
|
-
const errorStack = errorTemplate?.getAttribute(
|
|
1849
|
-
"data-next-error-stack"
|
|
1850
|
-
);
|
|
1851
|
-
if (errorMessage) {
|
|
1852
|
-
error = new RemoteComponentsError(errorMessage);
|
|
1853
|
-
if (errorStack) {
|
|
1854
|
-
error.stack = errorStack;
|
|
1855
|
-
}
|
|
1856
|
-
}
|
|
1857
|
-
} catch (parseError) {
|
|
1858
|
-
if (isAbortError(parseError))
|
|
1859
|
-
throw parseError;
|
|
1860
|
-
}
|
|
1861
|
-
throw error;
|
|
2025
|
+
throw await errorFromFailedFetch(url.href, resolvedUrl, res);
|
|
1862
2026
|
}
|
|
1863
2027
|
const remoteHtml = await res.text();
|
|
1864
2028
|
htmlRef.current = remoteHtml;
|
|
@@ -1919,7 +2083,7 @@ function RemoteComponent({
|
|
|
1919
2083
|
prevIsRemoteComponentRef.current = isRemoteComponent;
|
|
1920
2084
|
prevUrlRef.current = url;
|
|
1921
2085
|
prevNameRef.current = remoteName;
|
|
1922
|
-
applyOriginToNodes(doc, url);
|
|
2086
|
+
applyOriginToNodes(doc, url, resolveClientUrl);
|
|
1923
2087
|
const links = Array.from(
|
|
1924
2088
|
doc.querySelectorAll("link[href]")
|
|
1925
2089
|
).filter((link) => {
|
|
@@ -2039,10 +2203,12 @@ function RemoteComponent({
|
|
|
2039
2203
|
shadowRootHtml = `<style data-remote-components-reset="">:host { all: initial; }</style>${shadowRootHtml}`;
|
|
2040
2204
|
}
|
|
2041
2205
|
shadowRoot.innerHTML = shadowRootHtml;
|
|
2206
|
+
htmlRef.current = null;
|
|
2042
2207
|
setRemoteComponent(null);
|
|
2043
2208
|
const { mount, unmount } = await loadStaticRemoteComponent(
|
|
2044
2209
|
Array.from(shadowRoot.querySelectorAll("script")),
|
|
2045
|
-
url
|
|
2210
|
+
url,
|
|
2211
|
+
resolveClientUrl
|
|
2046
2212
|
);
|
|
2047
2213
|
unmountRef.current = unmount;
|
|
2048
2214
|
await Promise.all(
|
|
@@ -2060,9 +2226,11 @@ function RemoteComponent({
|
|
|
2060
2226
|
}
|
|
2061
2227
|
)
|
|
2062
2228
|
);
|
|
2229
|
+
htmlRef.current = null;
|
|
2063
2230
|
const { mount, unmount } = await loadStaticRemoteComponent(
|
|
2064
2231
|
Array.from(component.querySelectorAll("script")),
|
|
2065
|
-
url
|
|
2232
|
+
url,
|
|
2233
|
+
resolveClientUrl
|
|
2066
2234
|
);
|
|
2067
2235
|
unmountRef.current = unmount;
|
|
2068
2236
|
await Promise.all(
|
|
@@ -2105,7 +2273,8 @@ function RemoteComponent({
|
|
|
2105
2273
|
...userShared
|
|
2106
2274
|
},
|
|
2107
2275
|
remoteShared,
|
|
2108
|
-
container: shadowRoot
|
|
2276
|
+
container: shadowRoot,
|
|
2277
|
+
resolveClientUrl
|
|
2109
2278
|
});
|
|
2110
2279
|
if (rsc) {
|
|
2111
2280
|
rsc.remove();
|
|
@@ -2151,7 +2320,8 @@ function RemoteComponent({
|
|
|
2151
2320
|
onError,
|
|
2152
2321
|
onChange,
|
|
2153
2322
|
onRequest,
|
|
2154
|
-
onResponse
|
|
2323
|
+
onResponse,
|
|
2324
|
+
resolveClientUrl
|
|
2155
2325
|
]);
|
|
2156
2326
|
if (remoteComponent instanceof Error) {
|
|
2157
2327
|
throw remoteComponent;
|
|
@@ -2182,7 +2352,8 @@ function RemoteComponent({
|
|
|
2182
2352
|
if (prevIsRemoteComponentRef.current) {
|
|
2183
2353
|
loadStaticRemoteComponent(
|
|
2184
2354
|
Array.from(shadowRoot.querySelectorAll("script")),
|
|
2185
|
-
url
|
|
2355
|
+
url,
|
|
2356
|
+
resolveClientUrl
|
|
2186
2357
|
).then(({ mount }) => {
|
|
2187
2358
|
return Promise.all(
|
|
2188
2359
|
Array.from(mount).map((mountFn) => mountFn(shadowRoot))
|
|
@@ -2265,6 +2436,8 @@ function RemoteComponent({
|
|
|
2265
2436
|
] });
|
|
2266
2437
|
}
|
|
2267
2438
|
export {
|
|
2268
|
-
RemoteComponent
|
|
2439
|
+
RemoteComponent,
|
|
2440
|
+
RemoteComponentsProvider,
|
|
2441
|
+
proxyClientRequestsThroughHost
|
|
2269
2442
|
};
|
|
2270
2443
|
//# sourceMappingURL=index.js.map
|