remote-components 0.0.33 → 0.0.35

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 (77) hide show
  1. package/dist/{component-loader-28ad0083.d.ts → component-loader-8951c052.d.ts} +6 -4
  2. package/dist/html/host.cjs +353 -129
  3. package/dist/html/host.cjs.map +1 -1
  4. package/dist/html/host.js +353 -129
  5. package/dist/html/host.js.map +1 -1
  6. package/dist/html/remote.cjs +199 -0
  7. package/dist/html/remote.cjs.map +1 -0
  8. package/dist/html/remote.d.ts +2 -0
  9. package/dist/html/remote.js +198 -0
  10. package/dist/html/remote.js.map +1 -0
  11. package/dist/internal/next/host/app-router-client.cjs +60 -19
  12. package/dist/internal/next/host/app-router-client.cjs.map +1 -1
  13. package/dist/internal/next/host/app-router-client.d.ts +3 -3
  14. package/dist/internal/next/host/app-router-client.js +65 -23
  15. package/dist/internal/next/host/app-router-client.js.map +1 -1
  16. package/dist/internal/next/host/app-router-compat.cjs.map +1 -1
  17. package/dist/internal/next/host/app-router-compat.d.ts +1 -1
  18. package/dist/internal/next/host/app-router-compat.js.map +1 -1
  19. package/dist/internal/next/remote/render-server.cjs +1 -1
  20. package/dist/internal/next/remote/render-server.cjs.map +1 -1
  21. package/dist/internal/next/remote/render-server.js +1 -1
  22. package/dist/internal/next/remote/render-server.js.map +1 -1
  23. package/dist/internal/shared/client/polyfill.cjs.map +1 -1
  24. package/dist/internal/shared/client/polyfill.js.map +1 -1
  25. package/dist/internal/shared/client/remote-component.cjs +237 -133
  26. package/dist/internal/shared/client/remote-component.cjs.map +1 -1
  27. package/dist/internal/shared/client/remote-component.d.ts +29 -22
  28. package/dist/internal/shared/client/remote-component.js +235 -133
  29. package/dist/internal/shared/client/remote-component.js.map +1 -1
  30. package/dist/internal/shared/error.cjs +1 -1
  31. package/dist/internal/shared/error.cjs.map +1 -1
  32. package/dist/internal/shared/error.d.ts +1 -1
  33. package/dist/internal/shared/error.js +1 -1
  34. package/dist/internal/shared/error.js.map +1 -1
  35. package/dist/internal/shared/ssr/dom-flight.cjs +24 -8
  36. package/dist/internal/shared/ssr/dom-flight.cjs.map +1 -1
  37. package/dist/internal/shared/ssr/dom-flight.d.ts +1 -1
  38. package/dist/internal/shared/ssr/dom-flight.js +24 -8
  39. package/dist/internal/shared/ssr/dom-flight.js.map +1 -1
  40. package/dist/internal/shared/ssr/fetch-remote-component.cjs +8 -4
  41. package/dist/internal/shared/ssr/fetch-remote-component.cjs.map +1 -1
  42. package/dist/internal/shared/ssr/fetch-remote-component.d.ts +2 -1
  43. package/dist/internal/shared/ssr/fetch-remote-component.js +9 -5
  44. package/dist/internal/shared/ssr/fetch-remote-component.js.map +1 -1
  45. package/dist/next/config.cjs +100 -86
  46. package/dist/next/config.cjs.map +1 -1
  47. package/dist/next/config.js +100 -86
  48. package/dist/next/config.js.map +1 -1
  49. package/dist/next/host/app-router-server.cjs +1 -0
  50. package/dist/next/host/app-router-server.cjs.map +1 -1
  51. package/dist/next/host/app-router-server.js +2 -1
  52. package/dist/next/host/app-router-server.js.map +1 -1
  53. package/dist/next/host/client/index.cjs +1381 -1187
  54. package/dist/next/host/client/index.cjs.map +1 -1
  55. package/dist/next/host/client/index.d.ts +1 -1
  56. package/dist/next/host/client/index.js +682 -490
  57. package/dist/next/host/client/index.js.map +1 -1
  58. package/dist/next/host/pages-router-server.cjs +4 -4
  59. package/dist/next/host/pages-router-server.cjs.map +1 -1
  60. package/dist/next/host/pages-router-server.js +4 -4
  61. package/dist/next/host/pages-router-server.js.map +1 -1
  62. package/dist/next/{middleware.cjs → proxy.cjs} +24 -14
  63. package/dist/next/proxy.cjs.map +1 -0
  64. package/dist/next/proxy.d.ts +29 -0
  65. package/dist/next/{middleware.js → proxy.js} +21 -11
  66. package/dist/next/proxy.js.map +1 -0
  67. package/dist/react/index.cjs +201 -107
  68. package/dist/react/index.cjs.map +1 -1
  69. package/dist/react/index.d.ts +2 -2
  70. package/dist/react/index.js +206 -112
  71. package/dist/react/index.js.map +1 -1
  72. package/dist/{types-7c207455.d.ts → types-4e7dea94.d.ts} +2 -1
  73. package/dist/{types-e4a3fa37.d.ts → types-cbf6c34f.d.ts} +2 -2
  74. package/package.json +22 -9
  75. package/dist/next/middleware.cjs.map +0 -1
  76. package/dist/next/middleware.d.ts +0 -29
  77. package/dist/next/middleware.js.map +0 -1
package/dist/html/host.js CHANGED
@@ -8,38 +8,6 @@ var __export = (target, all) => {
8
8
  __defProp(target, name, { get: all[name], enumerable: true });
9
9
  };
10
10
 
11
- // src/shared/webpack/shared-modules.ts
12
- function applySharedModules(bundle, resolve) {
13
- const self = globalThis;
14
- if (self.__remote_webpack_require__?.[bundle]) {
15
- const modulePaths = Object.keys(
16
- self.__remote_webpack_module_map__?.[bundle] ?? self.__remote_webpack_require__[bundle].m ?? {}
17
- );
18
- for (const [key, value] of Object.entries(resolve)) {
19
- let ids = modulePaths.filter((p) => p === key);
20
- if (ids.length === 0) {
21
- ids = modulePaths.filter((p) => p.includes(key));
22
- }
23
- for (let id of ids) {
24
- const webpackBundle = self.__remote_webpack_require__[bundle];
25
- if (webpackBundle.m) {
26
- if (self.__remote_webpack_module_map__?.[bundle]?.[id]) {
27
- id = `${self.__remote_webpack_module_map__[bundle][id]}`;
28
- }
29
- webpackBundle.m[id] = (module) => {
30
- module.exports = value;
31
- };
32
- }
33
- }
34
- }
35
- }
36
- }
37
- var init_shared_modules = __esm({
38
- "src/shared/webpack/shared-modules.ts"() {
39
- "use strict";
40
- }
41
- });
42
-
43
11
  // src/shared/error.ts
44
12
  function multipleRemoteComponentsError(url) {
45
13
  return new Error(
@@ -57,9 +25,9 @@ var init_error = __esm({
57
25
  "src/shared/error.ts"() {
58
26
  "use strict";
59
27
  RemoteComponentsError = class extends Error {
28
+ code = "REMOTE_COMPONENTS_ERROR";
60
29
  constructor(message, options) {
61
30
  super(message, options);
62
- this.code = "REMOTE_COMPONENTS_ERROR";
63
31
  this.name = "RemoteComponentsError";
64
32
  }
65
33
  };
@@ -184,6 +152,38 @@ var init_next_client_pages_loader = __esm({
184
152
  }
185
153
  });
186
154
 
155
+ // src/shared/webpack/shared-modules.ts
156
+ function applySharedModules(bundle, resolve) {
157
+ const self = globalThis;
158
+ if (self.__remote_webpack_require__?.[bundle]) {
159
+ const modulePaths = Object.keys(
160
+ self.__remote_webpack_module_map__?.[bundle] ?? self.__remote_webpack_require__[bundle].m ?? {}
161
+ );
162
+ for (const [key, value] of Object.entries(resolve)) {
163
+ let ids = modulePaths.filter((p) => p === key);
164
+ if (ids.length === 0) {
165
+ ids = modulePaths.filter((p) => p.includes(key));
166
+ }
167
+ for (let id of ids) {
168
+ const webpackBundle = self.__remote_webpack_require__[bundle];
169
+ if (webpackBundle.m) {
170
+ if (self.__remote_webpack_module_map__?.[bundle]?.[id]) {
171
+ id = `${self.__remote_webpack_module_map__[bundle][id]}`;
172
+ }
173
+ webpackBundle.m[id] = (module) => {
174
+ module.exports = value;
175
+ };
176
+ }
177
+ }
178
+ }
179
+ }
180
+ }
181
+ var init_shared_modules = __esm({
182
+ "src/shared/webpack/shared-modules.ts"() {
183
+ "use strict";
184
+ }
185
+ });
186
+
187
187
  // src/shared/utils/index.ts
188
188
  function escapeString(str) {
189
189
  return str.replace(/[^a-z0-9]/g, "_");
@@ -372,7 +372,10 @@ async function handleTurbopackChunk(code, bundle, url) {
372
372
  function createModuleRequire(runtime) {
373
373
  return (id) => {
374
374
  const self = globalThis;
375
- const { bundle, id: moduleId } = id.match(REMOTE_COMPONENT_REGEX)?.groups ?? { bundle: "default", id };
375
+ const { bundle, id: moduleId } = id.match(REMOTE_COMPONENT_REGEX)?.groups ?? {
376
+ bundle: "default",
377
+ id
378
+ };
376
379
  const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : runtime;
377
380
  try {
378
381
  if (remoteRuntime === RUNTIME_WEBPACK && bundle && moduleId) {
@@ -715,6 +718,106 @@ var init_webpack_adapter = __esm({
715
718
  }
716
719
  });
717
720
 
721
+ // src/shared/client/static-loader.ts
722
+ async function loadStaticRemoteComponent(scripts, url) {
723
+ const self = globalThis;
724
+ if (self.__remote_script_entrypoint_mount__?.[url.href]) {
725
+ self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
726
+ }
727
+ if (self.__remote_script_entrypoint_unmount__?.[url.href]) {
728
+ self.__remote_script_entrypoint_unmount__[url.href] = /* @__PURE__ */ new Set();
729
+ }
730
+ const mountUnmountSets = await Promise.all(
731
+ scripts.map(async (script) => {
732
+ try {
733
+ let src = typeof script.getAttribute === "function" ? script.getAttribute("src") ?? script.src : script.src;
734
+ if (!src && script.textContent) {
735
+ const blob = new Blob(
736
+ [
737
+ script.textContent.replace(
738
+ /import\.meta\.url/g,
739
+ JSON.stringify(url)
740
+ )
741
+ ],
742
+ {
743
+ type: "text/javascript"
744
+ }
745
+ );
746
+ src = URL.createObjectURL(blob);
747
+ }
748
+ const mod = await import(
749
+ /* @vite-ignore */
750
+ /* webpackIgnore: true */
751
+ new URL(src, url).href
752
+ );
753
+ if (src.startsWith("blob:")) {
754
+ URL.revokeObjectURL(src);
755
+ }
756
+ if (typeof mod.mount === "function" || typeof mod.default?.mount === "function") {
757
+ if (!self.__remote_script_entrypoint_mount__) {
758
+ self.__remote_script_entrypoint_mount__ = {};
759
+ }
760
+ if (!self.__remote_script_entrypoint_mount__[url.href]) {
761
+ self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
762
+ }
763
+ self.__remote_script_entrypoint_mount__[url.href]?.add(
764
+ mod.mount || mod.default?.mount || (() => {
765
+ })
766
+ );
767
+ }
768
+ if (typeof mod.unmount === "function" || typeof mod.default?.unmount === "function") {
769
+ if (!self.__remote_script_entrypoint_unmount__) {
770
+ self.__remote_script_entrypoint_unmount__ = {};
771
+ }
772
+ if (!self.__remote_script_entrypoint_unmount__[url.href]) {
773
+ self.__remote_script_entrypoint_unmount__[url.href] = /* @__PURE__ */ new Set();
774
+ }
775
+ self.__remote_script_entrypoint_unmount__[url.href]?.add(
776
+ mod.unmount || mod.default?.unmount || (() => {
777
+ })
778
+ );
779
+ }
780
+ return {
781
+ mount: mod.mount || mod.default?.mount,
782
+ unmount: mod.unmount || mod.default?.unmount
783
+ };
784
+ } catch (e) {
785
+ console.error(
786
+ new RemoteComponentsError(
787
+ `Error loading remote component script from "${script.src || url.href}".`,
788
+ { cause: e }
789
+ )
790
+ );
791
+ return {
792
+ mount: void 0,
793
+ unmount: void 0
794
+ };
795
+ }
796
+ })
797
+ );
798
+ return mountUnmountSets.reduce(
799
+ (acc, { mount, unmount }) => {
800
+ if (typeof mount === "function") {
801
+ acc.mount.add(mount);
802
+ }
803
+ if (typeof unmount === "function") {
804
+ acc.unmount.add(unmount);
805
+ }
806
+ return acc;
807
+ },
808
+ {
809
+ mount: /* @__PURE__ */ new Set(),
810
+ unmount: /* @__PURE__ */ new Set()
811
+ }
812
+ );
813
+ }
814
+ var init_static_loader = __esm({
815
+ "src/shared/client/static-loader.ts"() {
816
+ "use strict";
817
+ init_error();
818
+ }
819
+ });
820
+
718
821
  // src/shared/client/polyfill.tsx
719
822
  import { jsx } from "react/jsx-runtime";
720
823
  function applyBundleUrlToSrc(bundle, src) {
@@ -1024,10 +1127,10 @@ async function webpackRuntime(bundle, shared, remoteShared) {
1024
1127
  var init_webpack = __esm({
1025
1128
  "src/html/host/runtime/webpack.ts"() {
1026
1129
  "use strict";
1130
+ init_polyfill();
1027
1131
  init_error();
1028
- init_shared_modules();
1029
1132
  init_next_client_pages_loader();
1030
- init_polyfill();
1133
+ init_shared_modules();
1031
1134
  }
1032
1135
  });
1033
1136
 
@@ -1078,10 +1181,35 @@ async function turbopackRuntime(url, bundle, shared, remoteShared) {
1078
1181
  var init_turbopack = __esm({
1079
1182
  "src/html/host/runtime/turbopack.ts"() {
1080
1183
  "use strict";
1081
- init_shared_modules();
1082
- init_next_client_pages_loader();
1083
- init_webpack_adapter();
1084
1184
  init_polyfill();
1185
+ init_webpack_adapter();
1186
+ init_next_client_pages_loader();
1187
+ init_shared_modules();
1188
+ }
1189
+ });
1190
+
1191
+ // src/html/host/runtime/script.ts
1192
+ var script_exports = {};
1193
+ __export(script_exports, {
1194
+ scriptRuntime: () => scriptRuntime
1195
+ });
1196
+ function scriptRuntime() {
1197
+ const self = globalThis;
1198
+ return {
1199
+ self,
1200
+ createFromReadableStream: () => Promise.resolve(null),
1201
+ applySharedModules: () => Promise.resolve(),
1202
+ nextClientPagesLoader: () => ({
1203
+ Component: null,
1204
+ App: null
1205
+ }),
1206
+ preloadScripts: loadStaticRemoteComponent
1207
+ };
1208
+ }
1209
+ var init_script = __esm({
1210
+ "src/html/host/runtime/script.ts"() {
1211
+ "use strict";
1212
+ init_static_loader();
1085
1213
  }
1086
1214
  });
1087
1215
 
@@ -1089,19 +1217,75 @@ var init_turbopack = __esm({
1089
1217
  import { startTransition, useLayoutEffect } from "react";
1090
1218
  import { hydrateRoot } from "react-dom/client";
1091
1219
 
1220
+ // src/shared/client/apply-origin.ts
1221
+ var tagNames = [
1222
+ "img",
1223
+ "source",
1224
+ "video",
1225
+ "audio",
1226
+ "track",
1227
+ "iframe",
1228
+ "embed",
1229
+ "script",
1230
+ "link"
1231
+ ];
1232
+ function applyOriginToNodes(doc, url) {
1233
+ if (url.origin !== location.origin) {
1234
+ const nodes = doc.querySelectorAll(
1235
+ tagNames.map(
1236
+ (type) => `${type}[src],${type}[srcset],${type}[href],${type}[imagesrcset]`
1237
+ ).join(",")
1238
+ );
1239
+ nodes.forEach((node) => {
1240
+ if (node.hasAttribute("src") && /^[./]+\/?/.test(node.getAttribute("src") ?? "")) {
1241
+ node.src = new URL(node.getAttribute("src") ?? "/", url).href;
1242
+ }
1243
+ if (node.hasAttribute("href") && /^[./]+\/?/.test(node.getAttribute("href") ?? "")) {
1244
+ node.setAttribute(
1245
+ "href",
1246
+ new URL(node.getAttribute("href") ?? "/", url).href
1247
+ );
1248
+ }
1249
+ if (node.hasAttribute("srcset")) {
1250
+ const srcSet = node.getAttribute("srcset")?.split(",").map((entry) => {
1251
+ const [urlPart, descriptor] = entry.trim().split(/\s+/);
1252
+ if (!urlPart)
1253
+ return entry;
1254
+ const absoluteUrl = new URL(urlPart, url).href;
1255
+ return descriptor ? `${absoluteUrl} ${descriptor}` : absoluteUrl;
1256
+ }).join(", ");
1257
+ if (srcSet) {
1258
+ node.setAttribute("srcset", srcSet);
1259
+ }
1260
+ }
1261
+ if (node.hasAttribute("imagesrcset")) {
1262
+ const srcSet = node.getAttribute("imagesrcset")?.split(",").map((entry) => {
1263
+ const [urlPart, descriptor] = entry.trim().split(/\s+/);
1264
+ if (!urlPart)
1265
+ return entry;
1266
+ const absoluteUrl = new URL(urlPart, url).href;
1267
+ return descriptor ? `${absoluteUrl} ${descriptor}` : absoluteUrl;
1268
+ }).join(", ");
1269
+ if (srcSet) {
1270
+ node.setAttribute("imagesrcset", srcSet);
1271
+ }
1272
+ }
1273
+ });
1274
+ }
1275
+ }
1276
+
1277
+ // src/shared/client/remote-component.ts
1278
+ init_error();
1279
+
1092
1280
  // src/shared/client/component-loader.ts
1093
- init_shared_modules();
1094
- init_next_client_pages_loader();
1095
1281
  init_error();
1096
- init_webpack_adapter();
1282
+ init_next_client_pages_loader();
1283
+ init_shared_modules();
1097
1284
  import * as React from "react";
1098
- import * as ReactDOM from "react-dom";
1099
- import * as ReactDOMClient from "react-dom/client";
1100
1285
  import * as JSXDevRuntime from "react/jsx-dev-runtime";
1101
1286
  import * as JSXRuntime from "react/jsx-runtime";
1102
-
1103
- // src/shared/client/script-loader.ts
1104
- init_error();
1287
+ import * as ReactDOM from "react-dom";
1288
+ import * as ReactDOMClient from "react-dom/client";
1105
1289
 
1106
1290
  // src/shared/client/rsc.ts
1107
1291
  import { ReadableStream } from "web-streams-polyfill";
@@ -1169,13 +1353,19 @@ function createRSCStream(rscName, data) {
1169
1353
  });
1170
1354
  }
1171
1355
 
1172
- // src/shared/client/remote-component.ts
1356
+ // src/shared/client/script-loader.ts
1357
+ init_error();
1358
+
1359
+ // src/shared/client/component-loader.ts
1173
1360
  init_webpack_adapter();
1361
+
1362
+ // src/shared/client/remote-component.ts
1174
1363
  init_const();
1175
- init_error();
1364
+ init_static_loader();
1365
+ init_webpack_adapter();
1176
1366
 
1177
1367
  // src/html/host/index.tsx
1178
- init_utils();
1368
+ init_error();
1179
1369
 
1180
1370
  // src/shared/ssr/fetch-headers.ts
1181
1371
  function remoteFetchHeaders(additionalHeaders) {
@@ -1195,65 +1385,8 @@ function remoteFetchHeaders(additionalHeaders) {
1195
1385
  };
1196
1386
  }
1197
1387
 
1198
- // src/shared/client/apply-origin.ts
1199
- var tagNames = [
1200
- "img",
1201
- "source",
1202
- "video",
1203
- "audio",
1204
- "track",
1205
- "iframe",
1206
- "embed",
1207
- "script",
1208
- "link"
1209
- ];
1210
- function applyOriginToNodes(doc, url) {
1211
- if (url.origin !== location.origin) {
1212
- const nodes = doc.querySelectorAll(
1213
- tagNames.map(
1214
- (type) => `${type}[src],${type}[srcset],${type}[href],${type}[imagesrcset]`
1215
- ).join(",")
1216
- );
1217
- nodes.forEach((node) => {
1218
- if (node.hasAttribute("src") && /^[./]+\/?/.test(node.getAttribute("src") ?? "")) {
1219
- node.src = new URL(node.getAttribute("src") ?? "/", url).href;
1220
- }
1221
- if (node.hasAttribute("href") && /^[./]+\/?/.test(node.getAttribute("href") ?? "")) {
1222
- node.setAttribute(
1223
- "href",
1224
- new URL(node.getAttribute("href") ?? "/", url).href
1225
- );
1226
- }
1227
- if (node.hasAttribute("srcset")) {
1228
- const srcSet = node.getAttribute("srcset")?.split(",").map((entry) => {
1229
- const [urlPart, descriptor] = entry.trim().split(/\s+/);
1230
- if (!urlPart)
1231
- return entry;
1232
- const absoluteUrl = new URL(urlPart, url).href;
1233
- return descriptor ? `${absoluteUrl} ${descriptor}` : absoluteUrl;
1234
- }).join(", ");
1235
- if (srcSet) {
1236
- node.setAttribute("srcset", srcSet);
1237
- }
1238
- }
1239
- if (node.hasAttribute("imagesrcset")) {
1240
- const srcSet = node.getAttribute("imagesrcset")?.split(",").map((entry) => {
1241
- const [urlPart, descriptor] = entry.trim().split(/\s+/);
1242
- if (!urlPart)
1243
- return entry;
1244
- const absoluteUrl = new URL(urlPart, url).href;
1245
- return descriptor ? `${absoluteUrl} ${descriptor}` : absoluteUrl;
1246
- }).join(", ");
1247
- if (srcSet) {
1248
- node.setAttribute("imagesrcset", srcSet);
1249
- }
1250
- }
1251
- });
1252
- }
1253
- }
1254
-
1255
1388
  // src/html/host/index.tsx
1256
- init_error();
1389
+ init_utils();
1257
1390
 
1258
1391
  // src/html/host/runtime/index.ts
1259
1392
  init_error();
@@ -1269,9 +1402,12 @@ async function getRuntime(type, url, bundle, shared, remoteShared) {
1269
1402
  } else if (type === "turbopack") {
1270
1403
  const { turbopackRuntime: turbopackRuntime2 } = await Promise.resolve().then(() => (init_turbopack(), turbopack_exports));
1271
1404
  return turbopackRuntime2(url, bundle, shared, remoteShared);
1405
+ } else if (type === "script") {
1406
+ const { scriptRuntime: scriptRuntime2 } = await Promise.resolve().then(() => (init_script(), script_exports));
1407
+ return scriptRuntime2();
1272
1408
  }
1273
1409
  throw new RemoteComponentsError(
1274
- `Remote Components runtime "${type}" is not supported. Supported runtimes are "webpack" and "turbopack".`
1410
+ `Remote Components runtime "${type}" is not supported. Supported runtimes are "webpack", "turbopack", and "script".`
1275
1411
  );
1276
1412
  }
1277
1413
 
@@ -1279,13 +1415,17 @@ async function getRuntime(type, url, bundle, shared, remoteShared) {
1279
1415
  import { jsx as jsx2 } from "react/jsx-runtime";
1280
1416
  if (typeof HTMLElement !== "undefined") {
1281
1417
  class RemoteComponent extends HTMLElement {
1282
- constructor() {
1283
- super(...arguments);
1284
- this.__next = null;
1285
- this.fouc = null;
1286
- this.isLoading = false;
1287
- this.root = null;
1288
- }
1418
+ name;
1419
+ bundle;
1420
+ fallbackSlot;
1421
+ __next = null;
1422
+ fouc = null;
1423
+ isLoading = false;
1424
+ prevIsRemoteComponent = false;
1425
+ prevUrl;
1426
+ root = null;
1427
+ reactRoot;
1428
+ additionalHeaders;
1289
1429
  static get observedAttributes() {
1290
1430
  return ["src", "name", "mode"];
1291
1431
  }
@@ -1397,23 +1537,27 @@ if (typeof HTMLElement !== "undefined") {
1397
1537
  }
1398
1538
  const parser = new DOMParser();
1399
1539
  const doc = parser.parseFromString(html, "text/html");
1400
- if (doc.querySelectorAll("div[data-bundle][data-route]").length > 1 && !doc.querySelector(`div[data-bundle][data-route][id^="${this.name}"]`)) {
1540
+ if (doc.querySelectorAll("div[data-bundle][data-route]").length > 1 && !doc.querySelector(
1541
+ `div[data-bundle][data-route][id^="${this.name}"]`
1542
+ ) || doc.querySelectorAll("remote-component").length > 1 && !doc.querySelector(`remote-component[name="${this.name}"]`)) {
1401
1543
  throw multipleRemoteComponentsError(
1402
1544
  url?.href ?? (this.getAttribute("src") || "unknown")
1403
1545
  );
1404
1546
  }
1405
1547
  const component = doc.querySelector(`div[data-bundle][data-route][id^="${this.name}"]`) ?? // fallback to the first element with the data-bundle and data-route attributes when not using a named remote component
1406
1548
  doc.querySelector("div[data-bundle][data-route]") ?? // fallback to Next.js Pages Router
1407
- doc.querySelector("div#__next");
1549
+ doc.querySelector("div#__next") ?? // fallback to a <remote-component> element
1550
+ doc.querySelector(`remote-component[name="${this.name}"]:not([src])`) ?? doc.querySelector("remote-component:not([src])");
1408
1551
  const nextData = JSON.parse(
1409
1552
  (doc.querySelector("#__NEXT_DATA__") ?? doc.querySelector("#__REMOTE_NEXT_DATA__"))?.textContent ?? "null"
1410
1553
  );
1554
+ const isRemoteComponent = component?.tagName.toLowerCase() === "remote-component";
1411
1555
  if (nextData && nextData.buildId === "development" && !this.reactRoot) {
1412
1556
  this.fouc = document.createElement("style");
1413
1557
  this.fouc.textContent = `:host { display: none; }`;
1414
1558
  this.root.appendChild(this.fouc);
1415
1559
  }
1416
- this.name = component?.getAttribute("id")?.replace(/_ssr$/, "") || (nextData ? "__next" : this.name);
1560
+ this.name = component?.getAttribute("id")?.replace(/_ssr$/, "") || isRemoteComponent && component.getAttribute("name") || (nextData ? "__next" : this.name);
1417
1561
  const rsc = doc.querySelector(`#${this.name}_rsc`);
1418
1562
  this.bundle = component?.getAttribute("data-bundle") || nextData?.props.__REMOTE_COMPONENT__?.bundle || this.bundle;
1419
1563
  if (url) {
@@ -1426,12 +1570,13 @@ if (typeof HTMLElement !== "undefined") {
1426
1570
  const metadata = document.createElement("script");
1427
1571
  metadata.type = "application/json";
1428
1572
  metadata.setAttribute("data-remote-component", "");
1429
- metadata.textContent = JSON.stringify({
1573
+ const metadataObj = {
1430
1574
  name: this.name,
1431
1575
  bundle: this.bundle,
1432
- route: component?.getAttribute("data-route") ?? nextData?.page ?? "/",
1433
- runtime: component?.getAttribute("data-runtime") ?? nextData?.props.__REMOTE_COMPONENT__?.runtime
1434
- });
1576
+ route: component?.getAttribute("data-route") ?? nextData?.page ?? url?.pathname ?? "/",
1577
+ runtime: component?.getAttribute("data-runtime") ?? nextData?.props.__REMOTE_COMPONENT__?.runtime ?? "script"
1578
+ };
1579
+ metadata.textContent = JSON.stringify(metadataObj);
1435
1580
  if (this.previousElementSibling?.getAttribute("data-remote-component") !== null) {
1436
1581
  this.previousElementSibling?.remove();
1437
1582
  }
@@ -1446,11 +1591,38 @@ if (typeof HTMLElement !== "undefined") {
1446
1591
  remoteShared.__remote_components_missing_shared__
1447
1592
  );
1448
1593
  }
1449
- if (!component || !(rsc || nextData)) {
1594
+ if (!component || !(rsc || nextData || isRemoteComponent)) {
1450
1595
  throw new RemoteComponentsError(
1451
1596
  `Remote Component not found on ${src}.${this.name !== "__vercel_remote_component" ? ` The name for the <RemoteComponent> is "${this.name}". Check <RemoteComponent> usage.` : ""} Did you forget to wrap the content in <RemoteComponent>?`
1452
1597
  );
1453
1598
  }
1599
+ if (this.prevIsRemoteComponent) {
1600
+ if (this.prevUrl) {
1601
+ const prevUrl = this.prevUrl;
1602
+ const self2 = globalThis;
1603
+ if (self2.__remote_script_entrypoint_unmount__?.[prevUrl.href]) {
1604
+ await Promise.all(
1605
+ Array.from(
1606
+ self2.__remote_script_entrypoint_unmount__[prevUrl.href] ?? []
1607
+ ).map(async (unmount) => {
1608
+ try {
1609
+ await unmount(this.root);
1610
+ } catch (e) {
1611
+ console.error(
1612
+ new RemoteComponentsError(
1613
+ `Error while calling unmount() for Remote Component from ${prevUrl.href}.`,
1614
+ { cause: e }
1615
+ )
1616
+ );
1617
+ }
1618
+ })
1619
+ );
1620
+ }
1621
+ }
1622
+ this.root.innerHTML = "";
1623
+ }
1624
+ this.prevUrl = url ?? new URL(location.href);
1625
+ this.prevIsRemoteComponent = isRemoteComponent;
1454
1626
  const removable = Array.from(this.childNodes);
1455
1627
  const links = doc.querySelectorAll("link[href]");
1456
1628
  const remoteComponentSrc = this.getAttribute("src");
@@ -1516,7 +1688,35 @@ if (typeof HTMLElement !== "undefined") {
1516
1688
  applyOriginToNodes(doc, url ?? new URL(location.href));
1517
1689
  if (!this.reactRoot) {
1518
1690
  Array.from(component.children).forEach((el) => {
1519
- this.root?.appendChild(el);
1691
+ if (!isRemoteComponent && el.tagName.toLowerCase() === "script") {
1692
+ const newScript = document.createElement("script");
1693
+ for (const attr of el.attributes) {
1694
+ if (attr.name === "src") {
1695
+ newScript.setAttribute(
1696
+ attr.name,
1697
+ new URL(attr.value, url ?? location.origin).href
1698
+ );
1699
+ } else {
1700
+ newScript.setAttribute(attr.name, attr.value);
1701
+ }
1702
+ }
1703
+ newScript.textContent = el.textContent;
1704
+ if (remoteComponentSrc) {
1705
+ newScript.setAttribute(
1706
+ "data-remote-component-src",
1707
+ remoteComponentSrc
1708
+ );
1709
+ }
1710
+ this.root?.appendChild(newScript);
1711
+ } else {
1712
+ const newEl = el.cloneNode(true);
1713
+ for (const attr of el.attributes) {
1714
+ if (attr.name.startsWith("on")) {
1715
+ newEl.setAttribute(attr.name, attr.value);
1716
+ }
1717
+ }
1718
+ this.root?.appendChild(newEl);
1719
+ }
1520
1720
  });
1521
1721
  }
1522
1722
  for (const el of removable) {
@@ -1555,7 +1755,7 @@ if (typeof HTMLElement !== "undefined") {
1555
1755
  nextClientPagesLoader: nextClientPagesLoader2,
1556
1756
  preloadScripts
1557
1757
  } = await getRuntime(
1558
- component.getAttribute("data-runtime") ?? "webpack",
1758
+ metadataObj.runtime,
1559
1759
  url ?? new URL(location.href),
1560
1760
  this.bundle,
1561
1761
  {
@@ -1567,8 +1767,8 @@ if (typeof HTMLElement !== "undefined") {
1567
1767
  },
1568
1768
  remoteShared
1569
1769
  );
1570
- const scripts = doc.querySelectorAll(
1571
- "script[src],script[data-src]"
1770
+ const scripts = isRemoteComponent ? component.querySelectorAll("script") : doc.querySelectorAll(
1771
+ "script[src],script[data-src],script[data-remote-component-entrypoint]"
1572
1772
  );
1573
1773
  if (!url) {
1574
1774
  url = new URL(
@@ -1582,6 +1782,13 @@ if (typeof HTMLElement !== "undefined") {
1582
1782
  this.bundle ?? "default",
1583
1783
  this.name ?? "__vercel_remote_component"
1584
1784
  );
1785
+ if (isRemoteComponent) {
1786
+ Array.from(component.children).forEach((child) => {
1787
+ if (child.tagName === "SCRIPT") {
1788
+ child.remove();
1789
+ }
1790
+ });
1791
+ }
1585
1792
  const doCleanup = () => {
1586
1793
  if (this.root && remoteComponentSrc) {
1587
1794
  const selector = `[data-remote-component-src]:not([data-remote-component-src="${remoteComponentSrc}"])`;
@@ -1681,7 +1888,7 @@ if (typeof HTMLElement !== "undefined") {
1681
1888
  });
1682
1889
  }
1683
1890
  remoteComponent.isLoading = false;
1684
- }, [initial]);
1891
+ }, [initial, remoteComponent]);
1685
1892
  return NextApp ? /* @__PURE__ */ jsx2(NextApp, { Component: NextComponent, ...nextData.props }) : /* @__PURE__ */ jsx2(NextComponent, { ...nextData.props });
1686
1893
  })(App, Component, this);
1687
1894
  if (this.reactRoot) {
@@ -1703,6 +1910,23 @@ if (typeof HTMLElement !== "undefined") {
1703
1910
  if (this.fouc) {
1704
1911
  this.root.removeChild(this.fouc);
1705
1912
  }
1913
+ } else if (self.__remote_script_entrypoint_mount__?.[url.href]) {
1914
+ await Promise.all(
1915
+ Array.from(
1916
+ self.__remote_script_entrypoint_mount__[url.href] ?? []
1917
+ ).map(async (mount) => {
1918
+ try {
1919
+ await mount(this.root);
1920
+ } catch (e) {
1921
+ console.error(
1922
+ new RemoteComponentsError(
1923
+ `Error while calling mount() for Remote Component from ${url.href}.`,
1924
+ { cause: e }
1925
+ )
1926
+ );
1927
+ }
1928
+ })
1929
+ );
1706
1930
  }
1707
1931
  this.isLoading = false;
1708
1932
  }