remote-components 0.0.42 → 0.0.44

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 (85) hide show
  1. package/dist/{component-loader-2ca91ad8.d.ts → component-loader-26b1f55e.d.ts} +63 -1
  2. package/dist/html/host.cjs +55 -19
  3. package/dist/html/host.cjs.map +1 -1
  4. package/dist/html/host.js +55 -19
  5. package/dist/html/host.js.map +1 -1
  6. package/dist/internal/next/host/app-router-client.d.ts +30 -3
  7. package/dist/internal/next/remote/render-client.cjs +1 -1
  8. package/dist/internal/next/remote/render-client.cjs.map +1 -1
  9. package/dist/internal/next/remote/render-client.js +1 -1
  10. package/dist/internal/next/remote/render-client.js.map +1 -1
  11. package/dist/internal/shared/client/remote-component.cjs +29 -0
  12. package/dist/internal/shared/client/remote-component.cjs.map +1 -1
  13. package/dist/internal/shared/client/remote-component.d.ts +2 -2
  14. package/dist/internal/shared/client/remote-component.js +29 -0
  15. package/dist/internal/shared/client/remote-component.js.map +1 -1
  16. package/dist/internal/shared/ssr/dom-flight.d.ts +1 -1
  17. package/dist/internal/shared/ssr/fetch-headers.cjs +4 -6
  18. package/dist/internal/shared/ssr/fetch-headers.cjs.map +1 -1
  19. package/dist/internal/shared/ssr/fetch-headers.d.ts +1 -1
  20. package/dist/internal/shared/ssr/fetch-headers.js +4 -6
  21. package/dist/internal/shared/ssr/fetch-headers.js.map +1 -1
  22. package/dist/internal/shared/ssr/fetch-remote-component.cjs +14 -23
  23. package/dist/internal/shared/ssr/fetch-remote-component.cjs.map +1 -1
  24. package/dist/internal/shared/ssr/fetch-remote-component.d.ts +5 -9
  25. package/dist/internal/shared/ssr/fetch-remote-component.js +13 -21
  26. package/dist/internal/shared/ssr/fetch-remote-component.js.map +1 -1
  27. package/dist/internal/shared/ssr/fetch-with-hooks.cjs +40 -0
  28. package/dist/internal/shared/ssr/fetch-with-hooks.cjs.map +1 -0
  29. package/dist/internal/shared/ssr/fetch-with-hooks.d.ts +39 -0
  30. package/dist/internal/shared/ssr/fetch-with-hooks.js +16 -0
  31. package/dist/internal/shared/ssr/fetch-with-hooks.js.map +1 -0
  32. package/dist/internal/shared/ssr/get-ssr-relative-path-base-url.cjs.map +1 -1
  33. package/dist/internal/shared/ssr/get-ssr-relative-path-base-url.js.map +1 -1
  34. package/dist/next/config.cjs +1 -9
  35. package/dist/next/config.cjs.map +1 -1
  36. package/dist/next/config.js +1 -9
  37. package/dist/next/config.js.map +1 -1
  38. package/dist/next/host/app-router-server.cjs +7 -8
  39. package/dist/next/host/app-router-server.cjs.map +1 -1
  40. package/dist/next/host/app-router-server.d.ts +13 -4
  41. package/dist/next/host/app-router-server.js +7 -8
  42. package/dist/next/host/app-router-server.js.map +1 -1
  43. package/dist/next/host/client/index.cjs +149 -72
  44. package/dist/next/host/client/index.cjs.map +1 -1
  45. package/dist/next/host/client/index.d.ts +2 -49
  46. package/dist/next/host/client/index.js +144 -66
  47. package/dist/next/host/client/index.js.map +1 -1
  48. package/dist/next/host/pages-router-client.cjs +62 -2
  49. package/dist/next/host/pages-router-client.cjs.map +1 -1
  50. package/dist/next/host/pages-router-client.d.ts +26 -1
  51. package/dist/next/host/pages-router-client.js +63 -3
  52. package/dist/next/host/pages-router-client.js.map +1 -1
  53. package/dist/next/host/pages-router-server.cjs +18 -22
  54. package/dist/next/host/pages-router-server.cjs.map +1 -1
  55. package/dist/next/host/pages-router-server.d.ts +36 -3
  56. package/dist/next/host/pages-router-server.js +18 -22
  57. package/dist/next/host/pages-router-server.js.map +1 -1
  58. package/dist/next/index.cjs.map +1 -1
  59. package/dist/next/index.d.ts +1 -1
  60. package/dist/next/index.js.map +1 -1
  61. package/dist/react/index.cjs +1581 -87
  62. package/dist/react/index.cjs.map +1 -1
  63. package/dist/react/index.d.ts +13 -14
  64. package/dist/react/index.js +1559 -87
  65. package/dist/react/index.js.map +1 -1
  66. package/dist/shared/host/app.cjs +1 -1
  67. package/dist/shared/host/app.cjs.map +1 -1
  68. package/dist/shared/host/app.js +1 -1
  69. package/dist/shared/host/app.js.map +1 -1
  70. package/dist/shared/host/pages.cjs +1 -1
  71. package/dist/shared/host/pages.cjs.map +1 -1
  72. package/dist/shared/host/pages.js +1 -1
  73. package/dist/shared/host/pages.js.map +1 -1
  74. package/dist/shared/remote/app.cjs +1 -1
  75. package/dist/shared/remote/app.cjs.map +1 -1
  76. package/dist/shared/remote/app.js +1 -1
  77. package/dist/shared/remote/app.js.map +1 -1
  78. package/dist/shared/remote/pages.cjs +1 -1
  79. package/dist/shared/remote/pages.cjs.map +1 -1
  80. package/dist/shared/remote/pages.js +1 -1
  81. package/dist/shared/remote/pages.js.map +1 -1
  82. package/dist/types-2ab78f67.d.ts +71 -0
  83. package/package.json +54 -32
  84. package/dist/types-235b68a5.d.ts +0 -32
  85. package/dist/types-cbf6c34f.d.ts +0 -9
@@ -7,6 +7,68 @@ interface RemoteComponentMetadata {
7
7
  id: string;
8
8
  type: 'nextjs' | 'remote-component' | 'unknown';
9
9
  }
10
+ /**
11
+ * Hook function that intercepts remote component fetch requests.
12
+ * Can be used to modify request options, provide a custom response, or inspect the request.
13
+ *
14
+ * @param url - The URL being fetched
15
+ * @param init - The fetch init options being used
16
+ * @returns Optional Response to use instead of fetching, or void/undefined to proceed with normal fetch
17
+ *
18
+ * @example
19
+ * // Log all remote component requests
20
+ * const onRequest = async (url, init) => {
21
+ * console.log('Fetching remote component from:', url.href);
22
+ * };
23
+ *
24
+ * @example
25
+ * // Add custom headers
26
+ * const onRequest = async (url, init) => {
27
+ * (init.headers as Headers).set('X-Custom-Header', 'value');
28
+ * };
29
+ *
30
+ * @example
31
+ * // Provide a cached response
32
+ * const onRequest = async (url) => {
33
+ * const cached = cache.get(url.href);
34
+ * if (cached) {
35
+ * return new Response(cached);
36
+ * }
37
+ * };
38
+ */
39
+ type OnRequestHook = (url: URL, init: RequestInit) => Promise<Response | undefined> | Response | undefined;
40
+ /**
41
+ * Hook function that is called after a remote component fetch completes.
42
+ * Can be used to inspect the response, check for redirects, or transform the response.
43
+ *
44
+ * @param url - The original URL that was requested
45
+ * @param response - The Response object from the fetch
46
+ * @returns Optional Response to use instead of the original, or void/undefined to use the original response
47
+ *
48
+ * @example
49
+ * // Check for redirects
50
+ * const onResponse = async (url, response) => {
51
+ * if (response.redirected) {
52
+ * console.log(`Redirected from ${url.href} to ${response.url}`);
53
+ * }
54
+ * };
55
+ *
56
+ * @example
57
+ * // Cache the response
58
+ * const onResponse = async (url, response) => {
59
+ * const cloned = response.clone();
60
+ * cache.set(url.href, await cloned.text());
61
+ * };
62
+ *
63
+ * @example
64
+ * // Transform the response
65
+ * const onResponse = async (url, response) => {
66
+ * const text = await response.text();
67
+ * const modified = text.replace(/foo/g, 'bar');
68
+ * return new Response(modified, response);
69
+ * };
70
+ */
71
+ type OnResponseHook = (url: URL, response: Response) => Promise<Response | undefined> | Response | undefined;
10
72
 
11
73
  interface RemoteComponentProps {
12
74
  /** The src provided to the `<RemoteComponent>` component. May be relative or absolute URL. */
@@ -79,4 +141,4 @@ type LoadRemoteComponentProps = Pick<RemoteComponentProps, 'name' | 'bundle' | '
79
141
  */
80
142
  declare function loadRemoteComponent({ url, name, rscName, bundle, route, runtime, data, nextData, scripts, shared, remoteShared, container, }: LoadRemoteComponentProps): Promise<LoaderResult>;
81
143
 
82
- export { GlobalScope as G, LoadRemoteComponentProps as L, MountOrUnmountFunction as M, RemoteComponentProps as R, LoaderResult as a, loadRemoteComponent as l };
144
+ export { GlobalScope as G, LoadRemoteComponentProps as L, MountOrUnmountFunction as M, OnRequestHook as O, RemoteComponentProps as R, OnResponseHook as a, LoaderResult as b, loadRemoteComponent as l };
@@ -287,6 +287,7 @@ var init_const = __esm({
287
287
  // src/shared/client/chunk-loader.ts
288
288
  function createChunkLoader(runtime) {
289
289
  return function __turbopack_chunk_load__(chunkId, scriptBundle) {
290
+ logDebug("ChunkLoader", `Loading chunk: "${chunkId}"`);
290
291
  const self = globalThis;
291
292
  const {
292
293
  bundle,
@@ -296,8 +297,14 @@ function createChunkLoader(runtime) {
296
297
  bundle: scriptBundle ?? "",
297
298
  id: chunkId
298
299
  };
300
+ logDebug(
301
+ "ChunkLoader",
302
+ `Parsed chunk - bundle: "${bundle}", path: "${path}", prefix: "${prefix}"`
303
+ );
299
304
  const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : runtime;
305
+ logDebug("ChunkLoader", `Remote runtime: "${remoteRuntime}"`);
300
306
  if (remoteRuntime === RUNTIME_WEBPACK) {
307
+ logDebug("ChunkLoader", "Skipping chunk load - webpack runtime detected");
301
308
  return Promise.resolve(void 0);
302
309
  }
303
310
  const url = new URL(
@@ -308,14 +315,17 @@ function createChunkLoader(runtime) {
308
315
  self.__remote_bundle_url__?.[bundle ?? "default"] ?? new URL(location.origin)
309
316
  ).href;
310
317
  if (url.endsWith(".css")) {
318
+ logDebug("ChunkLoader", `Skipping CSS file: "${url}"`);
311
319
  return;
312
320
  }
313
321
  if (!self.__remote_components_turbopack_chunk_loader_promise__) {
314
322
  self.__remote_components_turbopack_chunk_loader_promise__ = {};
315
323
  }
316
324
  if (self.__remote_components_turbopack_chunk_loader_promise__[url]) {
325
+ logDebug("ChunkLoader", `Returning cached promise for: "${url}"`);
317
326
  return self.__remote_components_turbopack_chunk_loader_promise__[url];
318
327
  }
328
+ logDebug("ChunkLoader", `Fetching chunk from: "${url}"`);
319
329
  self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise((resolve, reject) => {
320
330
  fetch(url).then((res) => res.text()).then((code) => {
321
331
  if (code.includes("globalThis.TURBOPACK")) {
@@ -327,7 +337,12 @@ function createChunkLoader(runtime) {
327
337
  };
328
338
  }
329
339
  async function handleTurbopackChunk(code, bundle, url) {
340
+ logDebug(
341
+ "ChunkLoader",
342
+ `Handling Turbopack chunk - bundle: "${bundle}", url: "${url}"`
343
+ );
330
344
  if (/importScripts\(\.\.\.self.TURBOPACK_NEXT_CHUNK_URLS/.test(code)) {
345
+ logDebug("ChunkLoader", `Skipping worker chunk: "${url}"`);
331
346
  const preloadLinks = document.querySelectorAll(
332
347
  `link[rel="preload"][href="${new URL(url).pathname}"]`
333
348
  );
@@ -336,6 +351,7 @@ async function handleTurbopackChunk(code, bundle, url) {
336
351
  }
337
352
  const self = globalThis;
338
353
  const bundleKey = getBundleKey(bundle);
354
+ logDebug("ChunkLoader", `Bundle key: "${bundleKey}"`);
339
355
  const transformedCode = code.replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${bundleKey}`).replace(
340
356
  /TURBOPACK_WORKER_LOCATION/g,
341
357
  `TURBOPACK_WORKER_LOCATION_${bundleKey}`
@@ -355,6 +371,7 @@ async function handleTurbopackChunk(code, bundle, url) {
355
371
  )
356
372
  ).href}$1$2.js.map`
357
373
  );
374
+ logDebug("ChunkLoader", `Creating blob script for: "${url}"`);
358
375
  await new Promise((scriptResolve, scriptReject) => {
359
376
  const blob = new Blob([transformedCode], {
360
377
  type: "application/javascript; charset=UTF-8"
@@ -381,10 +398,18 @@ async function handleTurbopackChunk(code, bundle, url) {
381
398
  document.head.appendChild(script);
382
399
  });
383
400
  const chunkLists = self[`TURBOPACK_${bundleKey}_CHUNK_LISTS`];
401
+ logDebug(
402
+ "ChunkLoader",
403
+ `Processing chunk lists for bundle "${bundle}": ${chunkLists?.length ?? 0} lists`
404
+ );
384
405
  const loadChunkLists = [];
385
406
  while (chunkLists?.length) {
386
407
  const { chunks } = chunkLists.shift() ?? { chunks: [] };
387
408
  if (chunks.length > 0) {
409
+ logDebug(
410
+ "ChunkLoader",
411
+ `Loading ${chunks.length} additional chunks for bundle "${bundle}": [${chunks.join(", ")}]`
412
+ );
388
413
  chunks.forEach((id) => {
389
414
  const chunkLoadResult = self.__webpack_chunk_load__?.(
390
415
  `[${bundle}] ${url.slice(0, url.indexOf("/_next"))}/_next/${id}`
@@ -396,6 +421,10 @@ async function handleTurbopackChunk(code, bundle, url) {
396
421
  }
397
422
  }
398
423
  if (loadChunkLists.length > 0) {
424
+ logDebug(
425
+ "ChunkLoader",
426
+ `Waiting for ${loadChunkLists.length} additional chunks to load`
427
+ );
399
428
  await Promise.all(loadChunkLists);
400
429
  }
401
430
  }
@@ -403,6 +432,7 @@ var init_chunk_loader = __esm({
403
432
  "src/shared/client/chunk-loader.ts"() {
404
433
  "use strict";
405
434
  init_error();
435
+ init_logger();
406
436
  init_const();
407
437
  }
408
438
  });
@@ -1510,23 +1540,35 @@ init_webpack_adapter();
1510
1540
  init_error();
1511
1541
 
1512
1542
  // src/shared/ssr/fetch-headers.ts
1513
- function remoteFetchHeaders(additionalHeaders) {
1543
+ function remoteFetchHeaders() {
1514
1544
  return {
1515
1545
  /**
1516
1546
  * Authenticates deployment protection for the remote. Needed for SSR and SSG clients.
1517
- * Ensure the automation bypass secret is the same on the client and host.
1518
- * Otherwise, manually specify x-vercel-protection-bypass for the remote in the `additionalHeaders` parameter.
1547
+ * If the remote component uses vercel deployment protection, ensure the host and remote vercel
1548
+ * projects share a common automation bypass secret, and the shared secret is used as the
1549
+ * VERCEL_AUTOMATION_BYPASS_SECRET env var in the host project.
1519
1550
  */
1520
1551
  ...typeof process === "object" && typeof process.env === "object" && typeof process.env.VERCEL_AUTOMATION_BYPASS_SECRET === "string" ? {
1521
1552
  "x-vercel-protection-bypass": process.env.VERCEL_AUTOMATION_BYPASS_SECRET
1522
1553
  } : {},
1523
- ...Object.fromEntries(
1524
- additionalHeaders instanceof Headers ? additionalHeaders.entries() : Object.entries(additionalHeaders ?? {})
1525
- ),
1526
1554
  Accept: "text/html"
1527
1555
  };
1528
1556
  }
1529
1557
 
1558
+ // src/shared/ssr/fetch-with-hooks.ts
1559
+ async function fetchWithHooks(url, init, options = {}) {
1560
+ const { onRequest, onResponse } = options;
1561
+ let res = await onRequest?.(url, init);
1562
+ if (!res) {
1563
+ res = await fetch(url, init);
1564
+ }
1565
+ const transformedRes = await onResponse?.(url, res);
1566
+ if (transformedRes) {
1567
+ res = transformedRes;
1568
+ }
1569
+ return res;
1570
+ }
1571
+
1530
1572
  // src/html/host/index.tsx
1531
1573
  init_utils();
1532
1574
 
@@ -1569,7 +1611,8 @@ if (typeof HTMLElement !== "undefined") {
1569
1611
  prevName;
1570
1612
  root = null;
1571
1613
  reactRoot;
1572
- additionalHeaders;
1614
+ onRequest;
1615
+ onResponse;
1573
1616
  static get observedAttributes() {
1574
1617
  return ["src", "name", "mode"];
1575
1618
  }
@@ -1633,16 +1676,6 @@ if (typeof HTMLElement !== "undefined") {
1633
1676
  }
1634
1677
  this.name = this.getAttribute("name") || "__vercel_remote_component";
1635
1678
  this.bundle = "default";
1636
- const additionalHeadersString = this.getAttribute("additional-headers");
1637
- if (additionalHeadersString) {
1638
- this.additionalHeaders = additionalHeadersString.split(";").reduce((headers, headerPair) => {
1639
- const [key, value] = headerPair.split(":");
1640
- if (key && value) {
1641
- headers.set(key.trim(), value.trim());
1642
- }
1643
- return headers;
1644
- }, new Headers());
1645
- }
1646
1679
  if (this.hasAttribute("src") || this.querySelector("div#__REMOTE_COMPONENT__") || this.hasAttribute("data-ssr")) {
1647
1680
  this.load().catch((e) => {
1648
1681
  console.error(e);
@@ -1680,10 +1713,13 @@ if (typeof HTMLElement !== "undefined") {
1680
1713
  if (!remoteComponentChild && url) {
1681
1714
  const fetchInit = {
1682
1715
  method: "GET",
1683
- headers: remoteFetchHeaders(this.additionalHeaders),
1716
+ headers: remoteFetchHeaders(),
1684
1717
  credentials: this.getAttribute("credentials") || "same-origin"
1685
1718
  };
1686
- const res = await fetch(url, fetchInit);
1719
+ const res = await fetchWithHooks(url, fetchInit, {
1720
+ onRequest: this.onRequest,
1721
+ onResponse: this.onResponse
1722
+ });
1687
1723
  if (!res.ok) {
1688
1724
  let error = failedToFetchRemoteComponentError(url.href, res);
1689
1725
  try {