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
package/dist/html/host.js CHANGED
@@ -40,6 +40,32 @@ var init_shared_modules = __esm({
40
40
  }
41
41
  });
42
42
 
43
+ // src/shared/error.ts
44
+ function multipleRemoteComponentsError(url) {
45
+ return new Error(
46
+ `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.`
47
+ );
48
+ }
49
+ function failedToFetchRemoteComponentError(url, error) {
50
+ return new RemoteComponentsError(
51
+ `Failed to fetch Remote Component from "${url}". Is the URL correct and accessible?`,
52
+ { cause: error instanceof Error ? error : new Error(String(error)) }
53
+ );
54
+ }
55
+ var RemoteComponentsError;
56
+ var init_error = __esm({
57
+ "src/shared/error.ts"() {
58
+ "use strict";
59
+ RemoteComponentsError = class extends Error {
60
+ constructor(message, options) {
61
+ super(message, options);
62
+ this.code = "REMOTE_COMPONENTS_ERROR";
63
+ this.name = "RemoteComponentsError";
64
+ }
65
+ };
66
+ }
67
+ });
68
+
43
69
  // src/shared/webpack/next-client-pages-loader.ts
44
70
  function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
45
71
  const self = globalThis;
@@ -78,8 +104,8 @@ function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
78
104
  (key) => key.includes("/next/dist/client/page-loader.js")
79
105
  ) ?? ""] ?? -1;
80
106
  if (!(componentLoaderChunk && appLoaderChunk)) {
81
- throw new Error(
82
- `Next.js client pages loader not found in bundle "${bundle}"`
107
+ throw new RemoteComponentsError(
108
+ `Next.js client pages loader not found in bundle "${bundle}".`
83
109
  );
84
110
  }
85
111
  const __NEXT_P_ORIGINAL = self.__NEXT_P;
@@ -154,6 +180,7 @@ function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
154
180
  var init_next_client_pages_loader = __esm({
155
181
  "src/shared/webpack/next-client-pages-loader.ts"() {
156
182
  "use strict";
183
+ init_error();
157
184
  }
158
185
  });
159
186
 
@@ -312,11 +339,11 @@ async function handleTurbopackChunk(code, bundle, url) {
312
339
  scriptResolve(void 0);
313
340
  script.remove();
314
341
  };
315
- script.onerror = (error) => {
342
+ script.onerror = () => {
316
343
  URL.revokeObjectURL(scriptUrl);
317
344
  scriptReject(
318
- new Error(
319
- `Failed to load script: ${error instanceof Error ? error.message : String(error)}`
345
+ new RemoteComponentsError(
346
+ `Failed to load <script src="${script.src}"> for Remote Component. Check the URL is correct.`
320
347
  )
321
348
  );
322
349
  script.remove();
@@ -358,11 +385,11 @@ function createModuleRequire(runtime) {
358
385
  if (bundle && moduleId) {
359
386
  return handleTurbopackModule(bundle, moduleId, id);
360
387
  }
361
- throw new Error(`Module ${id} not found`);
388
+ throw new Error(`Module "${id}" not found.`);
362
389
  } catch (requireError) {
363
390
  if (typeof self.__original_webpack_require__ !== "function") {
364
- throw new Error(
365
- `Module ${id} not found in remote component bundle ${bundle}`,
391
+ throw new RemoteComponentsError(
392
+ `Module "${id}" not found in remote component bundle "${bundle}".`,
366
393
  {
367
394
  cause: requireError instanceof Error ? requireError : void 0
368
395
  }
@@ -371,8 +398,8 @@ function createModuleRequire(runtime) {
371
398
  try {
372
399
  return self.__original_webpack_require__(id);
373
400
  } catch (originalError) {
374
- throw new Error(
375
- `Module ${id} not found in remote component bundle ${bundle}`,
401
+ throw new RemoteComponentsError(
402
+ `Module "${id}" not found in remote component bundle "${bundle}".`,
376
403
  { cause: originalError instanceof Error ? originalError : void 0 }
377
404
  );
378
405
  }
@@ -663,7 +690,9 @@ function handleTurbopackModule(bundle, moduleId, id) {
663
690
  return self.__webpack_chunk_load__?.(bundleUrl, bundle);
664
691
  }
665
692
  }
666
- throw new Error(`Failed to load chunk ${url} for module ${id}`);
693
+ throw new Error(
694
+ `Failed to load Turbopack chunk "${url}" for module "${id}". Check the URL is correct.`
695
+ );
667
696
  },
668
697
  // global
669
698
  g: self.__remote_components_turbopack_global__[bundle],
@@ -681,6 +710,7 @@ function handleTurbopackModule(bundle, moduleId, id) {
681
710
  var init_webpack_adapter = __esm({
682
711
  "src/shared/client/webpack-adapter.ts"() {
683
712
  "use strict";
713
+ init_error();
684
714
  init_const();
685
715
  }
686
716
  });
@@ -904,10 +934,14 @@ async function webpackRuntime(bundle, shared, remoteShared) {
904
934
  const remoteBundle = match?.groups?.bundle;
905
935
  const id = match?.groups?.id;
906
936
  if (!(id && remoteBundle)) {
907
- throw new Error(`Module not found: "${remoteId}"`);
937
+ throw new RemoteComponentsError(
938
+ `Remote Component module "${remoteId}" not found. Did you forget to wrap the Next.js config with \`withRemoteComponents\` on both host and remote?`
939
+ );
908
940
  }
909
941
  if (typeof self.__remote_webpack_require__?.[remoteBundle] !== "function") {
910
- throw new Error(`Remote component loading error "${remoteBundle}"`);
942
+ throw new RemoteComponentsError(
943
+ `Remote Components are not available in "${remoteBundle}". Did you forget to wrap the Next.js config with \`withRemoteComponents\` on both host and remote?`
944
+ );
911
945
  }
912
946
  return self.__remote_webpack_require__[remoteBundle](id);
913
947
  };
@@ -929,8 +963,8 @@ async function webpackRuntime(bundle, shared, remoteShared) {
929
963
  };
930
964
  newScript.onerror = () => {
931
965
  reject(
932
- new Error(
933
- `Failed to load script ${script.src} for remote component`
966
+ new RemoteComponentsError(
967
+ `Failed to load <script src="${script.src}"> for Remote Component. Check the URL is correct.`
934
968
  )
935
969
  );
936
970
  newScript.remove();
@@ -990,6 +1024,7 @@ async function webpackRuntime(bundle, shared, remoteShared) {
990
1024
  var init_webpack = __esm({
991
1025
  "src/html/host/runtime/webpack.ts"() {
992
1026
  "use strict";
1027
+ init_error();
993
1028
  init_shared_modules();
994
1029
  init_next_client_pages_loader();
995
1030
  init_polyfill();
@@ -1057,6 +1092,7 @@ import { hydrateRoot } from "react-dom/client";
1057
1092
  // src/shared/client/component-loader.ts
1058
1093
  init_shared_modules();
1059
1094
  init_next_client_pages_loader();
1095
+ init_error();
1060
1096
  init_webpack_adapter();
1061
1097
  import * as React from "react";
1062
1098
  import * as ReactDOM from "react-dom";
@@ -1064,6 +1100,9 @@ import * as ReactDOMClient from "react-dom/client";
1064
1100
  import * as JSXDevRuntime from "react/jsx-dev-runtime";
1065
1101
  import * as JSXRuntime from "react/jsx-runtime";
1066
1102
 
1103
+ // src/shared/client/script-loader.ts
1104
+ init_error();
1105
+
1067
1106
  // src/shared/client/rsc.ts
1068
1107
  import { ReadableStream } from "web-streams-polyfill";
1069
1108
  function fixPayload(payload) {
@@ -1133,6 +1172,7 @@ function createRSCStream(rscName, data) {
1133
1172
  // src/shared/client/remote-component.ts
1134
1173
  init_webpack_adapter();
1135
1174
  init_const();
1175
+ init_error();
1136
1176
 
1137
1177
  // src/html/host/index.tsx
1138
1178
  init_utils();
@@ -1212,7 +1252,11 @@ function applyOriginToNodes(doc, url) {
1212
1252
  }
1213
1253
  }
1214
1254
 
1255
+ // src/html/host/index.tsx
1256
+ init_error();
1257
+
1215
1258
  // src/html/host/runtime/index.ts
1259
+ init_error();
1216
1260
  async function getRuntime(type, url, bundle, shared, remoteShared) {
1217
1261
  if (typeof globalThis.process === "undefined") {
1218
1262
  globalThis.process = {
@@ -1226,7 +1270,9 @@ async function getRuntime(type, url, bundle, shared, remoteShared) {
1226
1270
  const { turbopackRuntime: turbopackRuntime2 } = await Promise.resolve().then(() => (init_turbopack(), turbopack_exports));
1227
1271
  return turbopackRuntime2(url, bundle, shared, remoteShared);
1228
1272
  }
1229
- throw new Error(`Runtime ${type} is not supported`);
1273
+ throw new RemoteComponentsError(
1274
+ `Remote Components runtime "${type}" is not supported. Supported runtimes are "webpack" and "turbopack".`
1275
+ );
1230
1276
  }
1231
1277
 
1232
1278
  // src/html/host/index.tsx
@@ -1251,9 +1297,6 @@ if (typeof HTMLElement !== "undefined") {
1251
1297
  if (this.getAttribute("src")) {
1252
1298
  this.load().catch((e) => {
1253
1299
  console.error(e);
1254
- throw new Error(
1255
- `Failed to load remote component: ${this.bundle ?? this.name ?? this.getAttribute("src")}`
1256
- );
1257
1300
  });
1258
1301
  }
1259
1302
  } else if (name === "mode" && oldValue !== newValue && this.root) {
@@ -1265,7 +1308,7 @@ if (typeof HTMLElement !== "undefined") {
1265
1308
  });
1266
1309
  this.root = newRoot;
1267
1310
  this.load().catch((e) => {
1268
- throw new Error(`Failed to load remote component: ${e}`);
1311
+ console.error(e);
1269
1312
  });
1270
1313
  }
1271
1314
  }
@@ -1299,14 +1342,14 @@ if (typeof HTMLElement !== "undefined") {
1299
1342
  }
1300
1343
  if (this.hasAttribute("src") || this.querySelector("div#__REMOTE_COMPONENT__") || this.hasAttribute("data-ssr")) {
1301
1344
  this.load().catch((e) => {
1302
- throw new Error(`Failed to load remote component: ${e}`);
1345
+ console.error(e);
1303
1346
  });
1304
1347
  }
1305
1348
  this.isLoading = true;
1306
1349
  const src = this.getAttribute("src");
1307
1350
  const remoteComponentChild = this.querySelector("div#__REMOTE_COMPONENT__") || this.querySelector("div[data-bundle][data-route]");
1308
1351
  if (!src && !remoteComponentChild) {
1309
- throw new Error("src attribute is required");
1352
+ throw new RemoteComponentsError('"src" attribute is required');
1310
1353
  }
1311
1354
  let url = null;
1312
1355
  let html = this.innerHTML;
@@ -1324,14 +1367,41 @@ if (typeof HTMLElement !== "undefined") {
1324
1367
  };
1325
1368
  const res = await fetch(url, fetchInit);
1326
1369
  if (!res.ok) {
1327
- throw new Error(
1328
- `Failed to fetch remote component "${this.name}": ${res.status}`
1329
- );
1370
+ let error = failedToFetchRemoteComponentError(url.href, {
1371
+ cause: new Error(`${res.status} ${res.statusText}`)
1372
+ });
1373
+ try {
1374
+ const body = await res.text();
1375
+ const parser2 = new DOMParser();
1376
+ const doc2 = parser2.parseFromString(body, "text/html");
1377
+ const errorTemplate = doc2.querySelector(
1378
+ "template[data-next-error-message]"
1379
+ );
1380
+ const errorMessage = errorTemplate?.getAttribute(
1381
+ "data-next-error-message"
1382
+ );
1383
+ const errorStack = errorTemplate?.getAttribute(
1384
+ "data-next-error-stack"
1385
+ );
1386
+ if (errorMessage) {
1387
+ error = new RemoteComponentsError(errorMessage);
1388
+ if (errorStack) {
1389
+ error.stack = errorStack;
1390
+ }
1391
+ }
1392
+ } catch {
1393
+ }
1394
+ throw error;
1330
1395
  }
1331
1396
  html = await res.text();
1332
1397
  }
1333
1398
  const parser = new DOMParser();
1334
1399
  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}"]`)) {
1401
+ throw multipleRemoteComponentsError(
1402
+ url?.href ?? (this.getAttribute("src") || "unknown")
1403
+ );
1404
+ }
1335
1405
  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
1336
1406
  doc.querySelector("div[data-bundle][data-route]") ?? // fallback to Next.js Pages Router
1337
1407
  doc.querySelector("div#__next");
@@ -1371,8 +1441,15 @@ if (typeof HTMLElement !== "undefined") {
1371
1441
  );
1372
1442
  const remoteShared = nextData?.props.__REMOTE_COMPONENT__?.shared ?? (JSON.parse(remoteSharedEl?.textContent ?? "{}") ?? {});
1373
1443
  remoteSharedEl?.parentElement?.removeChild(remoteSharedEl);
1444
+ if ("__remote_components_missing_shared__" in remoteShared) {
1445
+ throw new RemoteComponentsError(
1446
+ remoteShared.__remote_components_missing_shared__
1447
+ );
1448
+ }
1374
1449
  if (!component || !(rsc || nextData)) {
1375
- throw new Error(`Failed to find component with id "${this.name}"`);
1450
+ throw new RemoteComponentsError(
1451
+ `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
+ );
1376
1453
  }
1377
1454
  const removable = Array.from(this.childNodes);
1378
1455
  const links = doc.querySelectorAll("link[href]");
@@ -1390,8 +1467,8 @@ if (typeof HTMLElement !== "undefined") {
1390
1467
  };
1391
1468
  newLink.onerror = () => {
1392
1469
  reject(
1393
- new Error(
1394
- `Failed to load link ${link.href} for remote component`
1470
+ new RemoteComponentsError(
1471
+ `Failed to load <link href="${link.href}"> for Remote Component. Check the URL is correct.`
1395
1472
  )
1396
1473
  );
1397
1474
  };