Skip to content

Commit b68a907

Browse files
ryansolidClaude via Cursorcursoragent
committed
fix(solid): shell nodes outside pending streamed boundaries update on client writes
Snapshot hydration exists so everything outside an incomplete boundary can hydrate and move on, while each pending boundary later hydrates against its server snapshot. The latch contradicted that: after the root pass, a serialized node outside any pending streamed boundary kept re-adopting its server value on every client write until the WHOLE page finished hydrating. A navigation while a slow boundary streamed in left async shell data (layout titles, query results) on the old location next to sync memos already on the new one. Its re-entry took the latch's divergence path, which arms a takeover gate that, with no hydration scope open between chunks, only flips at hydration end. Streamed boundary owners are now marked pending (`_hp`) from registration until their release (resume or disposal). A latched node's rerun computes when it is not in the claim in progress (the root pass, or the resuming boundary's own subtree, `isClaiming()`) and no pending boundary holds it. Nodes under a pending boundary, in-claim reruns, and hybrid nodes keep the latch as before. Tests: - packages/solid/test/hydration-latch.spec.ts: the latch itself. - nav-before-resume (replayed router-shaped stream): navigates while two boundaries are pending; the shell updates, the pending boundaries still resume, hydration ends once. It also pins why Solid Router's former `if (registry && !done) done = true` at the write must go: ending hydration there misclaims a still-pending boundary. - write-before-resume (replayed stream): one source read in the shell and inside two pending boundaries, written after the root pass. The shell updates at once; each boundary resumes against the server snapshot with no hydration diagnostics, then converges to the client's value (the async one holding its whole content on the snapshot until its refetch lands). Size: +105 B minified on each hydrating scenario. Caps raised under a Size-Exception: hydrating + stores 30.74 -> 30.79 KB, base page 45.91 -> 45.95 KB, live page 50.09 -> 50.12 KB. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 1f9d41a commit b68a907

13 files changed

Lines changed: 642 additions & 5 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Fix: after the root hydration pass, a node that adopted its serialized server value and sits outside every still-pending streamed boundary now updates on a client write (a navigation or a cache write while a slow boundary streams in) instead of re-adopting its server value until the whole page finished hydrating. The wait contradicted the snapshot design: snapshots exist so everything outside an incomplete boundary hydrates and moves on, while each pending boundary later hydrates against its server snapshot and then catches up to the client's newer state. Streamed boundary owners are now marked pending until they resume or are disposed; nodes under a pending boundary, and nodes in the claim in progress (the root pass, or the resuming boundary's own subtree), keep the latch as before.

‎packages/solid/src/client/hydration.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ let _snapshotRootOwner: Owner | null = null;
245245
// The boundary owner whose resume window is open (null during a root pass,
246246
// which claims everything). Reached as `sharedConfig.isClaiming`.
247247
let _claimOwner: Owner | null = null;
248-
249248
function isClaiming(): boolean {
250249
if (!_claimOwner) return true;
251250
let owner: Owner | null = getOwner();
@@ -551,7 +550,16 @@ function readSerializedOrCompute(compute: (prev: any) => any, prev: any, options
551550
// rules out), and the hybrid wrappers re-enter this path for every later
552551
// run of such a node, not only on divergence.
553552
if (latchedOnce.has(o)) {
554-
if (options?.ssrSource !== "hybrid") armLiveTakeover(o);
553+
// A rerun outside the claim in progress by a node no pending boundary
554+
// holds: its section's hydration is over, so the rerun answers a client
555+
// write and computes, rather than re-adopting the server value until the
556+
// page's last boundary resumes.
557+
if (options?.ssrSource !== "hybrid") {
558+
let p: Owner | null = o;
559+
while (p && !(p as any)._hp) p = p._parent;
560+
if (!p && !(sharedConfig.hydrating && isClaiming())) return compute(prev);
561+
armLiveTakeover(o);
562+
}
555563
} else latchedOnce.add(o);
556564
return readHydratedValue(
557565
sharedConfig.load!(o.id!),
@@ -2557,6 +2565,7 @@ function initBoundaryResume(
25572565
id: string
25582566
): [trigger: () => void, resume: (shouldHydrate?: boolean) => void, release: () => boolean] {
25592567
_pendingBoundaries++;
2568+
(o as any)._hp = 1;
25602569
// Capture the current root's registry/gather pair for this boundary's
25612570
// late resume (#2917). Runs while the registering root's globals are live:
25622571
// during its hydrate() pass, or — for nested streamed boundaries — inside
@@ -2571,6 +2580,7 @@ function initBoundaryResume(
25712580
if (released) return false;
25722581
released = true;
25732582
_pendingBoundaries--;
2583+
(o as any)._hp = 0;
25742584
sharedConfig.boundaryScopes?.delete(id);
25752585
// Retire the fragment claim (see claimFragment): after this boundary
25762586
// resumes or is disposed, a late swap must be held rather than landing
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @vitest-environment jsdom
3+
*
4+
* The serialized-value latch after the root hydration pass: a node that
5+
* adopted its server value and sits outside every pending streamed boundary
6+
* recomputes on a client write; a node under a boundary still pending keeps
7+
* the server value until that boundary resumes.
8+
*/
9+
import { afterEach, expect, test } from "vitest";
10+
import { createRoot, flush } from "@solidjs/signals";
11+
import {
12+
enableHydration,
13+
sharedConfig,
14+
createMemo,
15+
createSignal
16+
} from "../src/client/hydration.js";
17+
import { Loading } from "../src/client/flow.js";
18+
19+
enableHydration();
20+
21+
function startHydration(data: Record<string, any>) {
22+
(globalThis as any)._$HY = {
23+
modules: {},
24+
loading: {},
25+
r: data,
26+
events: [],
27+
completed: new WeakSet()
28+
};
29+
sharedConfig.hydrating = true;
30+
(sharedConfig as any).has = (id: string) => id in data;
31+
(sharedConfig as any).load = (id: string) => data[id];
32+
(sharedConfig as any).gather = () => {};
33+
(sharedConfig as any).registry = new Map();
34+
}
35+
36+
function stopHydration() {
37+
sharedConfig.hydrating = false;
38+
(sharedConfig as any).has = undefined;
39+
(sharedConfig as any).load = undefined;
40+
(sharedConfig as any).gather = undefined;
41+
delete (globalThis as any)._$HY;
42+
}
43+
44+
afterEach(() => stopHydration());
45+
46+
test("after the root pass, a write reaches a shell node outside the pending boundary", () => {
47+
// `t1` is the streamed boundary, still pending; `t0` the shell memo and
48+
// `t10` a memo its fallback creates, both serialized as 100.
49+
const fr: any = new Promise(() => {});
50+
fr.s = 0;
51+
startHydration({ t0: 100, t1_fr: fr, t10: 100 });
52+
let shell!: () => number, held!: () => number, setS!: (v: number) => void;
53+
const dispose = createRoot(
54+
d => {
55+
const [s, set] = createSignal(1, { ownedWrite: true });
56+
setS = set;
57+
shell = createMemo(() => s());
58+
Loading({
59+
get fallback() {
60+
held = createMemo(() => s());
61+
return "loading";
62+
},
63+
get children() {
64+
return (() => s()) as any;
65+
}
66+
});
67+
return d;
68+
},
69+
{ id: "t" }
70+
);
71+
flush();
72+
expect([shell(), held()]).toEqual([100, 100]);
73+
// hydrate()'s `finally`: the root pass is over, the boundary still pending.
74+
sharedConfig.hydrating = false;
75+
76+
setS(2);
77+
flush();
78+
expect(shell()).toBe(2);
79+
expect(held()).toBe(100);
80+
expect(sharedConfig.isHydrationInProgress!()).toBe(true);
81+
dispose();
82+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"shell": "<main _hk=3><h1>title:/a</h1><h2>label:/a</h2><!--$--><template id=\"pl-6\"></template><p _hk=60>side-loading</p><!--pl-6--><!--/--><!--$--><section _hk=91><template id=\"pl-92\"></template><i _hk=920>a-loading</i><!--pl-92--></section><!--/--></main><script>(self.$R=self.$R||{})[\"\"]=[];_$HY.r[\"lib:side\"]=($R[0]={n:42});_$HY.r[\"$B\"]=($R[1]={0:$R[2]=($R[3]=($R[4]=() => {\n\tconst resolver = {\n\t\tp: 0,\n\t\ts: 0,\n\t\tf: 0\n\t};\n\tresolver.p = new Promise((resolve, reject) => {\n\t\tresolver.s = resolve;\n\t\tresolver.f = reject;\n\t});\n\treturn resolver;\n})()).p,90:$R[5]=($R[6]=$R[4]()).p,6000:$R[7]=($R[8]=$R[4]()).p,\"6_fr\":$R[9]=($R[10]=$R[4]()).p,\"92_fr\":$R[11]=($R[12]=$R[4]()).p});(b=>{for(var k in b)_$HY.r[k]=b[k];delete _$HY.r[\"$B\"]})(_$HY.r[\"$B\"]);($R[13]=(resolver, data) => {\n\tresolver.s(data);\n\tresolver.p.s = 1;\n\tresolver.p.v = data;\n})($R[3],\"title:/a\");</script>",
3+
"chunks": [
4+
"<template id=\"92\"><b _hk=92000>a-data</b></template><script>$R[13]($R[6],\"a-data\");$df(\"92\");function $df(e){return _$HY.f?_$HY.f(e):$dfr(e)}function $dfr(e,n,o,t){if(!(n=document.getElementById(e)))return 0;if(!(o=document.getElementById(\"pl-\"+e)))return(_$HY.dq=_$HY.dq||{})[e]=1,0;for(;o&&(8!==o.nodeType||o.nodeValue!==\"pl-\"+e);)t=o.nextSibling,o.remove(),o=t;t=o.parentNode,o.replaceWith(n.content),n.remove(),(_$HY.v=_$HY.v||{})[e]=1,_$HY.fe(e,t),_$HY.hp&&_$HY.hp[e]&&($dh(_$HY.hp[e]),delete _$HY.hp[e]),$dfd();return 1}function $dfl(e,o,n){if(!(o=document.getElementById(\"pl-\"+e)))return(_$HY.dlq=_$HY.dlq||{})[e]=1,0;if(o._$fl)return 1;for(n=o.nextSibling;n;){if(8===n.nodeType&&n.nodeValue===\"pl-\"+e){o.parentNode&&o.parentNode.insertBefore(o.content.cloneNode(!0),n),o._$fl=1,$dfd();return 1}n=n.nextSibling}return 0}function $dflj(e,i){for(i=0;i<e.length;i++)$dfl(e[i])}function $dfd(e,i){if(e=_$HY.dq){_$HY.dq=0;for(i in e)$df(i)}if(e=_$HY.dlq){_$HY.dlq=0;for(i in e)$dfl(i)}}function $dfs(e,c,d){(_$HY.sc=_$HY.sc||{})[e]=c,d&&((_$HY.sd=_$HY.sd||{})[e]=1)}function $dfg(e,g,i,k){if(!(g=_$HY.sg&&_$HY.sg[e]))return;for(i=0;i<g.length;i++)if(_$HY.sc&&_$HY.sc[g[i]]>0)return;for(i=0;i<g.length;i++)k=g[i],delete _$HY.sg[k],$df(k)}function $dfc(e){if(--_$HY.sc[e]<=0){delete _$HY.sc[e],_$HY.sg&&_$HY.sg[e]?$dfg(e):!(_$HY.sd&&_$HY.sd[e])&&$df(e);_$HY.sd&&delete _$HY.sd[e]}}function $dfj(e,i,n){for(i=0;i<e.length;i++)if(_$HY.sc&&_$HY.sc[e[i]]>0){for(n=0;n<e.length;n++)(_$HY.sg=_$HY.sg||{})[e[n]]=e;return}for(i=0;i<e.length;i++)$df(e[i])};</script><script>$R[13]($R[12],!0);</script>",
5+
"<template id=\"6\"><aside _hk=6001>side-data</aside></template><script>$R[13]($R[8],\"side-data\");$df(\"6\");</script><script>$R[13]($R[10],!0);</script>"
6+
]
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"shell": "<main _hk=2><h1>title:/a</h1><h2>label:/a</h2><!--$--><template id=\"pl-5\"></template><p _hk=50>side-loading</p><!--pl-5--><!--/--><!--$--><template id=\"pl-6\"></template><p _hk=60>sync-loading</p><!--pl-6--><!--/--></main><script>(self.$R=self.$R||{})[\"\"]=[];_$HY.r[\"$B\"]=($R[0]={1:$R[1]=($R[2]=($R[3]=() => {\n\tconst resolver = {\n\t\tp: 0,\n\t\ts: 0,\n\t\tf: 0\n\t};\n\tresolver.p = new Promise((resolve, reject) => {\n\t\tresolver.s = resolve;\n\t\tresolver.f = reject;\n\t});\n\treturn resolver;\n})()).p,5001:$R[4]=($R[5]=$R[3]()).p,6000:$R[6]=($R[7]=$R[3]()).p,\"5_fr\":$R[8]=($R[9]=$R[3]()).p,\"6_fr\":$R[10]=($R[11]=$R[3]()).p});(b=>{for(var k in b)_$HY.r[k]=b[k];delete _$HY.r[\"$B\"]})(_$HY.r[\"$B\"]);($R[12]=(resolver, data) => {\n\tresolver.s(data);\n\tresolver.p.s = 1;\n\tresolver.p.v = data;\n})($R[2],\"title:/a\");</script>",
3+
"chunks": [
4+
"<template id=\"5\"><aside _hk=5002><span class=\"inner\">inner:/a</span><span class=\"data\">data:/a</span><span class=\"raw\">/a</span></aside></template><script>$R[12]($R[5],\"data:/a\");$df(\"5\");function $df(e){return _$HY.f?_$HY.f(e):$dfr(e)}function $dfr(e,n,o,t){if(!(n=document.getElementById(e)))return 0;if(!(o=document.getElementById(\"pl-\"+e)))return(_$HY.dq=_$HY.dq||{})[e]=1,0;for(;o&&(8!==o.nodeType||o.nodeValue!==\"pl-\"+e);)t=o.nextSibling,o.remove(),o=t;t=o.parentNode,o.replaceWith(n.content),n.remove(),(_$HY.v=_$HY.v||{})[e]=1,_$HY.fe(e,t),_$HY.hp&&_$HY.hp[e]&&($dh(_$HY.hp[e]),delete _$HY.hp[e]),$dfd();return 1}function $dfl(e,o,n){if(!(o=document.getElementById(\"pl-\"+e)))return(_$HY.dlq=_$HY.dlq||{})[e]=1,0;if(o._$fl)return 1;for(n=o.nextSibling;n;){if(8===n.nodeType&&n.nodeValue===\"pl-\"+e){o.parentNode&&o.parentNode.insertBefore(o.content.cloneNode(!0),n),o._$fl=1,$dfd();return 1}n=n.nextSibling}return 0}function $dflj(e,i){for(i=0;i<e.length;i++)$dfl(e[i])}function $dfd(e,i){if(e=_$HY.dq){_$HY.dq=0;for(i in e)$df(i)}if(e=_$HY.dlq){_$HY.dlq=0;for(i in e)$dfl(i)}}function $dfs(e,c,d){(_$HY.sc=_$HY.sc||{})[e]=c,d&&((_$HY.sd=_$HY.sd||{})[e]=1)}function $dfg(e,g,i,k){if(!(g=_$HY.sg&&_$HY.sg[e]))return;for(i=0;i<g.length;i++)if(_$HY.sc&&_$HY.sc[g[i]]>0)return;for(i=0;i<g.length;i++)k=g[i],delete _$HY.sg[k],$df(k)}function $dfc(e){if(--_$HY.sc[e]<=0){delete _$HY.sc[e],_$HY.sg&&_$HY.sg[e]?$dfg(e):!(_$HY.sd&&_$HY.sd[e])&&$df(e);_$HY.sd&&delete _$HY.sd[e]}}function $dfj(e,i,n){for(i=0;i<e.length;i++)if(_$HY.sc&&_$HY.sc[e[i]]>0){for(n=0;n<e.length;n++)(_$HY.sg=_$HY.sg||{})[e[n]]=e;return}for(i=0;i<e.length;i++)$df(e[i])};</script><script>$R[12]($R[9],!0);</script><template id=\"6\"><nav _hk=6001><span class=\"gate\">static</span><span class=\"sync\">sync:/a</span></nav></template><script>$R[12]($R[7],\"static\");$df(\"6\");</script><script>$R[12]($R[11],!0);</script>"
5+
]
6+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
*
4+
* A router-shaped page for "navigate while an SSR-streamed
5+
* boundary is still pending". Compiled with both generates: the server
6+
* spec (test/server/nav-before-resume.gen.spec.tsx) writes the chunks the
7+
* client spec (test/hydration/nav-before-resume.spec.tsx) replays.
8+
*
9+
* - `title`: shell-level async memo keyed on the location (layout data).
10+
* - `label`: shell-level sync memo keyed on the location.
11+
* - `<Side>`: a slow streamed boundary OUTSIDE the route outlet; it reads a
12+
* library keyed value at resume (the #2964 late-boundary adoption shape).
13+
* - `<RouteA>`: the initial route, with its own streamed boundary that is
14+
* still pending when the client navigates away.
15+
* - `<RouteB>`: the navigation target, rendered fresh.
16+
*/
17+
import { createMemo, createSignal, Loading, onSettled, Show } from "solid-js";
18+
import { getHydrationWriter, isServer, takeHydrationValue } from "@solidjs/web";
19+
20+
const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));
21+
22+
export type NavHooks = {
23+
settled?: () => void;
24+
sideTaken?: (v: unknown) => void;
25+
bRendered?: () => void;
26+
};
27+
28+
export function createNavApp(hooks: NavHooks = {}) {
29+
const [path, setPath] = createSignal("/a", { ownedWrite: true });
30+
31+
function Side() {
32+
const data = createMemo(async () => {
33+
await sleep(60);
34+
return "side-data";
35+
});
36+
if (isServer) getHydrationWriter()?.write("lib:side", { n: 42 });
37+
else hooks.sideTaken?.(takeHydrationValue("lib:side"));
38+
return <aside>{data()}</aside>;
39+
}
40+
41+
function RouteA() {
42+
const d = createMemo(async () => {
43+
await sleep(20);
44+
return "a-data";
45+
});
46+
return (
47+
<section>
48+
<Loading fallback={<i>a-loading</i>}>
49+
<b>{d()}</b>
50+
</Loading>
51+
</section>
52+
);
53+
}
54+
55+
function RouteB() {
56+
hooks.bRendered?.();
57+
return (
58+
<section>
59+
<em>page b</em>
60+
</section>
61+
);
62+
}
63+
64+
function App() {
65+
const title = createMemo(async () => {
66+
const p = path();
67+
await sleep(1);
68+
return "title:" + p;
69+
});
70+
const label = createMemo(() => "label:" + path());
71+
onSettled(() => {
72+
hooks.settled?.();
73+
});
74+
return (
75+
<main>
76+
<h1>{title()}</h1>
77+
<h2>{label()}</h2>
78+
<Loading fallback={<p>side-loading</p>}>
79+
<Side />
80+
</Loading>
81+
<Show when={path() === "/a"} fallback={<RouteB />}>
82+
<RouteA />
83+
</Show>
84+
</main>
85+
);
86+
}
87+
88+
return { App, navigate: (p: string) => setPath(p) };
89+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
*
4+
* One source (`path`) read in the shell (a sync and an async memo) and
5+
* inside two slow streamed boundaries: `<Side>` reads it through a sync
6+
* memo, an async memo, and directly; `<SyncSide>` reads it only
7+
* synchronously. Compiled with both generates: the server spec
8+
* (test/server/write-before-resume.gen.spec.tsx) writes the chunks the
9+
* client spec (test/hydration/write-before-resume.spec.tsx) replays.
10+
*/
11+
import { createMemo, createSignal, Loading } from "solid-js";
12+
13+
const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));
14+
15+
export function createSharedApp() {
16+
const [path, setPath] = createSignal("/a", { ownedWrite: true });
17+
18+
function Side() {
19+
const inner = createMemo(() => "inner:" + path());
20+
const data = createMemo(async () => {
21+
const p = path();
22+
await sleep(60);
23+
return "data:" + p;
24+
});
25+
return (
26+
<aside>
27+
<span class="inner">{inner()}</span>
28+
<span class="data">{data()}</span>
29+
<span class="raw">{path()}</span>
30+
</aside>
31+
);
32+
}
33+
34+
function SyncSide() {
35+
const gate = createMemo(async () => {
36+
await sleep(60);
37+
return "static";
38+
});
39+
return (
40+
<nav>
41+
<span class="gate">{gate()}</span>
42+
<span class="sync">{"sync:" + path()}</span>
43+
</nav>
44+
);
45+
}
46+
47+
function App() {
48+
const label = createMemo(() => "label:" + path());
49+
const title = createMemo(async () => {
50+
const p = path();
51+
await sleep(1);
52+
return "title:" + p;
53+
});
54+
return (
55+
<main>
56+
<h1>{title()}</h1>
57+
<h2>{label()}</h2>
58+
<Loading fallback={<p>side-loading</p>}>
59+
<Side />
60+
</Loading>
61+
<Loading fallback={<p>sync-loading</p>}>
62+
<SyncSide />
63+
</Loading>
64+
</main>
65+
);
66+
}
67+
68+
return { App, navigate: (p: string) => setPath(p) };
69+
}

0 commit comments

Comments
 (0)