remote-components 0.0.32 → 0.0.33

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 (100) hide show
  1. package/dist/{component-loader-e5513139.d.ts → component-loader-28ad0083.d.ts} +1 -0
  2. package/dist/html/host.cjs +105 -28
  3. package/dist/html/host.cjs.map +1 -1
  4. package/dist/html/host.js +105 -28
  5. package/dist/html/host.js.map +1 -1
  6. package/dist/internal/next/host/app-router-client.cjs +6 -2
  7. package/dist/internal/next/host/app-router-client.cjs.map +1 -1
  8. package/dist/internal/next/host/app-router-client.d.ts +2 -2
  9. package/dist/internal/next/host/app-router-client.js +6 -2
  10. package/dist/internal/next/host/app-router-client.js.map +1 -1
  11. package/dist/internal/next/remote/render-client.cjs +13 -3
  12. package/dist/internal/next/remote/render-client.cjs.map +1 -1
  13. package/dist/internal/next/remote/render-client.js +13 -3
  14. package/dist/internal/next/remote/render-client.js.map +1 -1
  15. package/dist/internal/next/remote/render-server.cjs +22 -3
  16. package/dist/internal/next/remote/render-server.cjs.map +1 -1
  17. package/dist/internal/next/remote/render-server.js +23 -4
  18. package/dist/internal/next/remote/render-server.js.map +1 -1
  19. package/dist/internal/shared/client/remote-component.cjs +35 -17
  20. package/dist/internal/shared/client/remote-component.cjs.map +1 -1
  21. package/dist/internal/shared/client/remote-component.d.ts +9 -2
  22. package/dist/internal/shared/client/remote-component.js +34 -17
  23. package/dist/internal/shared/client/remote-component.js.map +1 -1
  24. package/dist/internal/shared/error.cjs +50 -0
  25. package/dist/internal/shared/error.cjs.map +1 -0
  26. package/dist/internal/shared/error.d.ts +10 -0
  27. package/dist/internal/shared/error.js +24 -0
  28. package/dist/internal/shared/error.js.map +1 -0
  29. package/dist/internal/shared/ssr/dom-flight.cjs +8 -1
  30. package/dist/internal/shared/ssr/dom-flight.cjs.map +1 -1
  31. package/dist/internal/shared/ssr/dom-flight.d.ts +2 -1
  32. package/dist/internal/shared/ssr/dom-flight.js +8 -1
  33. package/dist/internal/shared/ssr/dom-flight.js.map +1 -1
  34. package/dist/internal/shared/ssr/fetch-remote-component.cjs +48 -5
  35. package/dist/internal/shared/ssr/fetch-remote-component.cjs.map +1 -1
  36. package/dist/internal/shared/ssr/fetch-remote-component.d.ts +1 -1
  37. package/dist/internal/shared/ssr/fetch-remote-component.js +52 -5
  38. package/dist/internal/shared/ssr/fetch-remote-component.js.map +1 -1
  39. package/dist/internal/webpack/next-client-pages-loader.cjs +3 -2
  40. package/dist/internal/webpack/next-client-pages-loader.cjs.map +1 -1
  41. package/dist/internal/webpack/next-client-pages-loader.js +3 -2
  42. package/dist/internal/webpack/next-client-pages-loader.js.map +1 -1
  43. package/dist/next/config.cjs +26 -9
  44. package/dist/next/config.cjs.map +1 -1
  45. package/dist/next/config.js +26 -9
  46. package/dist/next/config.js.map +1 -1
  47. package/dist/next/host/client/index.cjs +86 -29
  48. package/dist/next/host/client/index.cjs.map +1 -1
  49. package/dist/next/host/client/index.d.ts +1 -1
  50. package/dist/next/host/client/index.js +86 -29
  51. package/dist/next/host/client/index.js.map +1 -1
  52. package/dist/next/host/pages-router-client.cjs +1 -1
  53. package/dist/next/host/pages-router-client.cjs.map +1 -1
  54. package/dist/next/host/pages-router-client.js +1 -1
  55. package/dist/next/host/pages-router-client.js.map +1 -1
  56. package/dist/next/host/pages-router-server.cjs +4 -3
  57. package/dist/next/host/pages-router-server.cjs.map +1 -1
  58. package/dist/next/host/pages-router-server.js +4 -3
  59. package/dist/next/host/pages-router-server.js.map +1 -1
  60. package/dist/next/index.cjs +4 -2
  61. package/dist/next/index.cjs.map +1 -1
  62. package/dist/next/index.js +4 -2
  63. package/dist/next/index.js.map +1 -1
  64. package/dist/next/remote/pages-router.cjs +1 -1
  65. package/dist/next/remote/pages-router.cjs.map +1 -1
  66. package/dist/next/remote/pages-router.js +1 -1
  67. package/dist/next/remote/pages-router.js.map +1 -1
  68. package/dist/react/index.cjs +42 -11
  69. package/dist/react/index.cjs.map +1 -1
  70. package/dist/react/index.d.ts +2 -2
  71. package/dist/react/index.js +46 -11
  72. package/dist/react/index.js.map +1 -1
  73. package/dist/shared/host/app.cjs +36 -0
  74. package/dist/shared/host/app.cjs.map +1 -0
  75. package/dist/shared/host/app.d.ts +3 -0
  76. package/dist/shared/host/app.js +12 -0
  77. package/dist/shared/host/app.js.map +1 -0
  78. package/dist/shared/host/pages.cjs +36 -0
  79. package/dist/shared/host/pages.cjs.map +1 -0
  80. package/dist/shared/host/pages.d.ts +3 -0
  81. package/dist/shared/host/pages.js +12 -0
  82. package/dist/shared/host/pages.js.map +1 -0
  83. package/dist/shared/remote/app.cjs +32 -0
  84. package/dist/shared/remote/app.cjs.map +1 -0
  85. package/dist/shared/remote/app.d.ts +3 -0
  86. package/dist/shared/remote/app.js +8 -0
  87. package/dist/shared/remote/app.js.map +1 -0
  88. package/dist/shared/remote/pages.cjs +32 -0
  89. package/dist/shared/remote/pages.cjs.map +1 -0
  90. package/dist/shared/remote/pages.d.ts +3 -0
  91. package/dist/shared/remote/pages.js +8 -0
  92. package/dist/shared/remote/pages.js.map +1 -0
  93. package/dist/shared/remote/wrapper.cjs +48 -0
  94. package/dist/shared/remote/wrapper.cjs.map +1 -0
  95. package/dist/shared/remote/wrapper.d.ts +2 -0
  96. package/dist/shared/remote/wrapper.js +25 -0
  97. package/dist/shared/remote/wrapper.js.map +1 -0
  98. package/dist/{types-b8210fd3.d.ts → types-7c207455.d.ts} +1 -1
  99. package/dist/{types-280a3640.d.ts → types-e4a3fa37.d.ts} +1 -0
  100. package/package.json +37 -1
@@ -5,6 +5,7 @@ interface RemoteComponentMetadata {
5
5
  route: string;
6
6
  runtime: 'webpack' | 'turbopack';
7
7
  id: string;
8
+ type: 'nextjs' | 'unknown';
8
9
  }
9
10
 
10
11
  interface RemoteComponentProps {
@@ -62,6 +62,32 @@ var init_shared_modules = __esm({
62
62
  }
63
63
  });
64
64
 
65
+ // src/shared/error.ts
66
+ function multipleRemoteComponentsError(url) {
67
+ return new Error(
68
+ `Multiple Remote Components found at "${url}". When a page exposes multiple Remote Components you must specify the "name" prop to select which one to load.`
69
+ );
70
+ }
71
+ function failedToFetchRemoteComponentError(url, error) {
72
+ return new RemoteComponentsError(
73
+ `Failed to fetch Remote Component from "${url}". Is the URL correct and accessible?`,
74
+ { cause: error instanceof Error ? error : new Error(String(error)) }
75
+ );
76
+ }
77
+ var RemoteComponentsError;
78
+ var init_error = __esm({
79
+ "src/shared/error.ts"() {
80
+ "use strict";
81
+ RemoteComponentsError = class extends Error {
82
+ constructor(message, options) {
83
+ super(message, options);
84
+ this.code = "REMOTE_COMPONENTS_ERROR";
85
+ this.name = "RemoteComponentsError";
86
+ }
87
+ };
88
+ }
89
+ });
90
+
65
91
  // src/shared/webpack/next-client-pages-loader.ts
66
92
  function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
67
93
  const self = globalThis;
@@ -100,8 +126,8 @@ function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
100
126
  (key) => key.includes("/next/dist/client/page-loader.js")
101
127
  ) ?? ""] ?? -1;
102
128
  if (!(componentLoaderChunk && appLoaderChunk)) {
103
- throw new Error(
104
- `Next.js client pages loader not found in bundle "${bundle}"`
129
+ throw new RemoteComponentsError(
130
+ `Next.js client pages loader not found in bundle "${bundle}".`
105
131
  );
106
132
  }
107
133
  const __NEXT_P_ORIGINAL = self.__NEXT_P;
@@ -176,6 +202,7 @@ function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
176
202
  var init_next_client_pages_loader = __esm({
177
203
  "src/shared/webpack/next-client-pages-loader.ts"() {
178
204
  "use strict";
205
+ init_error();
179
206
  }
180
207
  });
181
208
 
@@ -334,11 +361,11 @@ async function handleTurbopackChunk(code, bundle, url) {
334
361
  scriptResolve(void 0);
335
362
  script.remove();
336
363
  };
337
- script.onerror = (error) => {
364
+ script.onerror = () => {
338
365
  URL.revokeObjectURL(scriptUrl);
339
366
  scriptReject(
340
- new Error(
341
- `Failed to load script: ${error instanceof Error ? error.message : String(error)}`
367
+ new RemoteComponentsError(
368
+ `Failed to load <script src="${script.src}"> for Remote Component. Check the URL is correct.`
342
369
  )
343
370
  );
344
371
  script.remove();
@@ -380,11 +407,11 @@ function createModuleRequire(runtime) {
380
407
  if (bundle && moduleId) {
381
408
  return handleTurbopackModule(bundle, moduleId, id);
382
409
  }
383
- throw new Error(`Module ${id} not found`);
410
+ throw new Error(`Module "${id}" not found.`);
384
411
  } catch (requireError) {
385
412
  if (typeof self.__original_webpack_require__ !== "function") {
386
- throw new Error(
387
- `Module ${id} not found in remote component bundle ${bundle}`,
413
+ throw new RemoteComponentsError(
414
+ `Module "${id}" not found in remote component bundle "${bundle}".`,
388
415
  {
389
416
  cause: requireError instanceof Error ? requireError : void 0
390
417
  }
@@ -393,8 +420,8 @@ function createModuleRequire(runtime) {
393
420
  try {
394
421
  return self.__original_webpack_require__(id);
395
422
  } catch (originalError) {
396
- throw new Error(
397
- `Module ${id} not found in remote component bundle ${bundle}`,
423
+ throw new RemoteComponentsError(
424
+ `Module "${id}" not found in remote component bundle "${bundle}".`,
398
425
  { cause: originalError instanceof Error ? originalError : void 0 }
399
426
  );
400
427
  }
@@ -685,7 +712,9 @@ function handleTurbopackModule(bundle, moduleId, id) {
685
712
  return self.__webpack_chunk_load__?.(bundleUrl, bundle);
686
713
  }
687
714
  }
688
- throw new Error(`Failed to load chunk ${url} for module ${id}`);
715
+ throw new Error(
716
+ `Failed to load Turbopack chunk "${url}" for module "${id}". Check the URL is correct.`
717
+ );
689
718
  },
690
719
  // global
691
720
  g: self.__remote_components_turbopack_global__[bundle],
@@ -703,6 +732,7 @@ function handleTurbopackModule(bundle, moduleId, id) {
703
732
  var init_webpack_adapter = __esm({
704
733
  "src/shared/client/webpack-adapter.ts"() {
705
734
  "use strict";
735
+ init_error();
706
736
  init_const();
707
737
  }
708
738
  });
@@ -924,10 +954,14 @@ async function webpackRuntime(bundle, shared, remoteShared) {
924
954
  const remoteBundle = match?.groups?.bundle;
925
955
  const id = match?.groups?.id;
926
956
  if (!(id && remoteBundle)) {
927
- throw new Error(`Module not found: "${remoteId}"`);
957
+ throw new RemoteComponentsError(
958
+ `Remote Component module "${remoteId}" not found. Did you forget to wrap the Next.js config with \`withRemoteComponents\` on both host and remote?`
959
+ );
928
960
  }
929
961
  if (typeof self.__remote_webpack_require__?.[remoteBundle] !== "function") {
930
- throw new Error(`Remote component loading error "${remoteBundle}"`);
962
+ throw new RemoteComponentsError(
963
+ `Remote Components are not available in "${remoteBundle}". Did you forget to wrap the Next.js config with \`withRemoteComponents\` on both host and remote?`
964
+ );
931
965
  }
932
966
  return self.__remote_webpack_require__[remoteBundle](id);
933
967
  };
@@ -949,8 +983,8 @@ async function webpackRuntime(bundle, shared, remoteShared) {
949
983
  };
950
984
  newScript.onerror = () => {
951
985
  reject(
952
- new Error(
953
- `Failed to load script ${script.src} for remote component`
986
+ new RemoteComponentsError(
987
+ `Failed to load <script src="${script.src}"> for Remote Component. Check the URL is correct.`
954
988
  )
955
989
  );
956
990
  newScript.remove();
@@ -1010,6 +1044,7 @@ async function webpackRuntime(bundle, shared, remoteShared) {
1010
1044
  var init_webpack = __esm({
1011
1045
  "src/html/host/runtime/webpack.ts"() {
1012
1046
  "use strict";
1047
+ init_error();
1013
1048
  init_shared_modules();
1014
1049
  init_next_client_pages_loader();
1015
1050
  init_polyfill();
@@ -1087,8 +1122,12 @@ var JSXDevRuntime = __toESM(require("react/jsx-dev-runtime"), 1);
1087
1122
  var JSXRuntime = __toESM(require("react/jsx-runtime"), 1);
1088
1123
  init_shared_modules();
1089
1124
  init_next_client_pages_loader();
1125
+ init_error();
1090
1126
  init_webpack_adapter();
1091
1127
 
1128
+ // src/shared/client/script-loader.ts
1129
+ init_error();
1130
+
1092
1131
  // src/shared/client/rsc.ts
1093
1132
  var import_web_streams_polyfill = require("web-streams-polyfill");
1094
1133
  function fixPayload(payload) {
@@ -1158,6 +1197,7 @@ function createRSCStream(rscName, data) {
1158
1197
  // src/shared/client/remote-component.ts
1159
1198
  init_webpack_adapter();
1160
1199
  init_const();
1200
+ init_error();
1161
1201
 
1162
1202
  // src/html/host/index.tsx
1163
1203
  init_utils();
@@ -1237,7 +1277,11 @@ function applyOriginToNodes(doc, url) {
1237
1277
  }
1238
1278
  }
1239
1279
 
1280
+ // src/html/host/index.tsx
1281
+ init_error();
1282
+
1240
1283
  // src/html/host/runtime/index.ts
1284
+ init_error();
1241
1285
  async function getRuntime(type, url, bundle, shared, remoteShared) {
1242
1286
  if (typeof globalThis.process === "undefined") {
1243
1287
  globalThis.process = {
@@ -1251,7 +1295,9 @@ async function getRuntime(type, url, bundle, shared, remoteShared) {
1251
1295
  const { turbopackRuntime: turbopackRuntime2 } = await Promise.resolve().then(() => (init_turbopack(), turbopack_exports));
1252
1296
  return turbopackRuntime2(url, bundle, shared, remoteShared);
1253
1297
  }
1254
- throw new Error(`Runtime ${type} is not supported`);
1298
+ throw new RemoteComponentsError(
1299
+ `Remote Components runtime "${type}" is not supported. Supported runtimes are "webpack" and "turbopack".`
1300
+ );
1255
1301
  }
1256
1302
 
1257
1303
  // src/html/host/index.tsx
@@ -1276,9 +1322,6 @@ if (typeof HTMLElement !== "undefined") {
1276
1322
  if (this.getAttribute("src")) {
1277
1323
  this.load().catch((e) => {
1278
1324
  console.error(e);
1279
- throw new Error(
1280
- `Failed to load remote component: ${this.bundle ?? this.name ?? this.getAttribute("src")}`
1281
- );
1282
1325
  });
1283
1326
  }
1284
1327
  } else if (name === "mode" && oldValue !== newValue && this.root) {
@@ -1290,7 +1333,7 @@ if (typeof HTMLElement !== "undefined") {
1290
1333
  });
1291
1334
  this.root = newRoot;
1292
1335
  this.load().catch((e) => {
1293
- throw new Error(`Failed to load remote component: ${e}`);
1336
+ console.error(e);
1294
1337
  });
1295
1338
  }
1296
1339
  }
@@ -1324,14 +1367,14 @@ if (typeof HTMLElement !== "undefined") {
1324
1367
  }
1325
1368
  if (this.hasAttribute("src") || this.querySelector("div#__REMOTE_COMPONENT__") || this.hasAttribute("data-ssr")) {
1326
1369
  this.load().catch((e) => {
1327
- throw new Error(`Failed to load remote component: ${e}`);
1370
+ console.error(e);
1328
1371
  });
1329
1372
  }
1330
1373
  this.isLoading = true;
1331
1374
  const src = this.getAttribute("src");
1332
1375
  const remoteComponentChild = this.querySelector("div#__REMOTE_COMPONENT__") || this.querySelector("div[data-bundle][data-route]");
1333
1376
  if (!src && !remoteComponentChild) {
1334
- throw new Error("src attribute is required");
1377
+ throw new RemoteComponentsError('"src" attribute is required');
1335
1378
  }
1336
1379
  let url = null;
1337
1380
  let html = this.innerHTML;
@@ -1349,14 +1392,41 @@ if (typeof HTMLElement !== "undefined") {
1349
1392
  };
1350
1393
  const res = await fetch(url, fetchInit);
1351
1394
  if (!res.ok) {
1352
- throw new Error(
1353
- `Failed to fetch remote component "${this.name}": ${res.status}`
1354
- );
1395
+ let error = failedToFetchRemoteComponentError(url.href, {
1396
+ cause: new Error(`${res.status} ${res.statusText}`)
1397
+ });
1398
+ try {
1399
+ const body = await res.text();
1400
+ const parser2 = new DOMParser();
1401
+ const doc2 = parser2.parseFromString(body, "text/html");
1402
+ const errorTemplate = doc2.querySelector(
1403
+ "template[data-next-error-message]"
1404
+ );
1405
+ const errorMessage = errorTemplate?.getAttribute(
1406
+ "data-next-error-message"
1407
+ );
1408
+ const errorStack = errorTemplate?.getAttribute(
1409
+ "data-next-error-stack"
1410
+ );
1411
+ if (errorMessage) {
1412
+ error = new RemoteComponentsError(errorMessage);
1413
+ if (errorStack) {
1414
+ error.stack = errorStack;
1415
+ }
1416
+ }
1417
+ } catch {
1418
+ }
1419
+ throw error;
1355
1420
  }
1356
1421
  html = await res.text();
1357
1422
  }
1358
1423
  const parser = new DOMParser();
1359
1424
  const doc = parser.parseFromString(html, "text/html");
1425
+ if (doc.querySelectorAll("div[data-bundle][data-route]").length > 1 && !doc.querySelector(`div[data-bundle][data-route][id^="${this.name}"]`)) {
1426
+ throw multipleRemoteComponentsError(
1427
+ url?.href ?? (this.getAttribute("src") || "unknown")
1428
+ );
1429
+ }
1360
1430
  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
1361
1431
  doc.querySelector("div[data-bundle][data-route]") ?? // fallback to Next.js Pages Router
1362
1432
  doc.querySelector("div#__next");
@@ -1396,8 +1466,15 @@ if (typeof HTMLElement !== "undefined") {
1396
1466
  );
1397
1467
  const remoteShared = nextData?.props.__REMOTE_COMPONENT__?.shared ?? (JSON.parse(remoteSharedEl?.textContent ?? "{}") ?? {});
1398
1468
  remoteSharedEl?.parentElement?.removeChild(remoteSharedEl);
1469
+ if ("__remote_components_missing_shared__" in remoteShared) {
1470
+ throw new RemoteComponentsError(
1471
+ remoteShared.__remote_components_missing_shared__
1472
+ );
1473
+ }
1399
1474
  if (!component || !(rsc || nextData)) {
1400
- throw new Error(`Failed to find component with id "${this.name}"`);
1475
+ throw new RemoteComponentsError(
1476
+ `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>?`
1477
+ );
1401
1478
  }
1402
1479
  const removable = Array.from(this.childNodes);
1403
1480
  const links = doc.querySelectorAll("link[href]");
@@ -1415,8 +1492,8 @@ if (typeof HTMLElement !== "undefined") {
1415
1492
  };
1416
1493
  newLink.onerror = () => {
1417
1494
  reject(
1418
- new Error(
1419
- `Failed to load link ${link.href} for remote component`
1495
+ new RemoteComponentsError(
1496
+ `Failed to load <link href="${link.href}"> for Remote Component. Check the URL is correct.`
1420
1497
  )
1421
1498
  );
1422
1499
  };