Skip to content

Commit 9408b9d

Browse files
ryansolidClaude via Cursorcursoragent
committed
fix(ssr): a shell suspension above <Errored> keeps its <Loading> content and its owners
Two failures when the shell suspends inside an <Errored> subtree that is not under a <Loading> (the router's flash-decode read after a no-JS post, a lazy() route with no <Loading> between): - <Errored> holds its children, placeholders included, in its retry state, so the whole subtree is a pending root hole. A <Loading> below it whose content settled first inlined into a shell that did not yet hold its placeholder: the replace was a no-op, the fragment left the registry, and the fallback shipped with nothing to swap it. Fragments settling in that window are now parked and spliced in when a root-hole re-pull lands their placeholder. - ssrScope unwraps accessor chains in place. When the accessor that suspended was returned by the hole's expression ({props.children} resolving to the <Errored>), the whole scope thunk became the retry, so every pass re-read the expression and re-created every component above the boundary; one owning its pending source never converged. The retry now resumes the accessor that suspended, with the scope's counter where it was. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 6be6c51 commit 9408b9d

5 files changed

Lines changed: 358 additions & 4 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"solid-js": patch
3+
"@solidjs/web": patch
4+
---
5+
6+
Fix two SSR failures when the shell suspends above an `<Errored>` (a lazy memo throwing `NotReadyError`, the router's flash-decode shape). A `<Loading>` below the boundary whose content settled before the shell's suspension did lost its content: the fallback shipped and nothing swapped it, because the fragment inlined into a shell that did not yet hold its placeholder. And a hole that reached a suspension through a returned accessor (`{props.children}` resolving to an `<Errored>` whose subtree is pending — a `lazy()` route with no `<Loading>` between, for one) re-read its expression on every retry, re-creating every component above the boundary — state and pending sources included, so a component owning its pending source never converged. The retry now resumes the accessor that suspended.

‎packages/solid/src/server/signals.ts‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,20 +620,40 @@ export function ssrScope<T>(fn: () => T): () => unknown {
620620
while (parent._transparent && parent._parent) parent = parent._parent;
621621
const prefix = parent._scopeSlot >= 0 ? materializeId(parent)! : parent.id!;
622622
const slot = parent._childCount++;
623+
// An accessor the unwrap below reached that suspended, with the scope's
624+
// counter at its call: the retry pull resumes there. Re-running `fn`
625+
// re-reads the hole's expression, and a `props.children` getter creates
626+
// its components afresh — the client reads it once and unwraps in an
627+
// inner effect.
628+
let resume: (() => unknown) | undefined;
629+
let resumeCount = 0;
623630
return () => {
624631
const prevId = parent.id;
625632
const prevSlot = parent._scopeSlot;
626633
const prevCount = parent._childCount;
627634
parent.id = prefix;
628635
parent._scopeSlot = slot;
629-
parent._childCount = 0;
636+
parent._childCount = resume ? resumeCount : 0;
630637
try {
631-
let v: unknown = fn();
638+
let v: unknown = resume || fn();
639+
resume = undefined;
632640
// Unwrap accessor chains in-scope: reading a memo / component thunk can
633641
// create owners and allocate ids, which must land under the hole scope
634642
// just like the client's inner unwrapping effect (transparent, so it
635643
// shares the outer insert effect's scope).
636-
while (typeof v === "function") v = (v as () => unknown)();
644+
while (typeof v === "function") {
645+
const next = v as () => unknown;
646+
const count = parent._childCount;
647+
try {
648+
v = next();
649+
} catch (err) {
650+
if (err instanceof NotReadyError) {
651+
resume = next;
652+
resumeCount = count;
653+
}
654+
throw err;
655+
}
656+
}
637657
return v;
638658
} finally {
639659
parent.id = prevId;

‎packages/web/src/server.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2637,6 +2637,16 @@ export function renderToStream(code, options = {}) {
26372637
}
26382638
let rootHoles = null;
26392639
let nextHoleId = 0;
2640+
// Markup of fragments that settled pre-flush while their placeholder was
2641+
// still inside a pending root hole — an <Errored> holding its children
2642+
// over a shell suspension keeps the boundary's placeholder in its own
2643+
// retry state, out of `html`. Spliced in when a root-hole re-pull lands it.
2644+
let parkedInlines = null;
2645+
function inlineFragment(key, value) {
2646+
if (rootHoles && !html.includes(`<template id="pl-${key}">`)) {
2647+
(parkedInlines ||= new Map()).set(key, value);
2648+
} else html = replacePlaceholder(html, key, value);
2649+
}
26402650
let buffer = {
26412651
write(payload) {
26422652
tmp += payload;
@@ -2876,7 +2886,7 @@ export function renderToStream(code, options = {}) {
28762886
// Head registrations stay pending: a boundary that inlines into
28772887
// the shell commits with the shell flush (its key is no longer
28782888
// a pending fragment, so renderShellHead picks them up).
2879-
queue(() => (html = replacePlaceholder(html, key, value !== undefined ? value : "")));
2889+
queue(() => inlineFragment(key, value !== undefined ? value : ""));
28802890
serializeFragmentAssets(key, tracking.boundaryModules, context);
28812891
item.resolve(error);
28822892
} else {
@@ -3008,6 +3018,13 @@ export function renderToStream(code, options = {}) {
30083018
for (const p of res.p) blockingPromises.add(p);
30093019
}
30103020
}
3021+
if (parkedInlines) {
3022+
for (const [key, value] of parkedInlines) {
3023+
if (!html.includes(`<template id="pl-${key}">`)) continue;
3024+
html = replacePlaceholder(html, key, value);
3025+
parkedInlines.delete(key);
3026+
}
3027+
}
30113028
if (pending.length) {
30123029
rootHoles = pending;
30133030
return false;
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
*/
4+
import { describe, expect, test } from "vitest";
5+
import { renderToStream, Errored, Loading } from "@solidjs/web";
6+
import type { JSX } from "@solidjs/web";
7+
import { createMemo, lazy, NotReadyError } from "solid-js";
8+
9+
const delay = (ms: number) => new Promise(r => setTimeout(r, ms));
10+
11+
function settle(code: () => any, options?: object, ms = 1000): Promise<string> {
12+
return Promise.race([
13+
new Promise<string>(resolve => renderToStream(code, options).then(resolve)),
14+
delay(ms).then(() => "TIMEOUT")
15+
]);
16+
}
17+
18+
type Decode = { done: boolean; promise?: Promise<void> };
19+
20+
function setup(scope: "instance" | "request") {
21+
const counts = { owner: 0 };
22+
let requestDecode: Decode | undefined;
23+
// A stateful ancestor with the router's flash-decode shape: a transparent
24+
// lazy memo that throws NotReadyError carrying a cached promise.
25+
function Owner(props: { children: (read: () => string) => JSX.Element }) {
26+
counts.owner++;
27+
let instanceDecode: Decode | undefined;
28+
const data = createMemo(
29+
() => {
30+
let decode = scope === "instance" ? instanceDecode : requestDecode;
31+
if (!decode) {
32+
const d: Decode = { done: false };
33+
d.promise = delay(5).then(() => void (d.done = true));
34+
decode = d;
35+
if (scope === "instance") instanceDecode = d;
36+
else requestDecode = d;
37+
}
38+
if (!decode.done) throw new NotReadyError(decode.promise);
39+
return "ready";
40+
},
41+
{ lazy: true, transparent: true } as any
42+
);
43+
return <>{props.children(data)}</>;
44+
}
45+
function BodyReader(props: { read: () => string }) {
46+
const value = createMemo(() => props.read());
47+
return <p class="value">{value()}</p>;
48+
}
49+
function TemplateReader(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 { counts, Owner, BodyReader, TemplateReader, Document };
61+
}
62+
63+
describe("a shell suspension beside an async <Loading> child", () => {
64+
function Async() {
65+
const value = createMemo(async () => {
66+
await delay(5);
67+
return "async-done";
68+
});
69+
return <p class="async">{value()}</p>;
70+
}
71+
for (const where of ["shell", "loading"] as const)
72+
for (const errored of [true, false])
73+
for (const document of [true, false])
74+
test(`read in ${where}, errored=${errored}, document=${document}`, async () => {
75+
const { Owner, BodyReader, Document } = setup("request");
76+
const Body = (read: () => string) => (
77+
<>
78+
{where === "shell" && <BodyReader read={read} />}
79+
<Loading fallback="loading">
80+
{where === "loading" && <BodyReader read={read} />}
81+
<Async />
82+
</Loading>
83+
</>
84+
);
85+
const App = () => (
86+
<Owner>
87+
{read =>
88+
errored ? (
89+
<Errored fallback={err => <p>error: {String(err())}</p>}>{Body(read)}</Errored>
90+
) : (
91+
Body(read)
92+
)
93+
}
94+
</Owner>
95+
);
96+
const html = await settle(() =>
97+
document ? (
98+
<Document>
99+
<App />
100+
</Document>
101+
) : (
102+
<App />
103+
)
104+
);
105+
expect(html).toContain('class="value">ready</p>');
106+
expect(html).toContain("async-done");
107+
});
108+
});
109+
110+
describe("a shell suspension under <Errored>", () => {
111+
for (const scope of ["instance", "request"] as const)
112+
for (const reader of ["body", "template"] as const)
113+
for (const errored of [true, false])
114+
for (const document of [true, false])
115+
test(`${scope} source, ${reader} read, errored=${errored}, document=${document}`, async () => {
116+
const { counts, Owner, BodyReader, TemplateReader, Document } = setup(scope);
117+
const Reader = reader === "body" ? BodyReader : TemplateReader;
118+
const App = () => (
119+
<Owner>
120+
{read =>
121+
errored ? (
122+
<Errored fallback={err => <p>error: {String(err())}</p>}>
123+
<Reader read={read} />
124+
<Loading fallback="loading">tail</Loading>
125+
</Errored>
126+
) : (
127+
<>
128+
<Reader read={read} />
129+
<Loading fallback="loading">tail</Loading>
130+
</>
131+
)
132+
}
133+
</Owner>
134+
);
135+
const html = await settle(() =>
136+
document ? (
137+
<Document>
138+
<App />
139+
</Document>
140+
) : (
141+
<App />
142+
)
143+
);
144+
expect(html).toContain('class="value">ready</p>');
145+
expect({ owner: counts.owner }).toEqual({ owner: 1 });
146+
});
147+
});
148+
149+
describe("a lazy route under <Errored> with no <Loading>", () => {
150+
for (const document of [true, false])
151+
test(`document=${document}`, async () => {
152+
const counts = { layout: 0 };
153+
const Route = lazy(
154+
() => delay(5).then(() => ({ default: () => <p class="route">route body</p> })),
155+
undefined,
156+
"./Route.tsx"
157+
);
158+
function Layout(props: { children?: JSX.Element }) {
159+
counts.layout++;
160+
return <>{props.children}</>;
161+
}
162+
const App = () => (
163+
<Layout>
164+
<Errored fallback={err => <p>error: {String(err())}</p>}>
165+
<Route />
166+
</Errored>
167+
</Layout>
168+
);
169+
const { Document } = setup("request");
170+
const html = await settle(
171+
() =>
172+
document ? (
173+
<Document>
174+
<App />
175+
</Document>
176+
) : (
177+
<App />
178+
),
179+
{ manifest: { "./Route.tsx": { file: "assets/route.js" } } }
180+
);
181+
expect(html).toContain('class="route">route body</p>');
182+
expect(counts).toEqual({ layout: 1 });
183+
});
184+
});

0 commit comments

Comments
 (0)