remote-components 0.0.23 → 0.0.25

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 (74) hide show
  1. package/dist/html/host.cjs +329 -92
  2. package/dist/html/host.cjs.map +1 -1
  3. package/dist/html/host.js +332 -92
  4. package/dist/html/host.js.map +1 -1
  5. package/dist/internal/next/host/app-router-client.cjs +180 -71
  6. package/dist/internal/next/host/app-router-client.cjs.map +1 -1
  7. package/dist/internal/next/host/app-router-client.js +188 -72
  8. package/dist/internal/next/host/app-router-client.js.map +1 -1
  9. package/dist/internal/next/host/app-router-compat.cjs +114 -0
  10. package/dist/internal/next/host/app-router-compat.cjs.map +1 -0
  11. package/dist/internal/next/host/app-router-compat.d.ts +31 -0
  12. package/dist/internal/next/host/app-router-compat.js +79 -0
  13. package/dist/internal/next/host/app-router-compat.js.map +1 -0
  14. package/dist/internal/next/remote/render-client.cjs +10 -2
  15. package/dist/internal/next/remote/render-client.cjs.map +1 -1
  16. package/dist/internal/next/remote/render-client.js +10 -2
  17. package/dist/internal/next/remote/render-client.js.map +1 -1
  18. package/dist/internal/shared/client/apply-origin.cjs +61 -0
  19. package/dist/internal/shared/client/apply-origin.cjs.map +1 -0
  20. package/dist/internal/shared/client/apply-origin.d.ts +3 -0
  21. package/dist/internal/shared/client/apply-origin.js +37 -0
  22. package/dist/internal/shared/client/apply-origin.js.map +1 -0
  23. package/dist/internal/shared/client/polyfill.cjs +149 -0
  24. package/dist/internal/shared/client/polyfill.cjs.map +1 -0
  25. package/dist/internal/shared/client/polyfill.d.ts +6 -0
  26. package/dist/internal/shared/client/polyfill.js +124 -0
  27. package/dist/internal/shared/client/polyfill.js.map +1 -0
  28. package/dist/internal/shared/client/remote-component.cjs +20 -13
  29. package/dist/internal/shared/client/remote-component.cjs.map +1 -1
  30. package/dist/internal/shared/client/remote-component.d.ts +4 -3
  31. package/dist/internal/shared/client/remote-component.js +20 -13
  32. package/dist/internal/shared/client/remote-component.js.map +1 -1
  33. package/dist/internal/shared/ssr/dom-flight.cjs +40 -0
  34. package/dist/internal/shared/ssr/dom-flight.cjs.map +1 -1
  35. package/dist/internal/shared/ssr/dom-flight.js +40 -0
  36. package/dist/internal/shared/ssr/dom-flight.js.map +1 -1
  37. package/dist/internal/shared/ssr/fetch-remote-component.cjs +1 -1
  38. package/dist/internal/shared/ssr/fetch-remote-component.cjs.map +1 -1
  39. package/dist/internal/shared/ssr/fetch-remote-component.d.ts +6 -0
  40. package/dist/internal/shared/ssr/fetch-remote-component.js +1 -1
  41. package/dist/internal/shared/ssr/fetch-remote-component.js.map +1 -1
  42. package/dist/internal/shared/utils.cjs +31 -0
  43. package/dist/internal/shared/utils.cjs.map +1 -0
  44. package/dist/internal/shared/utils.d.ts +3 -0
  45. package/dist/internal/shared/utils.js +7 -0
  46. package/dist/internal/shared/utils.js.map +1 -0
  47. package/dist/next/config.cjs +50 -28
  48. package/dist/next/config.cjs.map +1 -1
  49. package/dist/next/config.js +50 -28
  50. package/dist/next/config.js.map +1 -1
  51. package/dist/next/host/client/index.cjs +16 -1
  52. package/dist/next/host/client/index.cjs.map +1 -1
  53. package/dist/next/host/client/index.js +16 -1
  54. package/dist/next/host/client/index.js.map +1 -1
  55. package/dist/next/host/pages-router-server.cjs +27 -13
  56. package/dist/next/host/pages-router-server.cjs.map +1 -1
  57. package/dist/next/host/pages-router-server.js +27 -13
  58. package/dist/next/host/pages-router-server.js.map +1 -1
  59. package/dist/next/middleware.cjs +5 -2
  60. package/dist/next/middleware.cjs.map +1 -1
  61. package/dist/next/middleware.d.ts +1 -0
  62. package/dist/next/middleware.js +5 -2
  63. package/dist/next/middleware.js.map +1 -1
  64. package/dist/next/remote/pages-router.cjs +3 -1
  65. package/dist/next/remote/pages-router.cjs.map +1 -1
  66. package/dist/next/remote/pages-router.d.ts +1 -0
  67. package/dist/next/remote/pages-router.js +3 -1
  68. package/dist/next/remote/pages-router.js.map +1 -1
  69. package/dist/react/index.cjs +213 -161
  70. package/dist/react/index.cjs.map +1 -1
  71. package/dist/react/index.d.ts +2 -1
  72. package/dist/react/index.js +202 -150
  73. package/dist/react/index.js.map +1 -1
  74. package/package.json +1 -1
@@ -23,13 +23,17 @@ __export(react_exports, {
23
23
  module.exports = __toCommonJS(react_exports);
24
24
  var import_jsx_runtime = require("react/jsx-runtime");
25
25
  var import_react = require("react");
26
+ var import_react2 = require("react");
26
27
  var import_react_dom = require("react-dom");
27
28
  var import_remote_component = require("#internal/shared/client/remote-component");
29
+ var import_polyfill = require("#internal/shared/client/polyfill");
30
+ var import_apply_origin = require("#internal/shared/client/apply-origin");
31
+ var import_utils = require("#internal/shared/utils");
28
32
  function getRemoteComponentHtml(html) {
29
33
  if (typeof document === "undefined")
30
34
  return html;
31
- const temp = document.createElement("div");
32
- temp.innerHTML = html;
35
+ const parser = new DOMParser();
36
+ const temp = parser.parseFromString(html, "text/html");
33
37
  const ssrRemoteComponentContainer = temp.querySelector(
34
38
  'div[id^="__REMOTE_COMPONENT"]'
35
39
  );
@@ -44,6 +48,10 @@ function getRemoteComponentHtml(html) {
44
48
  }
45
49
  return "";
46
50
  }
51
+ const attrToProp = {
52
+ fetchpriority: "fetchPriority",
53
+ crossorigin: "crossOrigin"
54
+ };
47
55
  function RemoteComponent({
48
56
  src,
49
57
  isolate,
@@ -54,7 +62,7 @@ function RemoteComponent({
54
62
  shared = {},
55
63
  children
56
64
  }) {
57
- const name = (0, import_react.useMemo)(() => {
65
+ const name = (0, import_react2.useMemo)(() => {
58
66
  if (typeof src === "string") {
59
67
  const url2 = new URL(
60
68
  src,
@@ -68,26 +76,25 @@ function RemoteComponent({
68
76
  }
69
77
  return nameProp;
70
78
  }, [src, nameProp]);
71
- const [data, setData] = (0, import_react.useState)(null);
72
- const [remoteComponent, setRemoteComponent] = (0, import_react.useState)(null);
73
- const shadowRootContainerRef = (0, import_react.useRef)(null);
74
- const [shadowRoot, setShadowRoot] = (0, import_react.useState)(() => {
79
+ const [data, setData] = (0, import_react2.useState)(null);
80
+ const [remoteComponent, setRemoteComponent] = (0, import_react2.useState)(null);
81
+ const shadowRootContainerRef = (0, import_react2.useRef)(null);
82
+ const [shadowRoot, setShadowRoot] = (0, import_react2.useState)(() => {
75
83
  const self = globalThis;
76
- const shadowRootKey = `__remote_components_shadowroot_${src ? new URL(src, typeof location !== "undefined" ? location.origin : "http://localhost").href.replace(/[^a-z0-9]/g, "_") : ""}_${(data?.name ?? name).replace(/[^a-z0-9]/g, "_")}`;
77
- const ssrShadowRoot = typeof document !== "undefined" ? document.getElementById(
78
- `shadowroot_${src ? new URL(src, typeof location !== "undefined" ? location.origin : "http://localhost").href.replace(/[^a-z0-9]/g, "_") : ""}_${(data?.name ?? name).replace(/[^a-z0-9]/g, "_")}`
84
+ const shadowRootKey = `__remote_components_shadowroot_${src ? (0, import_utils.escapeString)(new URL(src, typeof location !== "undefined" ? location.origin : "http://localhost").href) : ""}_${(0, import_utils.escapeString)(data?.name ?? name)}`;
85
+ const ssrShadowRoot = typeof document !== "undefined" ? document.querySelector(
86
+ `[data-remote-component-id="shadowroot_${src ? (0, import_utils.escapeString)(new URL(src, typeof location !== "undefined" ? location.origin : "http://localhost").href) : ""}_${(0, import_utils.escapeString)(data?.name ?? name)}"]`
79
87
  )?.shadowRoot ?? self[shadowRootKey] ?? null : null;
80
88
  self[shadowRootKey] = null;
81
89
  return ssrShadowRoot;
82
90
  });
83
- const htmlRef = (0, import_react.useRef)(
84
- typeof document !== "undefined" ? document.getElementById(
85
- `shadowroot_${src ? new URL(src, typeof location !== "undefined" ? location.origin : "http://localhost").href.replace(/[^a-z0-9]/g, "_") : ""}_${(data?.name ?? name).replace(/[^a-z0-9]/g, "_")}`
91
+ const htmlRef = (0, import_react2.useRef)(
92
+ typeof document !== "undefined" ? document.querySelector(
93
+ `[data-remote-component-id="shadowroot_${src ? (0, import_utils.escapeString)(new URL(src, typeof location !== "undefined" ? location.origin : "http://localhost").href) : ""}_${(0, import_utils.escapeString)(data?.name ?? name)}"]`
86
94
  )?.shadowRoot?.innerHTML ?? document.getElementById(`__REMOTE_COMPONENT${name}`)?.innerHTML ?? document.querySelector(`div[data-bundle][data-route][id^="${name}"]`)?.innerHTML ?? document.querySelector("div[data-bundle][data-route]")?.innerHTML : null
87
95
  );
88
- const ssrHtmlRef = (0, import_react.useRef)(htmlRef.current);
89
- const endTemplateRef = (0, import_react.useRef)(null);
90
- const childrenRef = (0, import_react.useRef)(
96
+ const endTemplateRef = (0, import_react2.useRef)(null);
97
+ const childrenRef = (0, import_react2.useRef)(
91
98
  typeof document !== "undefined" ? (() => {
92
99
  let el = document.querySelector(`template[id="${name}_start"]`);
93
100
  const elements = [];
@@ -100,7 +107,8 @@ function RemoteComponent({
100
107
  return elements;
101
108
  })() : []
102
109
  );
103
- (0, import_react.useLayoutEffect)(() => {
110
+ const prevSrcRef = (0, import_react2.useRef)(null);
111
+ (0, import_react2.useLayoutEffect)(() => {
104
112
  if (childrenRef.current.length > 0 && remoteComponent) {
105
113
  childrenRef.current.forEach((el) => {
106
114
  el.remove();
@@ -108,14 +116,17 @@ function RemoteComponent({
108
116
  childrenRef.current = [];
109
117
  }
110
118
  if (isolate !== false && typeof document !== "undefined" && !shadowRoot) {
119
+ const self = globalThis;
120
+ const shadowRootKey = `__remote_components_shadowroot_${src ? (0, import_utils.escapeString)(new URL(src, typeof location !== "undefined" ? location.origin : "http://localhost").href) : ""}_${(0, import_utils.escapeString)(data?.name ?? name)}`;
111
121
  let shadowRootElement = null;
112
- const element = document.getElementById(
113
- `shadowroot_${src ? new URL(src, typeof location !== "undefined" ? location.origin : "http://localhost").href.replace(/[^a-z0-9]/g, "_") : ""}_${(data?.name ?? name).replace(/[^a-z0-9]/g, "_")}`
122
+ const element = document.querySelector(
123
+ `[data-remote-component-id="shadowroot_${src ? (0, import_utils.escapeString)(new URL(src, typeof location !== "undefined" ? location.origin : "http://localhost").href) : ""}_${(0, import_utils.escapeString)(data?.name ?? name)}"]`
114
124
  );
115
- shadowRootElement = element?.shadowRoot ?? null;
125
+ shadowRootElement = self[shadowRootKey] ?? element?.shadowRoot ?? null;
116
126
  if (!shadowRootElement && element) {
117
127
  try {
118
128
  shadowRootElement = element.attachShadow({ mode });
129
+ self[shadowRootKey] = shadowRootElement;
119
130
  } catch {
120
131
  }
121
132
  }
@@ -126,8 +137,31 @@ function RemoteComponent({
126
137
  setShadowRoot(shadowRootElement);
127
138
  }
128
139
  }
129
- }, [name, isolate, shadowRoot, remoteComponent, mode]);
130
- const url = (0, import_react.useMemo)(() => {
140
+ }, [name, isolate, shadowRoot, remoteComponent, mode, src, data]);
141
+ (0, import_react2.useLayoutEffect)(() => {
142
+ if (shadowRoot && remoteComponent) {
143
+ const resetStyles = shadowRoot.querySelectorAll(
144
+ "style[data-remote-components-reset]"
145
+ );
146
+ if (resetStyles.length > 1) {
147
+ resetStyles.forEach((style, index) => {
148
+ if (index > 0) {
149
+ style.remove();
150
+ }
151
+ });
152
+ }
153
+ htmlRef.current = null;
154
+ let el = shadowRoot.childNodes[0] ?? null;
155
+ while (el && (!("id" in el) || el.id !== `${name}_start`)) {
156
+ const nextEl = el.nextSibling;
157
+ if (el.nodeName !== "LINK" && el.nodeName !== "STYLE") {
158
+ el.parentNode?.removeChild(el);
159
+ }
160
+ el = nextEl;
161
+ }
162
+ }
163
+ }, [shadowRoot, remoteComponent, name]);
164
+ const url = (0, import_react2.useMemo)(() => {
131
165
  if (typeof src !== "string")
132
166
  return new URL(
133
167
  typeof document !== "undefined" ? location.href : "http://localhost"
@@ -138,117 +172,137 @@ function RemoteComponent({
138
172
  return new URL(src, "http://localhost");
139
173
  }
140
174
  }, [src]);
141
- (0, import_react.useEffect)(() => {
175
+ (0, import_react2.useEffect)(() => {
142
176
  let mounted = true;
143
- (0, import_react.startTransition)(async () => {
144
- try {
145
- let html = getRemoteComponentHtml(
146
- htmlRef.current ?? (endTemplateRef.current?.previousElementSibling?.tagName === "div" ? endTemplateRef.current.previousElementSibling.innerHTML : "")
147
- );
148
- if (!html && src) {
149
- const fetchInit = {
150
- method: "GET",
151
- headers: {
152
- Accept: "text/html"
153
- },
154
- credentials
177
+ if (src && src !== prevSrcRef.current) {
178
+ prevSrcRef.current = src;
179
+ (0, import_react2.startTransition)(async () => {
180
+ try {
181
+ let html = getRemoteComponentHtml(
182
+ htmlRef.current ?? (endTemplateRef.current?.previousElementSibling?.tagName === "div" ? endTemplateRef.current.previousElementSibling.innerHTML : "")
183
+ );
184
+ if (!html && src) {
185
+ const fetchInit = {
186
+ method: "GET",
187
+ headers: {
188
+ Accept: "text/html"
189
+ },
190
+ credentials
191
+ };
192
+ const res = await fetch(url, fetchInit);
193
+ if (!res.ok) {
194
+ throw new Error(
195
+ `Failed to fetch remote component "${name}": ${res.status}`
196
+ );
197
+ }
198
+ const remoteHtml = await res.text();
199
+ htmlRef.current = remoteHtml;
200
+ html = getRemoteComponentHtml(remoteHtml);
201
+ }
202
+ const parser = new DOMParser();
203
+ const doc = parser.parseFromString(html, "text/html");
204
+ const component = doc.querySelector(`div[data-bundle][data-route][id^="${name}"]`) ?? // fallback to the first element with the data-bundle and data-route attributes when not using a named remote component
205
+ doc.querySelector("div[data-bundle][data-route]") ?? // fallback to Next.js Pages Router
206
+ doc.querySelector("div#__next");
207
+ const nextData = JSON.parse(
208
+ (doc.querySelector("#__NEXT_DATA__") ?? doc.querySelector("#__REMOTE_NEXT_DATA__"))?.textContent ?? "null"
209
+ );
210
+ const remoteName = component?.getAttribute("id")?.replace(/_ssr$/, "") || (nextData ? "__next" : name);
211
+ const rsc = doc.querySelector(`#${remoteName}_rsc`);
212
+ const bundle = component?.getAttribute("data-bundle") || nextData?.props.__REMOTE_COMPONENT__?.bundle || "default";
213
+ const metadata = {
214
+ name: remoteName,
215
+ bundle,
216
+ route: component?.getAttribute("data-route") ?? nextData?.page ?? import_remote_component.DEFAULT_ROUTE,
217
+ runtime: component?.getAttribute("data-runtime") ?? (nextData?.props.__REMOTE_COMPONENT__?.runtime || import_remote_component.RUNTIME_WEBPACK)
155
218
  };
156
- const res = await fetch(url, fetchInit);
157
- if (!res.ok) {
158
- throw new Error(
159
- `Failed to fetch remote component "${name}": ${res.status}`
160
- );
219
+ const remoteSharedEl = doc.querySelector(
220
+ `#${remoteName}_shared[data-remote-components-shared]`
221
+ );
222
+ const remoteShared = nextData?.props.__REMOTE_COMPONENT__?.shared ?? (JSON.parse(remoteSharedEl?.textContent ?? "{}") ?? {});
223
+ remoteSharedEl?.remove();
224
+ if (!component || !(rsc || nextData)) {
225
+ throw new Error(`Failed to find component with id "${remoteName}"`);
161
226
  }
162
- const remoteHtml = await res.text();
163
- htmlRef.current = remoteHtml;
164
- html = getRemoteComponentHtml(remoteHtml);
165
- }
166
- const doc = document.createElement("div");
167
- doc.innerHTML = html;
168
- const component = doc.querySelector(`div[data-bundle][data-route][id^="${name}"]`) ?? // fallback to the first element with the data-bundle and data-route attributes when not using a named remote component
169
- doc.querySelector("div[data-bundle][data-route]") ?? // fallback to Next.js Pages Router
170
- doc.querySelector("div#__next");
171
- const nextData = JSON.parse(
172
- (doc.querySelector("#__NEXT_DATA__") ?? doc.querySelector("#__REMOTE_NEXT_DATA__"))?.textContent ?? "null"
173
- );
174
- const remoteName = component?.getAttribute("id")?.replace(/_ssr$/, "") || (nextData ? "__next" : name);
175
- const rsc = doc.querySelector(`#${remoteName}_rsc`);
176
- const bundle = component?.getAttribute("data-bundle") || nextData?.props.__REMOTE_COMPONENT__?.bundle || "default";
177
- const metadata = {
178
- name: remoteName,
179
- bundle,
180
- route: component?.getAttribute("data-route") ?? nextData?.page ?? import_remote_component.DEFAULT_ROUTE,
181
- runtime: component?.getAttribute("data-runtime") ?? (nextData?.props.__REMOTE_COMPONENT__?.runtime || import_remote_component.RUNTIME_WEBPACK)
182
- };
183
- const remoteSharedEl = doc.querySelector(`#${remoteName}_shared`);
184
- const remoteShared = JSON.parse(remoteSharedEl?.textContent ?? "{}") ?? {};
185
- remoteSharedEl?.parentElement?.removeChild(remoteSharedEl);
186
- if (!component || !(rsc || nextData)) {
187
- throw new Error(`Failed to find component with id "${remoteName}"`);
188
- }
189
- const links = Array.from(
190
- doc.querySelectorAll("link[href]")
191
- ).map((link) => ({
192
- rel: link.rel,
193
- href: new URL(link.getAttribute("href") ?? link.href, url).href,
194
- as: link.getAttribute("as") || void 0
195
- }));
196
- const scripts = doc.querySelectorAll(
197
- "script[src],script[data-src]"
198
- );
199
- const inlineScripts = doc.querySelectorAll(
200
- "script:not([src]):not([data-src]):not([id*='_rsc']):not([id='__NEXT_DATA__']):not([id='__REMOTE_NEXT_DATA__'])"
201
- );
202
- const self = globalThis;
203
- const prevNextScripts = self.__next_s;
204
- const nextScripts = [];
205
- self.__next_s = nextScripts;
206
- await Promise.all(
207
- Array.from(inlineScripts).map((script) => {
208
- return new Promise((resolve) => {
209
- if (script.textContent && !script.textContent.includes("self.__next_f=") && !script.textContent.includes("self.__next_f.push")) {
210
- if (!script.getAttribute("type") || script.getAttribute("type") === "text/javascript" || script.getAttribute("type") === "application/javascript") {
211
- const newScript = document.createElement("script");
212
- const blob = new Blob([script.textContent], {
213
- type: "application/javascript"
214
- });
215
- const blobUrl = URL.createObjectURL(blob);
216
- newScript.onload = () => {
217
- resolve(void 0);
218
- URL.revokeObjectURL(blobUrl);
219
- newScript.remove();
220
- };
221
- newScript.onerror = () => {
222
- URL.revokeObjectURL(blobUrl);
223
- newScript.remove();
227
+ (0, import_apply_origin.applyOriginToNodes)(doc, url);
228
+ const links = Array.from(
229
+ doc.querySelectorAll("link[href]")
230
+ ).filter((link) => {
231
+ return !component.contains(link);
232
+ }).map((link) => ({
233
+ href: new URL(link.getAttribute("href") ?? link.href, url).href,
234
+ ...link.getAttributeNames().reduce((acc, key) => {
235
+ if (key !== "href") {
236
+ acc[attrToProp[key] ?? key] = link.getAttribute(key) ?? "";
237
+ }
238
+ return acc;
239
+ }, {})
240
+ }));
241
+ const scripts = doc.querySelectorAll(
242
+ "script[src],script[data-src]"
243
+ );
244
+ const inlineScripts = doc.querySelectorAll(
245
+ "script:not([src]):not([data-src]):not([id*='_rsc']):not([id='__NEXT_DATA__']):not([id='__REMOTE_NEXT_DATA__'])"
246
+ );
247
+ const self = globalThis;
248
+ const prevNextScripts = self.__next_s;
249
+ const nextScripts = [];
250
+ self.__next_s = nextScripts;
251
+ await Promise.all(
252
+ Array.from(inlineScripts).filter(
253
+ (script) => !(script.id.endsWith("_shared") && script.getAttribute("type") === "application/json" && typeof script.getAttribute(
254
+ "data-remote-components-shared"
255
+ ) === "string")
256
+ ).map((script) => {
257
+ return new Promise((resolve) => {
258
+ if (script.textContent && !script.textContent.includes("self.__next_f=") && !script.textContent.includes("self.__next_f.push")) {
259
+ if (!script.getAttribute("type") || script.getAttribute("type") === "text/javascript" || script.getAttribute("type") === "application/javascript") {
260
+ const newScript = document.createElement("script");
261
+ const blob = new Blob([script.textContent], {
262
+ type: "application/javascript"
263
+ });
264
+ const blobUrl = URL.createObjectURL(blob);
265
+ newScript.onload = () => {
266
+ resolve(void 0);
267
+ URL.revokeObjectURL(blobUrl);
268
+ newScript.remove();
269
+ };
270
+ newScript.onerror = () => {
271
+ URL.revokeObjectURL(blobUrl);
272
+ newScript.remove();
273
+ resolve(void 0);
274
+ };
275
+ newScript.src = blobUrl;
276
+ document.body.appendChild(newScript);
277
+ } else {
224
278
  resolve(void 0);
225
- };
226
- newScript.src = blobUrl;
227
- document.body.appendChild(newScript);
279
+ document.body.appendChild(script);
280
+ }
228
281
  } else {
229
282
  resolve(void 0);
230
- document.body.appendChild(script);
231
283
  }
232
- } else {
233
- resolve(void 0);
234
- }
235
- });
236
- })
237
- );
238
- nextScripts.forEach(([scriptSrc, props]) => {
239
- const script = document.createElement("script");
240
- if (scriptSrc) {
241
- script.src = scriptSrc;
242
- }
243
- if (typeof props.children === "string") {
244
- script.textContent = props.children;
245
- }
246
- (0, import_remote_component.setAttributesFromProps)(script, props);
247
- document.head.appendChild(script);
248
- });
249
- self.__next_s = prevNextScripts;
250
- if (mounted) {
284
+ });
285
+ })
286
+ );
287
+ nextScripts.forEach(([scriptSrc, props]) => {
288
+ const script = document.createElement("script");
289
+ if (scriptSrc) {
290
+ script.src = scriptSrc;
291
+ }
292
+ if (typeof props.children === "string") {
293
+ script.textContent = props.children;
294
+ }
295
+ (0, import_remote_component.setAttributesFromProps)(script, props);
296
+ document.head.appendChild(script);
297
+ });
298
+ self.__next_s = prevNextScripts;
299
+ let rscName;
251
300
  if (rsc) {
301
+ rscName = `__remote_component_rsc_${(0, import_utils.escapeString)(url.href)}_${(0, import_utils.escapeString)(remoteName)}`;
302
+ rsc.textContent = rsc.textContent?.replace(
303
+ new RegExp(`self\\["${remoteName}"\\]`, "g"),
304
+ `self["${rscName}"]`
305
+ ) ?? "";
252
306
  document.body.appendChild(rsc);
253
307
  }
254
308
  const newData = {
@@ -258,9 +312,11 @@ function RemoteComponent({
258
312
  url: url.href,
259
313
  data: rsc ? (rsc.textContent || "").split("\n").filter(Boolean) : []
260
314
  };
315
+ const userShared = await shared;
261
316
  const result = await (0, import_remote_component.loadRemoteComponent)({
262
317
  url: new URL(url, location.origin),
263
318
  name: remoteName,
319
+ rscName,
264
320
  bundle,
265
321
  route: metadata.route,
266
322
  runtime: metadata.runtime,
@@ -284,25 +340,29 @@ function RemoteComponent({
284
340
  ).href
285
341
  };
286
342
  }),
287
- shared,
343
+ shared: {
344
+ ...(0, import_polyfill.sharedPolyfills)(userShared),
345
+ ...userShared
346
+ },
288
347
  remoteShared,
289
348
  container: shadowRoot
290
349
  });
350
+ if (rsc) {
351
+ rsc.remove();
352
+ }
353
+ setData(newData);
354
+ if (result.error) {
355
+ setRemoteComponent(result.error);
356
+ } else {
357
+ setRemoteComponent(result.component);
358
+ }
359
+ } catch (error) {
291
360
  if (mounted) {
292
- setData(newData);
293
- if (result.error) {
294
- setRemoteComponent(result.error);
295
- } else {
296
- setRemoteComponent(result.component);
297
- }
361
+ setRemoteComponent(error);
298
362
  }
299
363
  }
300
- } catch (error) {
301
- if (mounted) {
302
- setRemoteComponent(error);
303
- }
304
- }
305
- });
364
+ });
365
+ }
306
366
  return () => {
307
367
  mounted = false;
308
368
  };
@@ -316,16 +376,17 @@ function RemoteComponent({
316
376
  route: data?.route || import_remote_component.DEFAULT_ROUTE,
317
377
  runtime: data?.runtime || import_remote_component.RUNTIME_WEBPACK
318
378
  }) });
319
- const linksToRender = data?.links?.map((link) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
379
+ const resetStyle = reset ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { "data-remote-components-reset": "", children: `:host { all: initial; }` }) : null;
380
+ const linksToRender = data?.links?.map((link) => /* @__PURE__ */ (0, import_react.createElement)(
320
381
  "link",
321
382
  {
322
- as: link.as,
383
+ ...link,
323
384
  href: new URL(link.href, url).href,
324
- rel: link.rel
325
- },
326
- `${link.href}_${link.rel}`
385
+ key: JSON.stringify(link)
386
+ }
327
387
  )) || null;
328
388
  const componentToRender = /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
389
+ resetStyle,
329
390
  linksToRender,
330
391
  remoteComponent ?? children
331
392
  ] });
@@ -334,20 +395,13 @@ function RemoteComponent({
334
395
  if (shadowRemoteComponentHtml) {
335
396
  shadowRemoteComponentHtml.remove();
336
397
  }
337
- if (shadowRoot && remoteComponent && htmlRef.current) {
338
- if (ssrHtmlRef.current) {
339
- const content = shadowRoot.querySelectorAll(":not(link,style)");
340
- content.forEach((node) => node.remove());
341
- ssrHtmlRef.current = null;
342
- }
343
- htmlRef.current = null;
344
- }
345
398
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
346
399
  metadataJson,
347
400
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
348
401
  "div",
349
402
  {
350
- id: `shadowroot_${src ? new URL(src, typeof location !== "undefined" ? location.origin : "http://localhost").href.replace(/[^a-z0-9]/g, "_") : ""}_${(data?.name ?? name).replace(/[^a-z0-9]/g, "_")}`,
403
+ "data-remote-component-id": `shadowroot_${src ? (0, import_utils.escapeString)(new URL(src, typeof location !== "undefined" ? location.origin : "http://localhost").href) : ""}_${(0, import_utils.escapeString)(data?.name ?? name)}`,
404
+ id: `shadowroot_${data?.name ?? name}`,
351
405
  ref: shadowRootContainerRef,
352
406
  children: [
353
407
  typeof document === "undefined" ? (
@@ -362,28 +416,25 @@ function RemoteComponent({
362
416
  src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="
363
417
  onload="(function(el){
364
418
  const root = el.getRootNode();
365
- globalThis.__remote_components_shadowroot_${src ? new URL(src, typeof location !== "undefined" ? location.origin : "http://localhost").href.replace(/[^a-z0-9]/g, "_") : ""}_${(data?.name ?? name).replace(/[^a-z0-9]/g, "_")} = root;
419
+ globalThis.__remote_components_shadowroot_${src ? (0, import_utils.escapeString)(new URL(src, typeof location !== "undefined" ? location.origin : "http://localhost").href) : ""}_${(0, import_utils.escapeString)(data?.name ?? name)} = root;
366
420
  el.parentElement.remove();
367
421
  })(this)"
368
422
  />`
369
423
  }
370
424
  }
371
425
  ) : null,
372
- reset ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
373
- "style",
374
- {
375
- "data-remote-components-reset": true,
376
- children: `:host { all: initial; }`
377
- }
378
- ) : null,
426
+ resetStyle,
379
427
  linksToRender,
380
428
  children
381
429
  ] })
382
430
  ) : null,
383
431
  shadowRoot && remoteComponent ? (0, import_react_dom.createPortal)(
384
432
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
433
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("template", { id: `${name}_start` }),
434
+ resetStyle,
385
435
  linksToRender,
386
- remoteComponent
436
+ remoteComponent,
437
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("template", { id: `${name}_end`, ref: endTemplateRef })
387
438
  ] }),
388
439
  shadowRoot
389
440
  ) : null
@@ -392,6 +443,7 @@ function RemoteComponent({
392
443
  )
393
444
  ] });
394
445
  }
446
+ htmlRef.current = null;
395
447
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
396
448
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("template", { id: `${name}_start` }),
397
449
  metadataJson,