Skip to content

Commit 3cffd86

Browse files
ryansolidclaude
andauthored
fix(web): async-mode SSR output independent of settle order (#4028) (#4161)
Once the renderToStream thenable is taken, document-mode hydration writes and the pending-promise stub batch are parked until the serializer flush and handed to seroval sorted by hydration key, so identical data gives identical bytes regardless of async latency. Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 Co-authored-by: Claude <[email protected]>
1 parent 32652bb commit 3cffd86

3 files changed

Lines changed: 155 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/web": patch
3+
---
4+
5+
Async-mode renders (`await renderToStream(...)`) now produce the same bytes for the same data, whatever order the async sources settle in (#4028). Once the thenable is taken, hydration writes and the pending-promise stub batch are held until the serializer flush and handed to seroval sorted by hydration key, so reference ids and resolution chunks no longer follow settle order. A body-digest ETag now matches across renders whose only difference was API latency. Streaming consumers (`pipe` / `pipeTo` / `readable`) and custom serializers/sinks are unchanged.

‎packages/web/src/server.ts‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,6 +2310,31 @@ export function renderToStream(code, options = {}) {
23102310
const canBatchStubs = !options.serializer && !options.sink;
23112311
let stubBatch = null;
23122312
const STUB_BATCH_KEY = "$B";
2313+
// Async mode (the thenable: the whole document resolves at once) has no
2314+
// reader before completion, so nothing is gained by handing seroval a
2315+
// value as it arrives — and handing it over in arrival order makes the
2316+
// bytes follow settle order: reference ids and resolution chunks are
2317+
// assigned as seroval meets values, so identical data with different
2318+
// latency produced different documents and a body-digest ETag missed
2319+
// revalidations (#4028). From the moment the thenable is taken (after the
2320+
// synchronous first pass, whose writes are already in render order), the
2321+
// document's writes park here and the stub batch stays parked too; both
2322+
// are handed over once, at the serializer flush, sorted by hydration key,
2323+
// by which point the drained registry has settled what they hold —
2324+
// seroval then meets every value, and every fulfillment, in key order.
2325+
let stableOrder = false;
2326+
let parkedWrites = null;
2327+
const byKey = (a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0);
2328+
const write = (id, value) => {
2329+
if (stableOrder && !dead) (parkedWrites ||= []).push([id, value]);
2330+
else serializer.write(id, value);
2331+
};
2332+
const flushParkedWrites = () => {
2333+
if (!parkedWrites) return;
2334+
const writes = parkedWrites.sort(byKey);
2335+
parkedWrites = null;
2336+
for (let i = 0; i < writes.length; i++) serializer.write(writes[i][0], writes[i][1]);
2337+
};
23132338
const flushStubBatch = () => {
23142339
if (!stubBatch) return;
23152340
const batch = stubBatch;
@@ -2320,7 +2345,9 @@ export function renderToStream(code, options = {}) {
23202345
return;
23212346
}
23222347
const obj = {};
2323-
for (const [id, p] of batch) obj[id] = p;
2348+
const entries = [...batch];
2349+
if (stableOrder) entries.sort(byKey);
2350+
for (const [id, p] of entries) obj[id] = p;
23242351
serializer.write(STUB_BATCH_KEY, obj);
23252352
pushTask(
23262353
`(b=>{for(var k in b)_$HY.r[k]=b[k];delete _$HY.r["${STUB_BATCH_KEY}"]})(_$HY.r["${STUB_BATCH_KEY}"])`
@@ -2528,6 +2555,7 @@ export function renderToStream(code, options = {}) {
25282555
// so the stub batch must land first. The task output still rides
25292556
// the shell snapshot — `tasks` accumulates until doShell reads it.
25302557
flushStubBatch();
2558+
flushParkedWrites();
25312559
serializer.flush();
25322560
})
25332561
); // double queue because of elsewhere
@@ -2874,7 +2902,7 @@ export function renderToStream(code, options = {}) {
28742902
if (p && typeof p === "object" && typeof p.then === "function") {
28752903
if (!firstFlushed && deferStream) {
28762904
blockingPromises.add(p);
2877-
p.then(d => serializer.write(id, d)).catch(e => serializer.write(id, e));
2905+
p.then(d => write(id, d)).catch(e => write(id, e));
28782906
return;
28792907
}
28802908
// Every pending promise handed to seroval joins the abandonment
@@ -2891,7 +2919,7 @@ export function renderToStream(code, options = {}) {
28912919
return;
28922920
}
28932921
}
2894-
serializer.write(id, p);
2922+
write(id, p);
28952923
},
28962924
escape: escape,
28972925
resolve: resolveSSRNode,
@@ -2947,7 +2975,7 @@ export function renderToStream(code, options = {}) {
29472975
});
29482976
if (canBatchStubs && !shellCompleted && !dead)
29492977
(stubBatch ||= new Map()).set(key + "_fr", p);
2950-
else serializer.write(key + "_fr", p);
2978+
else write(key + "_fr", p);
29512979
}
29522980
return (value, error, escaped) => {
29532981
if (registry.has(key)) {
@@ -3294,7 +3322,7 @@ export function renderToStream(code, options = {}) {
32943322
// drain-turn runway they had when writes happened at registration
32953323
// time, so a settled-before-shell record is visibly settled IN the
32963324
// shell (client boundaries branch on `.s === 1` at hydrate time).
3297-
flushStubBatch();
3325+
if (!stableOrder) flushStubBatch();
32983326
if (registry.size !== lastRegistrySize || drainTurn++ < MIN_DRAIN_TURNS) {
32993327
if (registry.size !== lastRegistrySize) drainTurn = 0;
33003328
lastRegistrySize = registry.size;
@@ -3436,6 +3464,9 @@ export function renderToStream(code, options = {}) {
34363464
// still open then. Consumers see an already-committed stub, which
34373465
// `createSSRResponse`/`commitEventResponse` pass through idempotently.
34383466
then(onFulfilled, onRejected) {
3467+
// Document mode only: a custom serializer/sink addresses records by
3468+
// key and keeps its own delivery order.
3469+
if (canBatchStubs) stableOrder = true;
34393470
const freezeHead = () => {
34403471
if (requestEvent && requestEvent.response)
34413472
commitResponseStub(requestEvent.response, { event: requestEvent });
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
*/
4+
// #4028: in async mode (`await renderToStream(...)`) identical data must give
5+
// identical bytes, whatever order the async sources settle in — a body-digest
6+
// ETag on a cached page otherwise misses revalidations that should be 304s.
7+
import { describe, expect, test } from "vitest";
8+
import { renderToStream } from "@solidjs/web";
9+
import { createMemo, Loading } from "solid-js";
10+
11+
const wait = (ms: number) => new Promise(r => setTimeout(r, ms));
12+
13+
function App(props: { delays: number[] }) {
14+
const items = props.delays.map((ms, i) =>
15+
createMemo(async () => {
16+
await wait(ms);
17+
return { id: i, name: "item-" + i, tags: ["t" + i, "shared"] };
18+
})
19+
);
20+
return (
21+
<div>
22+
{items.map(item => (
23+
<Loading fallback={<span>loading</span>}>
24+
<p>{item().name}</p>
25+
</Loading>
26+
))}
27+
</div>
28+
);
29+
}
30+
31+
describe("async-mode output is settle-order independent (#4028)", () => {
32+
test("identical data, different settle timing -> identical bytes", async () => {
33+
const fast = await renderToStream(() => <App delays={[5, 15, 25]} />);
34+
const slow = await renderToStream(() => <App delays={[25, 15, 5]} />);
35+
const mixed = await renderToStream(() => <App delays={[15, 25, 5]} />);
36+
expect(fast).toContain("item-2");
37+
expect(slow).toBe(fast);
38+
expect(mixed).toBe(fast);
39+
});
40+
41+
test("a shared promise payload settling out of order stays deterministic", async () => {
42+
function Shared(props: { delays: number[] }) {
43+
const shared = { s: "shared" };
44+
const items = props.delays.map((ms, i) =>
45+
createMemo(async () => {
46+
await wait(ms);
47+
return { i, shared };
48+
})
49+
);
50+
return (
51+
<ul>
52+
{items.map(item => (
53+
<Loading fallback="...">
54+
<li>{item().i}</li>
55+
</Loading>
56+
))}
57+
</ul>
58+
);
59+
}
60+
const a = await renderToStream(() => <Shared delays={[5, 20]} />);
61+
const b = await renderToStream(() => <Shared delays={[20, 5]} />);
62+
expect(b).toBe(a);
63+
});
64+
65+
test("values first serialized by a settled boundary's retry pass stay in key order", async () => {
66+
function Detail(props: { id: number; ms: number }) {
67+
const detail = createMemo(async () => {
68+
await wait(props.ms);
69+
return { detail: "d" + props.id };
70+
});
71+
return <i>{detail().detail}</i>;
72+
}
73+
function Nested(props: { delays: number[] }) {
74+
const items = props.delays.map((ms, i) =>
75+
createMemo(async () => {
76+
await wait(ms);
77+
return { i };
78+
})
79+
);
80+
return (
81+
<ul>
82+
{items.map((item, i) => (
83+
<Loading fallback="...">
84+
<li>
85+
{item().i}
86+
<Loading fallback="..">
87+
<Detail id={item().i} ms={props.delays[props.delays.length - 1 - i]} />
88+
</Loading>
89+
</li>
90+
</Loading>
91+
))}
92+
</ul>
93+
);
94+
}
95+
const a = await renderToStream(() => <Nested delays={[5, 20, 10]} />);
96+
const b = await renderToStream(() => <Nested delays={[20, 10, 5]} />);
97+
expect(a).toContain("d2");
98+
// Different delays shift which item waits on which, but the data is the
99+
// same: the bytes must be too.
100+
expect(b).toBe(a);
101+
});
102+
103+
test("a streaming consumer keeps delivering in settle order", async () => {
104+
const chunks: string[] = [];
105+
await new Promise<void>(resolve =>
106+
renderToStream(() => <App delays={[25, 5]} />).pipe({
107+
write: (c: string) => chunks.push(c),
108+
end: () => resolve()
109+
})
110+
);
111+
const html = chunks.join("");
112+
expect(html.indexOf("item-1")).toBeLessThan(html.indexOf("item-0"));
113+
});
114+
});

0 commit comments

Comments
 (0)