Skip to content

Commit 6f77b1b

Browse files
ryansolidClaude via Cursorcursoragent
authored
fix(ssr): the end of a streamed render waits for a suspended shell (#3771)
flushEnd flushed the serializer whenever the fragment registry was empty. A <Loading> fragment settling while the shell was still suspended (a lazy memo throwing NotReadyError above it, the router's flash-decode shape) emptied the registry before the root holes re-pulled, so the flush ran first. Seroval drops writes after flush and fires onDone once: - everything the re-pull serialized was missing from the hydration data, in both the piped and awaited forms; - the awaited form (renderToStream(...).then) completed with an empty document — onDone's doShell() bailed on the pending holes and completion ran anyway; - with an <Errored> holding the suspended subtree, the render never completed. Pending root holes now gate flushEnd. Both consumers' flush loops call it again once the holes resolve. (The serializeRootAssets guard was an earlier patch for one symptom of the same race.) Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent eaddda4 commit 6f77b1b

3 files changed

Lines changed: 158 additions & 1 deletion

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+
Fix the end of a streamed render racing a suspended shell. A `<Loading>` fragment settling while the shell was still suspended (a lazy memo throwing `NotReadyError` above it) could empty the fragment registry before the shell's root holes re-pulled, and the render flushed its serializer then: everything the re-pull serialized was dropped from the hydration data, and the awaited form (`renderToStream(...).then`) completed with an empty document — or, with an `<Errored>` holding the suspended subtree, never completed. The end of the response now waits for pending root holes.

‎packages/web/src/server.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2415,8 +2415,12 @@ export function renderToStream(code, options = {}) {
24152415
// END of the response (serializer flush → complete); shell and fragment
24162416
// flushing proceed normally around them.
24172417
let holds = 0;
2418+
// Pending root holes gate the end too: a fragment settling while the shell
2419+
// is suspended empties the registry before the holes re-pull, and a flush
2420+
// then would drop everything they serialize and complete the render with
2421+
// no shell. Both consumers' flush loops call this again once they resolve.
24182422
const flushEnd = () => {
2419-
if (!registry.size && !holds) {
2423+
if (!registry.size && !holds && !rootHoles) {
24202424
serializeRootAssets();
24212425
queue(() =>
24222426
queue(() => {
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
*/
4+
// The awaited render (`renderToStream(...).then`) under a shell suspension —
5+
// a lazy memo throwing NotReadyError on a request-scoped promise, the
6+
// router's flash-decode shape — with a <Loading> below whose content settles
7+
// before the suspension does.
8+
import { describe, expect, test } from "vitest";
9+
import { renderToStream, Errored, Loading } from "@solidjs/web";
10+
import type { JSX } from "@solidjs/web";
11+
import { createMemo, NotReadyError } from "solid-js";
12+
13+
const delay = (ms: number) => new Promise<void>(r => setTimeout(r, ms));
14+
15+
function settle(code: () => any, ms = 1000): Promise<string> {
16+
return Promise.race([
17+
new Promise<string>(resolve => renderToStream(code).then(resolve)),
18+
delay(ms).then(() => "TIMEOUT")
19+
]);
20+
}
21+
22+
const visibleText = (html: string) =>
23+
html.replace(/<script[\s\S]*?<\/script>/g, "").replace(/<[^>]*>/g, "");
24+
25+
function setup(shellMs: number, contentMs: number) {
26+
let decode: { done: boolean; promise: Promise<void> } | undefined;
27+
function Owner(props: { children: (read: () => string) => JSX.Element }) {
28+
const data = createMemo(
29+
() => {
30+
if (!decode) {
31+
const d = { done: false } as { done: boolean; promise: Promise<void> };
32+
d.promise = delay(shellMs).then(() => void (d.done = true));
33+
decode = d;
34+
}
35+
if (!decode.done) throw new NotReadyError(decode.promise);
36+
return "ready";
37+
},
38+
{ lazy: true, transparent: true } as any
39+
);
40+
return <>{props.children(data)}</>;
41+
}
42+
function Content() {
43+
const v = createMemo(async () => {
44+
await delay(contentMs);
45+
return "content body";
46+
});
47+
return <article>{v()}</article>;
48+
}
49+
function Reader(props: { read: () => string }) {
50+
return <p class="value">{props.read()}</p>;
51+
}
52+
function Document(props: { children?: JSX.Element }) {
53+
return (
54+
<html>
55+
<head />
56+
<body>{props.children}</body>
57+
</html>
58+
);
59+
}
60+
return { Owner, Content, Reader, Document };
61+
}
62+
63+
describe("a shell suspension's re-pull serializes into the document", () => {
64+
for (const mode of ["pipe", "then"] as const)
65+
test(mode, async () => {
66+
const { Owner, Content } = setup(10, 1);
67+
// Re-runs once the suspension settles; its async value serializes then.
68+
function Data() {
69+
const v = createMemo(async () => "shell-data-value");
70+
return <i>{v()}</i>;
71+
}
72+
function ShellData(props: { read: () => string }) {
73+
return <p>{props.read() && <Data />}</p>;
74+
}
75+
const code = () => (
76+
<Owner>
77+
{read => (
78+
<>
79+
<ShellData read={read} />
80+
<Loading fallback={<p class="fb">loading</p>}>
81+
<Content />
82+
</Loading>
83+
</>
84+
)}
85+
</Owner>
86+
);
87+
const html =
88+
mode === "then"
89+
? await settle(code)
90+
: await Promise.race([
91+
new Promise<string>(resolve => {
92+
const chunks: string[] = [];
93+
renderToStream(code).pipe({
94+
write: (c: string) => void chunks.push(c),
95+
end: () => resolve(chunks.join(""))
96+
});
97+
}),
98+
delay(1000).then(() => "TIMEOUT")
99+
]);
100+
expect(html).not.toBe("TIMEOUT");
101+
expect(visibleText(html)).toContain("shell-data-value");
102+
expect(html).toMatch(/<script[^>]*>[^<]*shell-data-value/);
103+
});
104+
});
105+
106+
for (const [label, shellMs, contentMs] of [
107+
["content settles first", 10, 1],
108+
["shell settles first", 1, 10]
109+
] as const)
110+
describe(`awaited render, a shell suspension above <Loading> (${label})`, () => {
111+
for (const errored of [true, false])
112+
for (const document of [true, false])
113+
test(`errored=${errored}, document=${document}`, async () => {
114+
const { Owner, Content, Reader, Document } = setup(shellMs, contentMs);
115+
const Body = (read: () => string) => (
116+
<>
117+
<Reader read={read} />
118+
<Loading fallback={<p class="fb">loading</p>}>
119+
<Content />
120+
</Loading>
121+
</>
122+
);
123+
const App = () => (
124+
<Owner>
125+
{read =>
126+
errored ? (
127+
<Errored fallback={err => <p>error: {String(err())}</p>}>{Body(read)}</Errored>
128+
) : (
129+
Body(read)
130+
)
131+
}
132+
</Owner>
133+
);
134+
const html = await settle(() =>
135+
document ? (
136+
<Document>
137+
<App />
138+
</Document>
139+
) : (
140+
<App />
141+
)
142+
);
143+
expect(html).not.toBe("TIMEOUT");
144+
expect(html).toContain('class="value">ready</p>');
145+
expect(visibleText(html)).toContain("content body");
146+
expect(visibleText(html)).not.toContain("loading");
147+
});
148+
});

0 commit comments

Comments
 (0)