@solidjs/web 2.0.0-rc.14 → 2.0.0-rc.15

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.
@@ -1,6 +1,6 @@
1
1
  import * as core from 'solid-js';
2
2
  import { getOwner, onCleanup, createMemo, runWithOwner, untrack, createRenderEffect, createSignal, createOwner } from 'solid-js';
3
- import { insert } from '@solidjs/web';
3
+ import { installServerComponentHydration, insert } from '@solidjs/web';
4
4
  import { frameAddress, SINGLE_FLIGHT_HEADER, LIVE_WIRE, deserializeStream, getServerFunctionsCodec, createChunk, deliverFlightData, ERROR_HEADER, hasFlightMetadata, ChunkReader, configureServerFunctionsClient } from '@solidjs/web/server-functions/client';
5
5
 
6
6
  const ELEMENT_NODE = 1;
@@ -481,7 +481,7 @@ class FrameImpl {
481
481
  #syncSlots(root) {
482
482
  if (!this.#slots && !this.#outer) return;
483
483
  const found = new Map();
484
- if (root) collectSlots(root.firstChild, null, found, found);else this.#collectSlots(found, found);
484
+ if (root) collectSlots(root, found, found);else this.#collectSlots(found, found);
485
485
  let waiting = false;
486
486
  const R = tierLoads.regions?.r;
487
487
  const B = tierLoads.bind?.r;
@@ -603,7 +603,7 @@ class FrameImpl {
603
603
  return props;
604
604
  }
605
605
  #collectSlots(found, elements) {
606
- collectSlots(this.#element.firstChild, null, found, elements);
606
+ collectSlots(this.#element, found, elements);
607
607
  }
608
608
  #findPlaceholder(name, root) {
609
609
  return findPlaceholder((root || this.#element).firstChild, null, placeholderId(name));
@@ -870,26 +870,40 @@ function findPlaceholder(n, end, id) {
870
870
  }
871
871
  return null;
872
872
  }
873
- function collectSlots(n, end, out, elements) {
874
- while (n && n !== end) {
875
- const id = slotStartId(n);
876
- if (id !== null) {
873
+ function collectSlots(root, out, elements) {
874
+ let announced;
875
+ const bind = elements && (tierLoads.bind || !(announced = globalThis._$HY?.r?.["sc:tiers"]) || announced.includes("bind"));
876
+ const B = bind && tierLoads.bind?.r;
877
+ const w = root.ownerDocument.createTreeWalker(root, bind ? 0x81 : 0x80);
878
+ let n, p;
879
+ while (n = w.nextNode()) {
880
+ if (n.nodeType !== COMMENT_NODE) {
881
+ if (!n.hasAttribute(FRAME_ID_ATTR)) {
882
+ if (n.hasAttributes()) B ? B.positions(n, elements) : hasSlotMarker(n) && (elements.b = true);
883
+ continue;
884
+ }
885
+ p = n;
886
+ } else if (n.data.startsWith("slot:")) {
887
+ const id = slotStartId(n);
888
+ if (!id) continue;
889
+ if (!bind) {
890
+ for (p = n.parentNode; p !== root; p = p.parentNode) if (p.hasAttribute(FRAME_ID_ATTR)) break;
891
+ if (p !== root) continue;
892
+ }
877
893
  if (!out.has(id)) out.set(id, n);
878
- n = afterRange(n, id);
879
- continue;
880
- }
881
- if (elements !== undefined && isTextStart(n)) {
882
- const B = tierLoads.bind?.r;
883
- B ? B.text(n, elements) : elements.b = true;
884
- }
885
- if (n.nodeType === ELEMENT_NODE && !isFrameElement(n)) {
886
- if (elements !== undefined && n.hasAttributes()) {
887
- const B = tierLoads.bind?.r;
888
- B ? B.positions(n, elements) : hasSlotMarker(n) && (elements.b = true);
894
+ const end = findMarker(n, slotEnd(id));
895
+ if (end) {
896
+ w.currentNode = end;
897
+ continue;
889
898
  }
890
- collectSlots(n.firstChild, null, out, elements);
899
+ p = n.parentNode;
900
+ if (p === root) return;
901
+ } else {
902
+ if (bind && n.data.startsWith(SLOT_TEXT)) B ? B.text(n, elements) : elements.b = true;
903
+ continue;
891
904
  }
892
- n = n.nextSibling;
905
+ while (p.lastChild) p = p.lastChild;
906
+ w.currentNode = p;
893
907
  }
894
908
  }
895
909
  function eachInRange(start, key, cb) {
@@ -973,12 +987,12 @@ function morphNode(oldNode, newNode, claim, ranges, grafts) {
973
987
  oldNode.data = newNode.data;
974
988
  }
975
989
  }
976
- const afterRange = (start, id) => afterMarker(start, slotEnd(id));
977
- const afterText = start => afterMarker(start, SLOT_TEXT_END);
978
- function afterMarker(start, end) {
990
+ const afterRange = (start, id) => findMarker(start, slotEnd(id))?.nextSibling;
991
+ const afterText = start => findMarker(start, SLOT_TEXT_END)?.nextSibling;
992
+ function findMarker(start, end) {
979
993
  let n = start.nextSibling;
980
994
  while (n) {
981
- if (n.nodeType === COMMENT_NODE && n.data === end) return n.nextSibling;
995
+ if (n.nodeType === COMMENT_NODE && n.data === end) return n;
982
996
  n = n.nextSibling;
983
997
  }
984
998
  return null;
@@ -1455,7 +1469,7 @@ function createServerComponentHandler({
1455
1469
  const target = entry || host;
1456
1470
  const applied = applyFrameResponse(response, target, {
1457
1471
  as: address,
1458
- version
1472
+ version: frameId => frameId === address ? version : bump(frameId)
1459
1473
  }).catch(err => target.apply({
1460
1474
  type: "error",
1461
1475
  id: address,
@@ -1558,23 +1572,21 @@ function followAddress(host, frame, binding) {
1558
1572
  });
1559
1573
  }
1560
1574
  function landing(host, address, value, failed) {
1561
- let thrown = host.get(address)?.error;
1575
+ let frame, thrown;
1576
+ const errored = () => (frame = host.get(address))?.error !== undefined && (thrown === (thrown = frame.version) ? 1 : 2);
1577
+ errored();
1562
1578
  return createMemo(() => {
1563
1579
  failed();
1564
- const error = host.get(address)?.error;
1565
- if (error !== undefined && error !== thrown) {
1566
- thrown = error;
1567
- throw error;
1568
- }
1580
+ const state = errored();
1581
+ if (state > 1) throw frame.error;
1569
1582
  const wait = host.landing(address);
1570
1583
  if (!wait) return value;
1571
- const asked = error !== undefined ? reask(address) : undefined;
1572
- return (asked ? asked.then(() => wait) : wait).then(() => value);
1584
+ return (state ? reask(address, wait) : wait).then(() => value);
1573
1585
  });
1574
1586
  }
1575
- function reask(address) {
1587
+ function reask(address, wait) {
1576
1588
  const call = callFor(address);
1577
- return call && call.retry();
1589
+ return call ? call.retry().then(() => wait) : wait;
1578
1590
  }
1579
1591
  function failing() {
1580
1592
  const [failed, setFailed] = createSignal(0, {
@@ -1593,6 +1605,29 @@ function getFrameHost() {
1593
1605
  }
1594
1606
  return sharedHost;
1595
1607
  }
1608
+ function claimScope(el) {
1609
+ const registry = sharedConfig.registry;
1610
+ const buckets = new Map();
1611
+ const bucketOf = key => key.slice(0, key.lastIndexOf("-") + 1);
1612
+ const index = root => {
1613
+ if (registry) for (const n of root.querySelectorAll("[_hk]")) {
1614
+ const p = bucketOf(n.getAttribute("_hk"));
1615
+ const b = buckets.get(p);
1616
+ b ? b.push(n) : buckets.set(p, [n]);
1617
+ }
1618
+ };
1619
+ index(el);
1620
+ return {
1621
+ registry,
1622
+ gather(id) {
1623
+ for (const n of buckets.get(bucketOf(id)) || []) {
1624
+ const k = n.getAttribute("_hk");
1625
+ if (k.startsWith(id) && n.isConnected && !registry.has(k)) registry.set(k, n);
1626
+ }
1627
+ },
1628
+ index
1629
+ };
1630
+ }
1596
1631
  function claimRender(prefix, existing, render, scope, bound) {
1597
1632
  const sc = sharedConfig;
1598
1633
  if (!sc.hydrateWindow || !sc.registry) return render();
@@ -1800,7 +1835,10 @@ function findBoundaryElement(id) {
1800
1835
  boundaryIndex = new Map();
1801
1836
  if (typeof document !== "undefined" && document.body) indexBoundaries(document.body);
1802
1837
  }
1803
- return boundaryIndex.get(id);
1838
+ for (let n = 0;; n++) {
1839
+ const key = n ? id + "~" + (n + 1) : id;
1840
+ if (!claimedBoundaries.has(key)) return boundaryIndex.get(key);
1841
+ }
1804
1842
  }
1805
1843
  const arrivals = new Map();
1806
1844
  const FRAME_SELECTOR = `[${FRAME_ID_ATTR}]`;
@@ -1823,6 +1861,7 @@ function boundaryMayArrive() {
1823
1861
  return !hy.done || !!(hy.fr && hy.fr.pending());
1824
1862
  }
1825
1863
  function installRevealHook() {
1864
+ installServerComponentHydration();
1826
1865
  const hy = globalThis._$HY;
1827
1866
  if (!hy || hy.$sc || !hy.fr) return;
1828
1867
  hy.$sc = true;
@@ -1846,29 +1885,29 @@ function installRevealHook() {
1846
1885
  }
1847
1886
  function documentBoundary(host, id, props, binding) {
1848
1887
  installRevealHook();
1849
- const claimed = claimedBoundaries.has(id);
1850
- const el = !claimed ? findBoundaryElement(id) : undefined;
1888
+ const el = findBoundaryElement(id);
1851
1889
  if (el) return adoptBoundary(host, id, el, props, binding);
1852
- if (!claimed && boundaryMayArrive()) {
1890
+ if (!claimedBoundaries.has(id) && boundaryMayArrive()) {
1853
1891
  const owner = getOwner();
1854
1892
  let live = true;
1855
1893
  onCleanup(() => live = false);
1856
1894
  return createMemo(() => awaitBoundary(id).then(() => live && runWithOwner(owner, () => {
1857
- const node = claimedBoundaries.has(id) ? undefined : findBoundaryElement(id);
1895
+ const node = findBoundaryElement(id);
1858
1896
  return node ? adoptBoundary(host, id, node, props, binding) : boundaryComponent(host, id)(props, binding);
1859
1897
  })));
1860
1898
  }
1861
1899
  return boundaryComponent(host, id)(props, binding);
1862
1900
  }
1863
1901
  function adoptBoundary(host, id, el, props, binding) {
1864
- claimedBoundaries.add(id);
1902
+ const siteId = el.getAttribute(FRAME_ID_ATTR);
1903
+ claimedBoundaries.add(siteId);
1865
1904
  const address = binding ? contentAddress(binding()) : id;
1866
1905
  const appliedRecords = new Set();
1867
1906
  const drainRecords = () => {
1868
1907
  const hy = globalThis._$HY;
1869
1908
  if (!hy || !hy.r) return;
1870
1909
  pumpLiveChannel();
1871
- const slotPrefix = `sc:slot:${id}:`;
1910
+ const slotPrefix = `sc:slot:${siteId}:`;
1872
1911
  for (const key of Object.keys(hy.r)) {
1873
1912
  if (appliedRecords.has(key)) continue;
1874
1913
  if (key.startsWith(slotPrefix)) {
@@ -1886,7 +1925,7 @@ function adoptBoundary(host, id, el, props, binding) {
1886
1925
  } else apply(value);
1887
1926
  } else if (key.startsWith("sc:region:")) {
1888
1927
  const childId = key.slice("sc:region:".length);
1889
- if (childId.startsWith(id + ".")) {
1928
+ if (childId.startsWith(siteId + ".")) {
1890
1929
  appliedRecords.add(key);
1891
1930
  const val = hy.r[key];
1892
1931
  const apply = html => host.apply({
@@ -1900,10 +1939,12 @@ function adoptBoundary(host, id, el, props, binding) {
1900
1939
  }
1901
1940
  }
1902
1941
  };
1942
+ const scope = claimScope(el);
1903
1943
  const fr = globalThis._$HY?.fr;
1904
1944
  let frame;
1905
1945
  const unsubscribe = fr ? fr.subscribe((_fragId, parent) => {
1906
1946
  const inside = !!parent && el.contains(parent);
1947
+ inside && scope.index(parent);
1907
1948
  drainRecords();
1908
1949
  if (inside && frame) frame.apply({
1909
1950
  version: frame.version ?? 0,
@@ -1911,7 +1952,7 @@ function adoptBoundary(host, id, el, props, binding) {
1911
1952
  });
1912
1953
  }) : undefined;
1913
1954
  const applyLiveOp = op => {
1914
- if (op.fid && op.fid !== id) return;
1955
+ if (op.fid && op.fid !== siteId) return;
1915
1956
  host.apply({
1916
1957
  ...op,
1917
1958
  id: address,
@@ -1932,16 +1973,13 @@ function adoptBoundary(host, id, el, props, binding) {
1932
1973
  adopt: true,
1933
1974
  host,
1934
1975
  id: address,
1935
- slots: slotsFor(props, {
1936
- registry: sc.registry,
1937
- gather: sc.gather
1938
- }),
1976
+ slots: slotsFor(props, scope),
1939
1977
  ownerScope: boundaryScope(owner),
1940
1978
  reveal: revealSeam(owner),
1941
1979
  onApply,
1942
- hold: () => sc.isHydrationInProgress?.() ? runWithOwner(owner, () => untrack(() => sc.holdBoundary("sc:" + id))) : () => {},
1980
+ hold: () => sc.isHydrationInProgress?.() ? runWithOwner(owner, () => untrack(() => sc.holdBoundary("sc:" + siteId))) : () => {},
1943
1981
  ...{
1944
- claimScope: id
1982
+ claimScope: siteId
1945
1983
  }
1946
1984
  });
1947
1985
  if (binding) {
@@ -1987,9 +2025,8 @@ function installServerComponents(host = getFrameHost(), options) {
1987
2025
  intercept: ({
1988
2026
  id
1989
2027
  }) => {
1990
- if (claimedBoundaries.has(id)) return undefined;
1991
2028
  if (findBoundaryElement(id)) return true;
1992
- if (!boundaryMayArrive()) return undefined;
2029
+ if (claimedBoundaries.has(id) || !boundaryMayArrive()) return undefined;
1993
2030
  installRevealHook();
1994
2031
  return awaitBoundary(id);
1995
2032
  }
@@ -3,7 +3,7 @@ import { OBSERVE, DEV, getOwner, NoHydration, runWithOwner, createMemo, Hydratio
3
3
  import { isResponseEnvelope, renderToStream, createLiveHoles, CLAIMS_STREAM, CLAIMS_DOCUMENT, SLOT_FACE_DATA, SLOT_FACE_MARKUP, slotValue, SLOT_FACE_STREAM, isSlotValue, SLOT_VALUE } from '@solidjs/web';
4
4
  import { Feature, createStream, toCrossJSONStream } from 'seroval';
5
5
  import { AbortSignalPlugin, CustomEventPlugin, DOMExceptionPlugin, EventPlugin, FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin } from 'seroval-plugins/web';
6
- import { textDigest, frameAddress, getEventServerFunctionInvocation, armLiveBody, createEventChunk, createChunk, ChunkReader, serializeStream, guardFailures } from '@solidjs/web/server-functions/server';
6
+ import { frameAddress, getEventServerFunctionInvocation, ChunkReader, serializeStream, guardFailures, armLiveBody, createEventChunk, createChunk, textDigest } from '@solidjs/web/server-functions/server';
7
7
 
8
8
  const DESCEND = Symbol("descend");
9
9
  const CYCLE = {};
@@ -132,9 +132,7 @@ function toBorderForm(value, envelopeContainers) {
132
132
  }
133
133
  function isShareableIterable(value) {
134
134
  try {
135
- if (value.__SEROVAL_STREAM__ === true) return false;
136
- if (typeof ReadableStream !== "undefined" && value instanceof ReadableStream) return false;
137
- return typeof value[Symbol.asyncIterator] === "function";
135
+ return typeof value[Symbol.asyncIterator] === "function" && !(typeof ReadableStream !== "undefined" && value instanceof ReadableStream);
138
136
  } catch {
139
137
  return false;
140
138
  }
@@ -863,9 +861,6 @@ function createFrameSink(write, frame, have, hooks) {
863
861
  }
864
862
  };
865
863
  }
866
- function renderToFrameStream(code, options = {}) {
867
- return frameStream(() => code, options);
868
- }
869
864
  function renderServerComponent(component, options = {}) {
870
865
  return frameStream((sink, frame) => {
871
866
  const props = createSlotProps(sink, frame);
@@ -1389,6 +1384,36 @@ function frameElementOpen(id) {
1389
1384
  const escaped = sharedConfig.context ? sharedConfig.context.escape(String(id), true) : String(id);
1390
1385
  return `<${FRAME_TAG} ${FRAME_ID_ATTR}="${escaped}" style="display:contents">`;
1391
1386
  }
1387
+ const DOCUMENT_SITES = Symbol();
1388
+ function contextSites(ctx) {
1389
+ if (ctx[DOCUMENT_SITES]) return ctx[DOCUMENT_SITES];
1390
+ const proto = Object.getPrototypeOf(ctx);
1391
+ if (proto && proto[DOCUMENT_SITES]) return ctx[DOCUMENT_SITES] = proto[DOCUMENT_SITES];
1392
+ if (proto && typeof proto.escape === "function") return ctx[DOCUMENT_SITES] = contextSites(proto);
1393
+ return ctx[DOCUMENT_SITES] = {
1394
+ byOwner: new Map(),
1395
+ counts: Object.create(null)
1396
+ };
1397
+ }
1398
+ function mintSiteId(sites, fnId) {
1399
+ const n = sites.counts[fnId] || 0;
1400
+ sites.counts[fnId] = n + 1;
1401
+ return n === 0 ? fnId : fnId + "~" + (n + 1);
1402
+ }
1403
+ function documentSiteId(fnId) {
1404
+ const ctx = sharedConfig.context;
1405
+ if (!ctx) return fnId;
1406
+ const sites = contextSites(ctx);
1407
+ const owner = getOwner ? getOwner() : null;
1408
+ if (!owner) return mintSiteId(sites, fnId);
1409
+ let byFn = sites.byOwner.get(owner);
1410
+ if (!byFn) sites.byOwner.set(owner, byFn = new Map());
1411
+ const prev = byFn.get(fnId);
1412
+ if (prev) return prev;
1413
+ const siteId = mintSiteId(sites, fnId);
1414
+ byFn.set(fnId, siteId);
1415
+ return siteId;
1416
+ }
1392
1417
  const FRAME_ELEMENT_CLOSE = `</${FRAME_TAG}>`;
1393
1418
  function documentNeeds(name) {
1394
1419
  const ctx = sharedConfig.context;
@@ -1455,6 +1480,9 @@ function armDocumentLiveHoles(ctx) {
1455
1480
  ctx.serialize("sc:live", new ReadableStream({
1456
1481
  start(c) {
1457
1482
  channel = c;
1483
+ },
1484
+ cancel() {
1485
+ closed = true;
1458
1486
  }
1459
1487
  }));
1460
1488
  const push = op => {
@@ -1538,28 +1566,31 @@ function frameTransformDirectResult(value, {
1538
1566
  }) {
1539
1567
  if (typeof value !== "function") return value;
1540
1568
  const component = value;
1541
- const wrapped = props => [{
1542
- t: frameElementOpen(id)
1543
- },
1544
- serverOwned(() => {
1545
- const page = sharedConfig.context;
1546
- armDocumentLiveHoles(page);
1547
- if (tierUrls && page && page.live && !page.live.tierUrls) page.live.tierUrls = tierUrls;
1548
- const ctx = Object.create(page);
1549
- ctx.claims = CLAIMS_DOCUMENT;
1550
- ctx.frameId = id;
1551
- sharedConfig.context = ctx;
1552
- try {
1553
- const slotProps = createDocumentSlotProps(props, id);
1554
- const live = !!wrapped[LIVE_SOURCE];
1555
- if (live) documentNeeds("wire");
1556
- return serverComponentScope(() => component(slotProps), live);
1557
- } finally {
1558
- sharedConfig.context = page;
1559
- }
1560
- }), {
1561
- t: FRAME_ELEMENT_CLOSE
1562
- }];
1569
+ const wrapped = props => {
1570
+ const siteId = documentSiteId(id);
1571
+ return [{
1572
+ t: frameElementOpen(siteId)
1573
+ },
1574
+ serverOwned(() => {
1575
+ const page = sharedConfig.context;
1576
+ armDocumentLiveHoles(page);
1577
+ if (tierUrls && page && page.live && !page.live.tierUrls) page.live.tierUrls = tierUrls;
1578
+ const ctx = Object.create(page);
1579
+ ctx.claims = CLAIMS_DOCUMENT;
1580
+ ctx.frameId = siteId;
1581
+ sharedConfig.context = ctx;
1582
+ try {
1583
+ const slotProps = createDocumentSlotProps(props, siteId);
1584
+ const live = !!wrapped[LIVE_SOURCE];
1585
+ if (live) documentNeeds("wire");
1586
+ return serverComponentScope(() => component(slotProps), live);
1587
+ } finally {
1588
+ sharedConfig.context = page;
1589
+ }
1590
+ }), {
1591
+ t: FRAME_ELEMENT_CLOSE
1592
+ }];
1593
+ };
1563
1594
  wrapped[SERVER_COMPONENT] = id;
1564
1595
  wrapped[SERVER_COMPONENT_SOURCE] = component;
1565
1596
  wrapped[SERVER_COMPONENT_ADDRESS] = frameAddress(id, args);
@@ -1848,6 +1879,7 @@ function serverComponentResponse(component, options = {}, init = {}) {
1848
1879
  });
1849
1880
  let closed = false;
1850
1881
  let stopLive = null;
1882
+ let failed = false;
1851
1883
  const body = new ReadableStream({
1852
1884
  start(controller) {
1853
1885
  const end = () => {
@@ -1867,6 +1899,7 @@ function serverComponentResponse(component, options = {}, init = {}) {
1867
1899
  });
1868
1900
  stream.pipe({
1869
1901
  write(chunk) {
1902
+ if (chunk.type === "error" && !chunk.key) failed = true;
1870
1903
  if (closed) return;
1871
1904
  try {
1872
1905
  controller.enqueue(frame(JSON.stringify(chunk)));
@@ -1889,6 +1922,10 @@ function serverComponentResponse(component, options = {}, init = {}) {
1889
1922
  });
1890
1923
  }
1891
1924
  });
1925
+ if (failed) {
1926
+ headers.set("Cache-Control", "no-store");
1927
+ for (const name of [...headers.keys()]) if (name === "surrogate-control" || name.endsWith("cdn-cache-control")) headers.delete(name);
1928
+ }
1892
1929
  return new Response(body, {
1893
1930
  status: init.status || 200,
1894
1931
  headers
@@ -2105,4 +2142,4 @@ function asyncArg(value) {
2105
2142
  return value;
2106
2143
  }
2107
2144
 
2108
- export { FRAME_HAVE_BUDGET, FRAME_HAVE_HEADER, FRAME_STREAM_HEADER, FRAME_TIERS_HEADER, SERVER_COMPONENT_BOOTSTRAP, ServerComponentPlugin, asyncArg, createFrameSink, frameTransformDirectResult, frameTransformFlightResult, frameTransformResult, isFrameStreamResponse, renderServerComponent, renderToFrameStream, serverComponentResponse };
2145
+ export { FRAME_HAVE_BUDGET, FRAME_HAVE_HEADER, FRAME_STREAM_HEADER, FRAME_TIERS_HEADER, SERVER_COMPONENT_BOOTSTRAP, ServerComponentPlugin, asyncArg, frameTransformDirectResult, frameTransformFlightResult, frameTransformResult, isFrameStreamResponse };
@@ -1,9 +1,9 @@
1
1
  import { sharedConfig, ssrSanitizeError, creationStamp, ssrHandleError, runInServerComponentScope, shareAsyncIterable, getProjectionTrace } from 'solid-js/internal';
2
- import { NoHydration, getOwner, runWithOwner, createMemo, Hydration, createOwner } from 'solid-js';
2
+ import { getOwner, NoHydration, runWithOwner, createMemo, Hydration, createOwner } from 'solid-js';
3
3
  import { isResponseEnvelope, renderToStream, createLiveHoles, CLAIMS_STREAM, CLAIMS_DOCUMENT, SLOT_FACE_DATA, SLOT_FACE_MARKUP, slotValue, SLOT_FACE_STREAM, isSlotValue } from '@solidjs/web';
4
4
  import { Feature, createStream, toCrossJSONStream } from 'seroval';
5
5
  import { AbortSignalPlugin, CustomEventPlugin, DOMExceptionPlugin, EventPlugin, FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin } from 'seroval-plugins/web';
6
- import { textDigest, frameAddress, getEventServerFunctionInvocation, armLiveBody, createEventChunk, createChunk, ChunkReader, serializeStream, guardFailures } from '@solidjs/web/server-functions/server';
6
+ import { frameAddress, getEventServerFunctionInvocation, ChunkReader, serializeStream, guardFailures, armLiveBody, createEventChunk, createChunk, textDigest } from '@solidjs/web/server-functions/server';
7
7
 
8
8
  const DESCEND = Symbol("descend");
9
9
  const CYCLE = {};
@@ -132,9 +132,7 @@ function toBorderForm(value, envelopeContainers) {
132
132
  }
133
133
  function isShareableIterable(value) {
134
134
  try {
135
- if (value.__SEROVAL_STREAM__ === true) return false;
136
- if (typeof ReadableStream !== "undefined" && value instanceof ReadableStream) return false;
137
- return typeof value[Symbol.asyncIterator] === "function";
135
+ return typeof value[Symbol.asyncIterator] === "function" && !(typeof ReadableStream !== "undefined" && value instanceof ReadableStream);
138
136
  } catch {
139
137
  return false;
140
138
  }
@@ -853,9 +851,6 @@ function createFrameSink(write, frame, have, hooks) {
853
851
  }
854
852
  };
855
853
  }
856
- function renderToFrameStream(code, options = {}) {
857
- return frameStream(() => code, options);
858
- }
859
854
  function renderServerComponent(component, options = {}) {
860
855
  return frameStream((sink, frame) => {
861
856
  const props = createSlotProps(sink, frame);
@@ -1335,6 +1330,36 @@ function frameElementOpen(id) {
1335
1330
  const escaped = sharedConfig.context ? sharedConfig.context.escape(String(id), true) : String(id);
1336
1331
  return `<${FRAME_TAG} ${FRAME_ID_ATTR}="${escaped}" style="display:contents">`;
1337
1332
  }
1333
+ const DOCUMENT_SITES = Symbol();
1334
+ function contextSites(ctx) {
1335
+ if (ctx[DOCUMENT_SITES]) return ctx[DOCUMENT_SITES];
1336
+ const proto = Object.getPrototypeOf(ctx);
1337
+ if (proto && proto[DOCUMENT_SITES]) return ctx[DOCUMENT_SITES] = proto[DOCUMENT_SITES];
1338
+ if (proto && typeof proto.escape === "function") return ctx[DOCUMENT_SITES] = contextSites(proto);
1339
+ return ctx[DOCUMENT_SITES] = {
1340
+ byOwner: new Map(),
1341
+ counts: Object.create(null)
1342
+ };
1343
+ }
1344
+ function mintSiteId(sites, fnId) {
1345
+ const n = sites.counts[fnId] || 0;
1346
+ sites.counts[fnId] = n + 1;
1347
+ return n === 0 ? fnId : fnId + "~" + (n + 1);
1348
+ }
1349
+ function documentSiteId(fnId) {
1350
+ const ctx = sharedConfig.context;
1351
+ if (!ctx) return fnId;
1352
+ const sites = contextSites(ctx);
1353
+ const owner = getOwner ? getOwner() : null;
1354
+ if (!owner) return mintSiteId(sites, fnId);
1355
+ let byFn = sites.byOwner.get(owner);
1356
+ if (!byFn) sites.byOwner.set(owner, byFn = new Map());
1357
+ const prev = byFn.get(fnId);
1358
+ if (prev) return prev;
1359
+ const siteId = mintSiteId(sites, fnId);
1360
+ byFn.set(fnId, siteId);
1361
+ return siteId;
1362
+ }
1338
1363
  const FRAME_ELEMENT_CLOSE = `</${FRAME_TAG}>`;
1339
1364
  function documentNeeds(name) {
1340
1365
  const ctx = sharedConfig.context;
@@ -1401,6 +1426,9 @@ function armDocumentLiveHoles(ctx) {
1401
1426
  ctx.serialize("sc:live", new ReadableStream({
1402
1427
  start(c) {
1403
1428
  channel = c;
1429
+ },
1430
+ cancel() {
1431
+ closed = true;
1404
1432
  }
1405
1433
  }));
1406
1434
  const push = op => {
@@ -1484,28 +1512,31 @@ function frameTransformDirectResult(value, {
1484
1512
  }) {
1485
1513
  if (typeof value !== "function") return value;
1486
1514
  const component = value;
1487
- const wrapped = props => [{
1488
- t: frameElementOpen(id)
1489
- },
1490
- serverOwned(() => {
1491
- const page = sharedConfig.context;
1492
- armDocumentLiveHoles(page);
1493
- if (tierUrls && page && page.live && !page.live.tierUrls) page.live.tierUrls = tierUrls;
1494
- const ctx = Object.create(page);
1495
- ctx.claims = CLAIMS_DOCUMENT;
1496
- ctx.frameId = id;
1497
- sharedConfig.context = ctx;
1498
- try {
1499
- const slotProps = createDocumentSlotProps(props, id);
1500
- const live = !!wrapped[LIVE_SOURCE];
1501
- if (live) documentNeeds("wire");
1502
- return serverComponentScope(() => component(slotProps), live);
1503
- } finally {
1504
- sharedConfig.context = page;
1505
- }
1506
- }), {
1507
- t: FRAME_ELEMENT_CLOSE
1508
- }];
1515
+ const wrapped = props => {
1516
+ const siteId = documentSiteId(id);
1517
+ return [{
1518
+ t: frameElementOpen(siteId)
1519
+ },
1520
+ serverOwned(() => {
1521
+ const page = sharedConfig.context;
1522
+ armDocumentLiveHoles(page);
1523
+ if (tierUrls && page && page.live && !page.live.tierUrls) page.live.tierUrls = tierUrls;
1524
+ const ctx = Object.create(page);
1525
+ ctx.claims = CLAIMS_DOCUMENT;
1526
+ ctx.frameId = siteId;
1527
+ sharedConfig.context = ctx;
1528
+ try {
1529
+ const slotProps = createDocumentSlotProps(props, siteId);
1530
+ const live = !!wrapped[LIVE_SOURCE];
1531
+ if (live) documentNeeds("wire");
1532
+ return serverComponentScope(() => component(slotProps), live);
1533
+ } finally {
1534
+ sharedConfig.context = page;
1535
+ }
1536
+ }), {
1537
+ t: FRAME_ELEMENT_CLOSE
1538
+ }];
1539
+ };
1509
1540
  wrapped[SERVER_COMPONENT] = id;
1510
1541
  wrapped[SERVER_COMPONENT_SOURCE] = component;
1511
1542
  wrapped[SERVER_COMPONENT_ADDRESS] = frameAddress(id, args);
@@ -1794,6 +1825,7 @@ function serverComponentResponse(component, options = {}, init = {}) {
1794
1825
  });
1795
1826
  let closed = false;
1796
1827
  let stopLive = null;
1828
+ let failed = false;
1797
1829
  const body = new ReadableStream({
1798
1830
  start(controller) {
1799
1831
  const end = () => {
@@ -1813,6 +1845,7 @@ function serverComponentResponse(component, options = {}, init = {}) {
1813
1845
  });
1814
1846
  stream.pipe({
1815
1847
  write(chunk) {
1848
+ if (chunk.type === "error" && !chunk.key) failed = true;
1816
1849
  if (closed) return;
1817
1850
  try {
1818
1851
  controller.enqueue(frame(JSON.stringify(chunk)));
@@ -1835,6 +1868,10 @@ function serverComponentResponse(component, options = {}, init = {}) {
1835
1868
  });
1836
1869
  }
1837
1870
  });
1871
+ if (failed) {
1872
+ headers.set("Cache-Control", "no-store");
1873
+ for (const name of [...headers.keys()]) if (name === "surrogate-control" || name.endsWith("cdn-cache-control")) headers.delete(name);
1874
+ }
1838
1875
  return new Response(body, {
1839
1876
  status: init.status || 200,
1840
1877
  headers
@@ -2051,4 +2088,4 @@ function asyncArg(value) {
2051
2088
  return value;
2052
2089
  }
2053
2090
 
2054
- export { FRAME_HAVE_BUDGET, FRAME_HAVE_HEADER, FRAME_STREAM_HEADER, FRAME_TIERS_HEADER, SERVER_COMPONENT_BOOTSTRAP, ServerComponentPlugin, asyncArg, createFrameSink, frameTransformDirectResult, frameTransformFlightResult, frameTransformResult, isFrameStreamResponse, renderServerComponent, renderToFrameStream, serverComponentResponse };
2091
+ export { FRAME_HAVE_BUDGET, FRAME_HAVE_HEADER, FRAME_STREAM_HEADER, FRAME_TIERS_HEADER, SERVER_COMPONENT_BOOTSTRAP, ServerComponentPlugin, asyncArg, frameTransformDirectResult, frameTransformFlightResult, frameTransformResult, isFrameStreamResponse };