remote-components 0.0.22 → 0.0.24

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