remote-components 0.0.42 → 0.0.43

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.
@@ -37,7 +37,7 @@ module.exports = __toCommonJS(client_exports);
37
37
  var Image = __toESM(require("next/image"), 1);
38
38
 
39
39
  // src/react/index.tsx
40
- var import_react = require("react");
40
+ var import_react2 = require("react");
41
41
  var import_react_dom = require("react-dom");
42
42
 
43
43
  // src/shared/client/apply-origin.ts
@@ -624,6 +624,7 @@ function getBundleKey(bundle) {
624
624
  // src/shared/client/chunk-loader.ts
625
625
  function createChunkLoader(runtime) {
626
626
  return function __turbopack_chunk_load__(chunkId, scriptBundle) {
627
+ logDebug("ChunkLoader", `Loading chunk: "${chunkId}"`);
627
628
  const self = globalThis;
628
629
  const {
629
630
  bundle,
@@ -633,8 +634,14 @@ function createChunkLoader(runtime) {
633
634
  bundle: scriptBundle ?? "",
634
635
  id: chunkId
635
636
  };
637
+ logDebug(
638
+ "ChunkLoader",
639
+ `Parsed chunk - bundle: "${bundle}", path: "${path}", prefix: "${prefix}"`
640
+ );
636
641
  const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : runtime;
642
+ logDebug("ChunkLoader", `Remote runtime: "${remoteRuntime}"`);
637
643
  if (remoteRuntime === RUNTIME_WEBPACK) {
644
+ logDebug("ChunkLoader", "Skipping chunk load - webpack runtime detected");
638
645
  return Promise.resolve(void 0);
639
646
  }
640
647
  const url = new URL(
@@ -645,14 +652,17 @@ function createChunkLoader(runtime) {
645
652
  self.__remote_bundle_url__?.[bundle ?? "default"] ?? new URL(location.origin)
646
653
  ).href;
647
654
  if (url.endsWith(".css")) {
655
+ logDebug("ChunkLoader", `Skipping CSS file: "${url}"`);
648
656
  return;
649
657
  }
650
658
  if (!self.__remote_components_turbopack_chunk_loader_promise__) {
651
659
  self.__remote_components_turbopack_chunk_loader_promise__ = {};
652
660
  }
653
661
  if (self.__remote_components_turbopack_chunk_loader_promise__[url]) {
662
+ logDebug("ChunkLoader", `Returning cached promise for: "${url}"`);
654
663
  return self.__remote_components_turbopack_chunk_loader_promise__[url];
655
664
  }
665
+ logDebug("ChunkLoader", `Fetching chunk from: "${url}"`);
656
666
  self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise((resolve, reject) => {
657
667
  fetch(url).then((res) => res.text()).then((code) => {
658
668
  if (code.includes("globalThis.TURBOPACK")) {
@@ -664,7 +674,12 @@ function createChunkLoader(runtime) {
664
674
  };
665
675
  }
666
676
  async function handleTurbopackChunk(code, bundle, url) {
677
+ logDebug(
678
+ "ChunkLoader",
679
+ `Handling Turbopack chunk - bundle: "${bundle}", url: "${url}"`
680
+ );
667
681
  if (/importScripts\(\.\.\.self.TURBOPACK_NEXT_CHUNK_URLS/.test(code)) {
682
+ logDebug("ChunkLoader", `Skipping worker chunk: "${url}"`);
668
683
  const preloadLinks = document.querySelectorAll(
669
684
  `link[rel="preload"][href="${new URL(url).pathname}"]`
670
685
  );
@@ -673,6 +688,7 @@ async function handleTurbopackChunk(code, bundle, url) {
673
688
  }
674
689
  const self = globalThis;
675
690
  const bundleKey = getBundleKey(bundle);
691
+ logDebug("ChunkLoader", `Bundle key: "${bundleKey}"`);
676
692
  const transformedCode = code.replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${bundleKey}`).replace(
677
693
  /TURBOPACK_WORKER_LOCATION/g,
678
694
  `TURBOPACK_WORKER_LOCATION_${bundleKey}`
@@ -692,6 +708,7 @@ async function handleTurbopackChunk(code, bundle, url) {
692
708
  )
693
709
  ).href}$1$2.js.map`
694
710
  );
711
+ logDebug("ChunkLoader", `Creating blob script for: "${url}"`);
695
712
  await new Promise((scriptResolve, scriptReject) => {
696
713
  const blob = new Blob([transformedCode], {
697
714
  type: "application/javascript; charset=UTF-8"
@@ -718,10 +735,18 @@ async function handleTurbopackChunk(code, bundle, url) {
718
735
  document.head.appendChild(script);
719
736
  });
720
737
  const chunkLists = self[`TURBOPACK_${bundleKey}_CHUNK_LISTS`];
738
+ logDebug(
739
+ "ChunkLoader",
740
+ `Processing chunk lists for bundle "${bundle}": ${chunkLists?.length ?? 0} lists`
741
+ );
721
742
  const loadChunkLists = [];
722
743
  while (chunkLists?.length) {
723
744
  const { chunks } = chunkLists.shift() ?? { chunks: [] };
724
745
  if (chunks.length > 0) {
746
+ logDebug(
747
+ "ChunkLoader",
748
+ `Loading ${chunks.length} additional chunks for bundle "${bundle}": [${chunks.join(", ")}]`
749
+ );
725
750
  chunks.forEach((id) => {
726
751
  const chunkLoadResult = self.__webpack_chunk_load__?.(
727
752
  `[${bundle}] ${url.slice(0, url.indexOf("/_next"))}/_next/${id}`
@@ -733,6 +758,10 @@ async function handleTurbopackChunk(code, bundle, url) {
733
758
  }
734
759
  }
735
760
  if (loadChunkLists.length > 0) {
761
+ logDebug(
762
+ "ChunkLoader",
763
+ `Waiting for ${loadChunkLists.length} additional chunks to load`
764
+ );
736
765
  await Promise.all(loadChunkLists);
737
766
  }
738
767
  }
@@ -1450,12 +1479,57 @@ function getClientOrServerUrl(src, serverFallback) {
1450
1479
  return typeof src === "string" ? new URL(src, fallback) : src;
1451
1480
  }
1452
1481
 
1453
- // src/react/index.tsx
1454
- var import_jsx_runtime2 = (
1455
- // TODO: remove wrapper div by converting HTML to RSC or React tree
1456
- require("react/jsx-runtime")
1457
- );
1458
- var import_react2 = require("react");
1482
+ // src/react/hooks/use-shadow-root.ts
1483
+ var import_react = require("react");
1484
+ function useShadowRoot({
1485
+ isolate,
1486
+ mode,
1487
+ keySuffix
1488
+ }) {
1489
+ const shadowRootContainerRef = (0, import_react.useRef)(null);
1490
+ const [shadowRoot, setShadowRoot] = (0, import_react.useState)(() => {
1491
+ const self = globalThis;
1492
+ const shadowRootKey = `__remote_components_shadowroot_${keySuffix}`;
1493
+ const ssrShadowRoot = typeof document !== "undefined" ? document.querySelector(
1494
+ `[data-remote-component-id="shadowroot_${keySuffix}"]`
1495
+ )?.shadowRoot ?? self[shadowRootKey] ?? null : null;
1496
+ self[shadowRootKey] = null;
1497
+ return ssrShadowRoot;
1498
+ });
1499
+ (0, import_react.useLayoutEffect)(() => {
1500
+ if (isolate !== false && typeof document !== "undefined" && (!shadowRoot || !shadowRoot.isConnected)) {
1501
+ const self = globalThis;
1502
+ const shadowRootKey = `__remote_components_shadowroot_${keySuffix}`;
1503
+ let shadowRootElement = null;
1504
+ const element = document.querySelector(
1505
+ `[data-remote-component-id="shadowroot_${keySuffix}"]`
1506
+ );
1507
+ shadowRootElement = self[shadowRootKey] ?? element?.shadowRoot ?? null;
1508
+ if (!shadowRootElement && element) {
1509
+ try {
1510
+ shadowRootElement = element.attachShadow({ mode });
1511
+ self[shadowRootKey] = shadowRootElement;
1512
+ } catch {
1513
+ }
1514
+ }
1515
+ if (shadowRootElement) {
1516
+ shadowRootElement.querySelectorAll("*:not(link)").forEach((node) => {
1517
+ node.remove();
1518
+ });
1519
+ setShadowRoot(shadowRootElement);
1520
+ }
1521
+ } else if (isolate === false && shadowRoot) {
1522
+ const self = globalThis;
1523
+ const shadowRootKey = `__remote_components_shadowroot_${keySuffix}`;
1524
+ self[shadowRootKey] = null;
1525
+ setShadowRoot(null);
1526
+ }
1527
+ }, [isolate, shadowRoot, mode, keySuffix]);
1528
+ return { shadowRoot, shadowRootContainerRef };
1529
+ }
1530
+
1531
+ // src/react/utils/parse-remote-html.ts
1532
+ var DUMMY_FALLBACK = "http://remote-components-dummy-fallback";
1459
1533
  function getRemoteComponentHtml(html) {
1460
1534
  if (typeof document === "undefined")
1461
1535
  return html;
@@ -1475,7 +1549,13 @@ function getRemoteComponentHtml(html) {
1475
1549
  }
1476
1550
  return "";
1477
1551
  }
1478
- var DUMMY_FALLBACK = "http://remote-components-dummy-fallback";
1552
+
1553
+ // src/react/index.tsx
1554
+ var import_jsx_runtime2 = (
1555
+ // TODO: remove wrapper div by converting HTML to RSC or React tree
1556
+ require("react/jsx-runtime")
1557
+ );
1558
+ var import_react3 = require("react");
1479
1559
  function RemoteComponent({
1480
1560
  src,
1481
1561
  isolate,
@@ -1491,7 +1571,8 @@ function RemoteComponent({
1491
1571
  onError,
1492
1572
  onChange
1493
1573
  }) {
1494
- const name = (0, import_react.useMemo)(() => {
1574
+ const instanceId = (0, import_react2.useId)();
1575
+ const name = (0, import_react2.useMemo)(() => {
1495
1576
  if (typeof src === "string") {
1496
1577
  const url2 = new URL(
1497
1578
  src,
@@ -1505,28 +1586,23 @@ function RemoteComponent({
1505
1586
  }
1506
1587
  return nameProp;
1507
1588
  }, [src, nameProp]);
1508
- const [data, setData] = (0, import_react.useState)(null);
1509
- const url = (0, import_react.useMemo)(() => getClientOrServerUrl(src, DUMMY_FALLBACK), [src]);
1589
+ const [data, setData] = (0, import_react2.useState)(null);
1590
+ const url = (0, import_react2.useMemo)(() => getClientOrServerUrl(src, DUMMY_FALLBACK), [src]);
1510
1591
  const id = url.origin === (typeof location !== "undefined" ? location.origin : DUMMY_FALLBACK) ? url.pathname : url.href;
1511
- const keySuffix = `${escapeString(id)}_${escapeString(data?.name ?? name)}`;
1512
- const [remoteComponent, setRemoteComponent] = (0, import_react.useState)(null);
1513
- const shadowRootContainerRef = (0, import_react.useRef)(null);
1514
- const [shadowRoot, setShadowRoot] = (0, import_react.useState)(() => {
1515
- const self = globalThis;
1516
- const shadowRootKey = `__remote_components_shadowroot_${keySuffix}`;
1517
- const ssrShadowRoot = typeof document !== "undefined" ? document.querySelector(
1518
- `[data-remote-component-id="shadowroot_${keySuffix}"]`
1519
- )?.shadowRoot ?? self[shadowRootKey] ?? null : null;
1520
- self[shadowRootKey] = null;
1521
- return ssrShadowRoot;
1592
+ const keySuffix = `${escapeString(id)}_${escapeString(data?.name ?? name)}_${escapeString(instanceId)}`;
1593
+ const [remoteComponent, setRemoteComponent] = (0, import_react2.useState)(null);
1594
+ const { shadowRoot, shadowRootContainerRef } = useShadowRoot({
1595
+ isolate,
1596
+ mode,
1597
+ keySuffix
1522
1598
  });
1523
- const htmlRef = (0, import_react.useRef)(
1599
+ const htmlRef = (0, import_react2.useRef)(
1524
1600
  typeof document !== "undefined" ? document.querySelector(
1525
1601
  `[data-remote-component-id="shadowroot_${keySuffix}"]`
1526
1602
  )?.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
1527
1603
  );
1528
- const endTemplateRef = (0, import_react.useRef)(null);
1529
- const childrenRef = (0, import_react.useRef)(
1604
+ const endTemplateRef = (0, import_react2.useRef)(null);
1605
+ const childrenRef = (0, import_react2.useRef)(
1530
1606
  typeof document !== "undefined" ? (() => {
1531
1607
  let el = document.querySelector(`template[id="${name}_start"]`);
1532
1608
  const elements = [];
@@ -1539,44 +1615,28 @@ function RemoteComponent({
1539
1615
  return elements;
1540
1616
  })() : []
1541
1617
  );
1542
- const prevSrcRef = (0, import_react.useRef)(null);
1543
- const componentHydrationHtml = (0, import_react.useRef)(null);
1544
- const prevIsRemoteComponentRef = (0, import_react.useRef)(false);
1545
- const prevUrlRef = (0, import_react.useRef)(null);
1546
- const prevRemoteComponentContainerRef = (0, import_react.useRef)(null);
1547
- const unmountRef = (0, import_react.useRef)(null);
1548
- const prevNameRef = (0, import_react.useRef)(void 0);
1549
- (0, import_react.useLayoutEffect)(() => {
1618
+ const prevSrcRef = (0, import_react2.useRef)(null);
1619
+ const componentHydrationHtml = (0, import_react2.useRef)(null);
1620
+ const prevIsRemoteComponentRef = (0, import_react2.useRef)(false);
1621
+ const prevUrlRef = (0, import_react2.useRef)(null);
1622
+ const prevRemoteComponentContainerRef = (0, import_react2.useRef)(null);
1623
+ const unmountRef = (0, import_react2.useRef)(null);
1624
+ const prevNameRef = (0, import_react2.useRef)(void 0);
1625
+ (0, import_react2.useLayoutEffect)(() => {
1626
+ const shadowRootKey = `__remote_components_shadowroot_${keySuffix}`;
1627
+ return () => {
1628
+ delete globalThis[shadowRootKey];
1629
+ };
1630
+ }, [keySuffix]);
1631
+ (0, import_react2.useLayoutEffect)(() => {
1550
1632
  if (childrenRef.current.length > 0 && remoteComponent) {
1551
1633
  childrenRef.current.forEach((el) => {
1552
1634
  el.remove();
1553
1635
  });
1554
1636
  childrenRef.current = [];
1555
1637
  }
1556
- if (isolate !== false && typeof document !== "undefined" && (!shadowRoot || !shadowRoot.isConnected)) {
1557
- const self = globalThis;
1558
- const shadowRootKey = `__remote_components_shadowroot_${keySuffix}`;
1559
- let shadowRootElement = null;
1560
- const element = document.querySelector(
1561
- `[data-remote-component-id="shadowroot_${keySuffix}"]`
1562
- );
1563
- shadowRootElement = self[shadowRootKey] ?? element?.shadowRoot ?? null;
1564
- if (!shadowRootElement && element) {
1565
- try {
1566
- shadowRootElement = element.attachShadow({ mode });
1567
- self[shadowRootKey] = shadowRootElement;
1568
- } catch {
1569
- }
1570
- }
1571
- if (shadowRootElement) {
1572
- shadowRootElement.querySelectorAll("*:not(link)").forEach((node) => {
1573
- node.remove();
1574
- });
1575
- setShadowRoot(shadowRootElement);
1576
- }
1577
- }
1578
- }, [isolate, shadowRoot, remoteComponent, mode, keySuffix]);
1579
- (0, import_react.useLayoutEffect)(() => {
1638
+ }, [remoteComponent]);
1639
+ (0, import_react2.useLayoutEffect)(() => {
1580
1640
  if (shadowRoot && remoteComponent) {
1581
1641
  const resetStyles = shadowRoot.querySelectorAll(
1582
1642
  "style[data-remote-components-reset]"
@@ -1599,13 +1659,13 @@ function RemoteComponent({
1599
1659
  }
1600
1660
  }
1601
1661
  }, [shadowRoot, remoteComponent, name]);
1602
- (0, import_react.useEffect)(() => {
1662
+ (0, import_react2.useEffect)(() => {
1603
1663
  if (src && src !== prevSrcRef.current) {
1604
1664
  const previousSrc = prevSrcRef.current;
1605
1665
  const previousName = prevNameRef.current;
1606
1666
  prevSrcRef.current = src;
1607
1667
  onBeforeLoad?.(src);
1608
- (0, import_react.startTransition)(async () => {
1668
+ (0, import_react2.startTransition)(async () => {
1609
1669
  try {
1610
1670
  let html = getRemoteComponentHtml(
1611
1671
  htmlRef.current ?? (endTemplateRef.current?.previousElementSibling?.tagName === "div" ? endTemplateRef.current.previousElementSibling.innerHTML : "")
@@ -1943,7 +2003,7 @@ function RemoteComponent({
1943
2003
  runtime: prevIsRemoteComponentRef.current ? RUNTIME_SCRIPT : data?.runtime || RUNTIME_WEBPACK
1944
2004
  }) });
1945
2005
  const resetStyle = reset ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("style", { "data-remote-components-reset": "react", children: `:host { all: initial; }` }) : null;
1946
- const linksToRender = data?.links?.map((link) => /* @__PURE__ */ (0, import_react2.createElement)(
2006
+ const linksToRender = data?.links?.map((link) => /* @__PURE__ */ (0, import_react3.createElement)(
1947
2007
  "link",
1948
2008
  {
1949
2009
  ...link,