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