Skip to content

Commit ce8e46b

Browse files
feat(solid): SSR flushes commit #0 — loadingValue/seedLoadingValue render on the server and hydrate as the claimed value, replacing the interim hydration strip. The server never suspends a loading-value source: processResult serves the loading value instead of NotReadyError on every async path (thenable, iterator, hybrid, NotReady-retry from an unready sync dep, ssrSource "client"), the boundary never trips, markup flushes from commit #0, and the landing streams as data through the existing serialization channel. The first-value lock generalizes to commit #0: on serialized paths the HTML-visible value never advances past the loading value (settle callbacks skip the comp.value write; projections mark the pending proxy ready immediately, retargeted at a frozen seed copy, and the V1 freeze skips retargeting), so later-rendered holes in the same response can't tear against already-flushed placeholder markup — landings live in the data channel, exactly as later iterator yields always have. One deliberate exception to "sync results are never serialized": a sync landing after a NotReady retry ships as a resolved promise, because placeholder markup is already on the wire and the client can't re-derive its way out of DOM claimed against commit #0. createSignal's server fn-form forwards loadingValue (it previously dropped everything but deferStream/ssrSource); ServerSsrOptions gains seedLoadingValue and createProjection applies it across all four async branches. Client side, the strip (stripLoadingValue) is deleted: hydrating nodes are born committed with the loading value so the claim matches the placeholder markup by construction — and readHydratedValue gains the inverse guard the loaded replay exposed: a settled serialized landing (s===1 stamped ref) must NOT unwrap synchronously for a loading-window node, or the client computes real-data structure during the claim walk against placeholder DOM and corrupts it (the exact mirror of the pre-strip streamed failure). It hands the async runtime a clean thenable instead — commit #0 serves through the synchronous walk, the landing applies on the following microtask, the acknowledged loaded-mode tradeoff. Parity scenarios flip from pinning the strip to pinning the design: shells now assert placeholder markup (skeltail/emptyend), settled text asserts the landing, and the update pass pins post-hydration refetch. Verified: signals 1225/1225, solid-js turbo 26/27 tasks — the one failure is solid-web's pre-existing retry-robustness timeout, reproduced identically on clean next; hydrate parity 65/65 both modes.
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 103e57f commit ce8e46b

7 files changed

Lines changed: 184 additions & 83 deletions

File tree

‎.changeset/loading-value-hydration-guard.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": minor
3+
---
4+
5+
SSR support for `loadingValue` / `seedLoadingValue` — the server flushes commit #0 and the client hydrates with it. A loading-value memo (or seeded derived store) never suspends on the server: markup renders from the loading value (the boundary never trips), the landing streams to the client as data, and the hydrating node is born committed with the same loading value, so the claim walk matches the server markup by construction; the serialized landing then closes the window on both sides right after hydration. The first-value lock generalizes to commit #0: HTML-visible reads stay at the loading value for the whole response (later renders in the same stream can't tear against already-flushed placeholder markup), and every landing after the placeholder flushed ships as data — including synchronous landings after a NotReady retry, the one exception to "sync results are never serialized". On the client, a settled serialized landing is handed to the async runtime as a thenable instead of unwrapping synchronously, so commit #0 also serves through the claim walk when the response was fully loaded before hydration began. `ssrSource: "client"` sources flush the loading value too, matching the prev-serving hydration wrapper on the client.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "loading-seed-store",
3-
"shell": "<template id=\"pl-1\"></template><p _hk=10>loading</p><!--pl-1--><script>(self.$R=self.$R||{})[\"\"]=[];_$HY.r[\"0\"]=$R[0]=($R[1]=($R[2]=() => {\n const resolver = {\n p: 0,\n s: 0,\n f: 0\n };\n resolver.p = new Promise((resolve, reject) => {\n resolver.s = resolve;\n resolver.f = reject;\n });\n return resolver;\n})()).p;_$HY.r[\"1_fr\"]=$R[3]=($R[4]=$R[2]()).p;</script>",
4-
"rest": "<script>($R[7]=(resolver, data) => {\n resolver.s(data);\n resolver.p.s = 1;\n resolver.p.v = data;\n})($R[1],$R[5]={items:$R[6]=[\"row-0\"],ready:!0});</script><template id=\"1\"><div _hk=1000><!--$--><span _hk=1003000>row-0</span><!--/--><p>end</p></div></template><script>$df(\"1\");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[7]($R[4],!0);</script>"
3+
"shell": "<div _hk=1000><!--$--><i _hk=10030>empty</i><!--/--><p>end</p></div><script>(self.$R=self.$R||{})[\"\"]=[];_$HY.r[\"0\"]=$R[0]=($R[1]=($R[2]=() => {\n const resolver = {\n p: 0,\n s: 0,\n f: 0\n };\n resolver.p = new Promise((resolve, reject) => {\n resolver.s = resolve;\n resolver.f = reject;\n });\n return resolver;\n})()).p;</script>",
4+
"rest": "<script>($R[5]=(resolver, data) => {\n resolver.s(data);\n resolver.p.s = 1;\n resolver.p.v = data;\n})($R[1],$R[3]={items:$R[4]=[\"row-0\"],ready:!0});</script>"
55
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "loading-value-memo",
3-
"shell": "<template id=\"pl-1\"></template><p _hk=10>loading</p><!--pl-1--><script>(self.$R=self.$R||{})[\"\"]=[];_$HY.r[\"0\"]=$R[0]=($R[1]=($R[2]=() => {\n const resolver = {\n p: 0,\n s: 0,\n f: 0\n };\n resolver.p = new Promise((resolve, reject) => {\n resolver.s = resolve;\n resolver.f = reject;\n });\n return resolver;\n})()).p;_$HY.r[\"1_fr\"]=$R[3]=($R[4]=$R[2]()).p;</script>",
4-
"rest": "<script>($R[6]=(resolver, data) => {\n resolver.s(data);\n resolver.p.s = 1;\n resolver.p.v = data;\n})($R[1],$R[5]={skeleton:!1,label:\"item-0\"});</script><template id=\"1\"><div _hk=1000><!--$--><b _hk=10030>item-0</b><!--/--><span>tail</span></div></template><script>$df(\"1\");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[6]($R[4],!0);</script>"
3+
"shell": "<div _hk=1000><!--$--><i _hk=10030>skel</i><!--/--><span>tail</span></div><script>(self.$R=self.$R||{})[\"\"]=[];_$HY.r[\"0\"]=$R[0]=($R[1]=($R[2]=() => {\n const resolver = {\n p: 0,\n s: 0,\n f: 0\n };\n resolver.p = new Promise((resolve, reject) => {\n resolver.s = resolve;\n resolver.f = reject;\n });\n return resolver;\n})()).p;</script>",
4+
"rest": "<script>($R[4]=(resolver, data) => {\n resolver.s(data);\n resolver.p.s = 1;\n resolver.p.v = data;\n})($R[1],$R[3]={skeleton:!1,label:\"item-0\"});</script>"
55
}

‎packages/solid-web/test/harness/scenarios.tsx‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -957,14 +957,13 @@ function ProjectionRepeatStream() {
957957
}
958958

959959
// ---------------------------------------------------------------------------
960-
// loadingValue under hydration: the server ignores the loading window today
961-
// (an async source suspends and streams the REAL value), so the hydrating
962-
// client must drop commit #0 and adopt the serialized value — computing
963-
// structure from the placeholder would claim the wrong Show branch against
964-
// real-data DOM (interim guard until SSR renders commit #0; stripLoadingValue
965-
// in solid-js client/hydration.ts). The placeholder here deliberately flips
966-
// the Show branch so a regression corrupts the walk instead of just
967-
// mismatching text.
960+
// loadingValue under SSR: the server serves commit #0 — markup flushes with
961+
// the loading value (the boundary never trips), the landing streams as data,
962+
// and the hydrating client is born committed with the same loading value, so
963+
// the claim matches by construction; the serialized landing then closes the
964+
// window on both sides. The placeholder here deliberately flips the Show
965+
// branch so any server/client disagreement about the window corrupts the
966+
// walk instead of just mismatching text.
968967
let refreshLoadingValue!: () => void;
969968
function LoadingValueMemo() {
970969
const [version, setVersion] = createSignal(0);
@@ -989,10 +988,10 @@ function LoadingValueMemo() {
989988
);
990989
}
991990

992-
// Same guard for the store form: seedLoadingValue is dropped during
993-
// hydration, so the derived store adopts the streamed server state like any
994-
// async store instead of exposing the seed (whose `ready: false` would flip
995-
// the Show branch against real-data DOM).
991+
// Store form: seedLoadingValue promotes the seed to commit #0 on both sides —
992+
// server markup flushes with the seed (ready: false renders the empty branch),
993+
// the landing streams as snapshot/patch data, and the client store is born
994+
// committed with the same seed before reconciling the landing in.
996995
let refreshLoadingSeedStore!: () => void;
997996
function LoadingSeedStore() {
998997
const [version, setVersion] = createSignal(0);
@@ -1366,6 +1365,7 @@ export const scenarios: Scenario[] = [
13661365
App: LoadingValueMemo,
13671366
async: true,
13681367
expectedText: "item-0tail",
1368+
serverText: "skeltail",
13691369
update: () => refreshLoadingValue(),
13701370
expectedTextAfterUpdate: "item-1tail",
13711371
stableSelector: "div, span, b"
@@ -1375,6 +1375,7 @@ export const scenarios: Scenario[] = [
13751375
App: LoadingSeedStore,
13761376
async: true,
13771377
expectedText: "row-0end",
1378+
serverText: "emptyend",
13781379
update: () => refreshLoadingSeedStore(),
13791380
expectedTextAfterUpdate: "row-1end",
13801381
stableSelector: "div, p"

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

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -347,17 +347,25 @@ function syncThenable(value: any) {
347347
* status `s` (1 = fulfilled, 2 = rejected) and payload `v`. The payload is
348348
* read directly — `v ?? ref` would leak the ref object for nullish payloads.
349349
*/
350-
function readHydratedValue(initP: any, refresh: () => void) {
350+
function readHydratedValue(initP: any, refresh: () => void, keepThenable?: boolean) {
351351
refresh();
352352
if (initP != null && typeof initP === "object") {
353+
// Commit #0 (loadingValue/seedLoadingValue): the server flushed markup
354+
// from the loading value, so the hydrating client must serve the same
355+
// value through the synchronous claim walk — a settled landing must NOT
356+
// unwrap synchronously here (structure computed from the real data would
357+
// claim against placeholder DOM). Hand the async runtime a clean thenable
358+
// (the settled refs are stamped `s`/`v`, which the core would also
359+
// fast-adopt): the landing applies on the microtask after the walk.
360+
if (keepThenable && typeof initP.then === "function") return { then: initP.then.bind(initP) };
353361
if (initP.s === 2) throw initP.v;
354362
if (initP.s === 1) return initP.v;
355363
}
356364
return initP;
357365
}
358366

359367
/** Shared “serialized init or run compute” path for memo/signal/optimistic/effect under hydration. */
360-
function readSerializedOrCompute(compute: (prev: any) => any, prev: any) {
368+
function readSerializedOrCompute(compute: (prev: any) => any, prev: any, keepThenable?: boolean) {
361369
const o = getOwner()!;
362370
// A computation must adopt its serialized server value for the whole
363371
// hydration lifecycle (`!done`), not just inside a synchronous resume window.
@@ -366,7 +374,16 @@ function readSerializedOrCompute(compute: (prev: any) => any, prev: any) {
366374
// So short-circuit to the server value whenever one is still waiting; once
367375
// hydration is `done`, always compute.
368376
if (sharedConfig.done || !sharedConfig.has!(o.id!)) return compute(prev);
369-
return readHydratedValue(sharedConfig.load!(o.id!), () => subFetch(compute, prev));
377+
return readHydratedValue(sharedConfig.load!(o.id!), () => subFetch(compute, prev), keepThenable);
378+
}
379+
380+
/** Options carry commit #0 — the loading window must hold through the claim walk. */
381+
function hasLoadingWindow(options: any): boolean {
382+
return (
383+
options != null &&
384+
typeof options === "object" &&
385+
("loadingValue" in options || options.seedLoadingValue === true)
386+
);
370387
}
371388

372389
function forwardIteratorReturn(it: any, value?: any) {
@@ -650,39 +667,13 @@ function hydrateStoreFromAsyncIterable(
650667

651668
// --- Hydration-aware implementations ---
652669

653-
/**
654-
* Interim guard until SSR renders commit #0 (the loadingValue follow-up):
655-
* the server ignores loading values today — an async source suspends into
656-
* its Loading boundary and streams the real value — so a node hydrating
657-
* with an open loading window would walk DOM the server rendered from the
658-
* REAL data while computing structure from the placeholder (a `Show` over
659-
* `data.skeleton` claims the wrong branch and corrupts the walk). During
660-
* hydration the loading window is therefore dropped: the node adopts the
661-
* serialized server value exactly like any async source, and loading
662-
* values apply only to fresh client mounts, where commit #0 is correct by
663-
* construction.
664-
*/
665-
function stripLoadingValue(options: any): any {
666-
if (options == null || typeof options !== "object") return options;
667-
if ("loadingValue" in options) {
668-
const { loadingValue: _loadingValue, ...rest } = options;
669-
return rest;
670-
}
671-
if ("seedLoadingValue" in options) {
672-
const { seedLoadingValue: _seedLoadingValue, ...rest } = options;
673-
return rest;
674-
}
675-
return options;
676-
}
677-
678670
// One signal-shaped hydration body for memo/signal/optimistic — the families
679671
// only ever differed in which core primitive committed the result, so the
680672
// core function is a parameter. createOptimistic reaches this through the
681673
// _hydrateSignalLike slot precisely so the wrapper does not statically couple
682674
// the optimistic engine to this body (which would drag it into CSR bundles).
683675
function hydrateSignalLike(coreFn: Function, fn: any, options?: any) {
684676
markTopLevelSnapshotScope();
685-
options = stripLoadingValue(options);
686677

687678
const ssrSource = options?.ssrSource;
688679

@@ -700,7 +691,8 @@ function hydrateSignalLike(coreFn: Function, fn: any, options?: any) {
700691
const aiResult = hydrateSignalFromAsyncIterable(coreFn, fn, options);
701692
if (aiResult !== null) return aiResult;
702693

703-
return coreFn((prev: any) => readSerializedOrCompute(fn, prev), options);
694+
const loading = hasLoadingWindow(options);
695+
return coreFn((prev: any) => readSerializedOrCompute(fn, prev, loading), options);
704696
}
705697

706698
function hydratedCreateMemo(compute: any, options?: any) {
@@ -739,8 +731,8 @@ function hydratedCreateErrorBoundary<T, U>(
739731
return coreErrorBoundary(fn, fallback);
740732
}
741733

742-
function wrapStoreFn(fn: any) {
743-
return (draft: any) => readSerializedOrCompute(() => fn(draft), draft);
734+
function wrapStoreFn(fn: any, keepThenable?: boolean) {
735+
return (draft: any) => readSerializedOrCompute(() => fn(draft), draft, keepThenable);
744736
}
745737

746738
function hydrateStoreLikeFn(
@@ -770,7 +762,11 @@ function hydrateStoreLikeFn(
770762
const o = getOwner()!;
771763
if (!hydrated()) {
772764
if (sharedConfig.has!(o.id!))
773-
return readHydratedValue(sharedConfig.load!(o.id!), () => subFetch(fn, draft));
765+
return readHydratedValue(
766+
sharedConfig.load!(o.id!),
767+
() => subFetch(fn, draft),
768+
hasLoadingWindow(options)
769+
);
774770
return fn(draft);
775771
}
776772
const { proxy, activate } = createShadowDraft(draft);
@@ -785,7 +781,7 @@ function hydrateStoreLikeFn(
785781
}
786782
const aiResult = hydrateStoreFromAsyncIterable(coreFn, fn, initialValue, options);
787783
if (aiResult !== null) return aiResult;
788-
return coreFn(wrapStoreFn(fn), initialValue, options);
784+
return coreFn(wrapStoreFn(fn, hasLoadingWindow(options)), initialValue, options);
789785
}
790786

791787
// The store-shaped counterpart to hydrateSignalLike: one body for
@@ -796,7 +792,6 @@ function hydrateStoreLikeFn(
796792
// reached moved.
797793
function hydrateStoreLike(coreFn: Function, fn: any, initialValue: any, options?: any) {
798794
markTopLevelSnapshotScope();
799-
options = stripLoadingValue(options);
800795
return hydrateStoreLikeFn(coreFn, fn, initialValue, options, options?.ssrSource);
801796
}
802797

0 commit comments

Comments
 (0)