Skip to content

Commit f1929d6

Browse files
committed
fix(solid): hydrate an errored boundary's content in the pass (#4273)
Replaces the re-run at hydration end. When the server sent an error, the boundary runs its content in the hydration pass with its data adopted by id and no DOM claimed under it (`_uc`, read by isClaiming), so nothing the server answered is fetched again and the fallback claims the server's markup. A content that fails on the client holds that error and tracks its inputs. A content that runs clean keeps the server's fallback and error up behind a hold, ended by reset() or the content re-running. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 2c2a912 commit f1929d6

4 files changed

Lines changed: 341 additions & 41 deletions

File tree

‎.changeset/errored-hydrated-recovery-4273.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"solid-js": patch
33
---
44

5-
An `<Errored>` boundary hydrated in the error state the server rendered now recovers. Before, it showed the server's error and tracked nothing, so it stayed on the fallback until `reset()` was called, even after the failed content's inputs changed. Now the content runs once on the client when hydration ends. If it fails again, the fallback stays, now showing the client's own error, and the boundary recovers when the content's inputs change or on `reset()`; the server's error only tells hydration to claim the fallback markup. If the error only happened on the server, the client's content replaces the fallback after hydration (#4273).
5+
An `<Errored>` boundary hydrated in the error state the server rendered now recovers. Before, hydration threw the server's error without running the content, so the boundary tracked nothing and stayed on the fallback until `reset()`, even after the failed content's inputs changed. Now the content runs during hydration, reading its server data without refetching but claiming no DOM, while the fallback claims the server's markup. If the content fails on the client, the boundary holds that client error and recovers when the content's inputs change. If the error only happened on the server, the server's fallback and error stay up until `reset()` is called or the content re-runs (#4273).

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

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,18 @@ let _snapshotRootOwner: Owner | null = null;
304304
// The boundary owner whose resume window is open (null during a root pass,
305305
// which claims everything). Reached as `sharedConfig.isClaiming`.
306306
let _claimOwner: Owner | null = null;
307+
// Set once a hydrated `<Errored>` runs content the server drew no markup
308+
// for: that content's owner (`_uc`) claims nothing.
309+
let _unclaimed = false;
307310
function isClaiming(): boolean {
308-
if (!_claimOwner) return true;
311+
if (!_claimOwner && !_unclaimed) return true;
309312
let owner: Owner | null = getOwner();
310313
while (owner) {
314+
if ((owner as any)._uc) return false;
311315
if (owner === _claimOwner) return true;
312316
owner = owner._parent;
313317
}
314-
return false;
318+
return !_claimOwner;
315319
}
316320

317321
/** @internal — shared with the store adapter module (store-hydration.ts). */
@@ -421,6 +425,7 @@ export function onHydrationEnd(callback: () => void): void {
421425
function drainHydrationCallbacks() {
422426
if (_hydrationDone) return;
423427
_hydrationDone = true;
428+
_unclaimed = false;
424429
_doneValue = true;
425430
clearSnapshots();
426431
setSnapshotCapture(false);
@@ -1276,22 +1281,41 @@ function hydratedCreateErrorBoundary<T, U>(
12761281
const expectedId = peekNextChildId(parent);
12771282
if (sharedConfig.has!(expectedId)) {
12781283
const err = sharedConfig.load!(expectedId);
1279-
// The server drew the fallback: the first pass throws its error to claim
1280-
// that markup. It tracks the end of hydration, where the content runs
1281-
// once for real so the boundary tracks its inputs and recovers when they
1282-
// change (D-39, #4273); failing again keeps the built fallback.
1284+
// The server drew the fallback; the client's own run decides. The
1285+
// content runs now with its data adopted by id but claiming no DOM —
1286+
// the markup is the fallback's — so what it throws is the client's
1287+
// error, held and tracked like any other. Content that runs clean keeps
1288+
// the server's fallback up behind a hold: a render effect in the content
1289+
// throwing the server's error until reset(), disposed with the content
1290+
// when it re-runs (#4273).
12831291
if (err !== undefined) {
1284-
let hydrated = true;
1285-
const [ended, setEnded] = coreSignal(false);
1286-
onHydrationEnd(() => setEnded(true));
1287-
return coreErrorBoundary(() => {
1288-
if (hydrated) {
1289-
hydrated = false;
1290-
ended();
1291-
throw err;
1292-
}
1293-
return fn();
1294-
}, fallback);
1292+
_unclaimed = true;
1293+
let o: any, held: any;
1294+
const read = coreErrorBoundary(
1295+
() => {
1296+
(o = getOwner() as any)._uc = 1;
1297+
return fn();
1298+
},
1299+
(e, reset) =>
1300+
fallback(
1301+
e,
1302+
(held = () => {
1303+
held = 0;
1304+
reset();
1305+
})
1306+
)
1307+
);
1308+
// No fallback yet: the content ran clean.
1309+
if (!held && (held = 1))
1310+
runWithOwner(o, () =>
1311+
coreRenderEffect(
1312+
() => {
1313+
if (held) throw err;
1314+
},
1315+
() => {}
1316+
)
1317+
);
1318+
return read;
12951319
}
12961320
}
12971321
return coreErrorBoundary(fn, fallback);

‎packages/solid/test/client-hydration.spec.ts‎

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,14 @@ describe("Error Boundary Hydration", () => {
8787
() => "children content",
8888
(err: any) => `fallback: ${err().message}`
8989
);
90-
result = read();
90+
result = read;
9191
},
9292
{ id: "t" }
9393
);
9494
flush();
95+
// The display settles in the flush after creation (#4273).
9596

96-
expect(result).toBe("fallback: server error");
97+
expect(result()).toBe("fallback: server error");
9798
});
9899

99100
test("createErrorBoundary passes through when no serialized error", () => {
@@ -131,14 +132,15 @@ describe("Error Boundary Hydration", () => {
131132
return `fallback: ${err().message}`;
132133
}
133134
);
134-
result = read();
135+
result = read;
135136
},
136137
{ id: "t" }
137138
);
138139
flush();
140+
// The display settles in the flush after creation (#4273).
139141

140142
// Initially shows fallback from serialized error
141-
expect(result).toBe("fallback: server error");
143+
expect(result()).toBe("fallback: server error");
142144
expect(resetFn).toBeDefined();
143145

144146
// After reset, the real fn should run
@@ -178,7 +180,7 @@ describe("Error Boundary Hydration", () => {
178180
expect(read()).toBe("item 3");
179181
});
180182

181-
test("createErrorBoundary hydrated from a server error holds the client's own error once hydration ends (#4273)", () => {
183+
test("createErrorBoundary hydrated from a server error holds the client's own error (#4273)", () => {
182184
startHydration({ t0: new Error("server error") });
183185

184186
let fails = true;
@@ -203,22 +205,46 @@ describe("Error Boundary Hydration", () => {
203205
{ id: "t" }
204206
);
205207
flush();
206-
expect(read()).toBe("fallback: server error");
207-
208-
// At hydration end the content's own run throws: the boundary holds the
209-
// client error, and the fallback receives it.
210-
stopHydration();
211-
flush();
208+
// The content's own run in the hydration pass throws: the boundary holds
209+
// the client error, and the fallback receives it.
212210
expect(read()).toBe("fallback: client error");
213211
expect((seen[seen.length - 1] as Error).message).toBe("client error");
214212

215213
// reset() clears the client error.
214+
stopHydration();
216215
fails = false;
217216
resetFn();
218217
flush();
219218
expect(read()).toBe("recovered");
220219
});
221220

221+
test("createErrorBoundary content that runs clean keeps the server's fallback until the content re-runs (#4273)", () => {
222+
startHydration({ t0: new Error("server error") });
223+
224+
const [label, setLabel] = coreSignal("client");
225+
let read!: () => unknown;
226+
createRoot(
227+
() => {
228+
read = createErrorBoundary(
229+
() => label(),
230+
(err: any) => `fallback: ${err().message}`
231+
);
232+
read();
233+
},
234+
{ id: "t" }
235+
);
236+
flush();
237+
expect(read()).toBe("fallback: server error");
238+
239+
stopHydration();
240+
flush();
241+
expect(read()).toBe("fallback: server error");
242+
243+
setLabel("changed");
244+
flush();
245+
expect(read()).toBe("changed");
246+
});
247+
222248
test("createErrorBoundary handles non-Error serialized values", () => {
223249
// Server might serialize a string or other value as the error
224250
startHydration({ t0: "string error" });
@@ -230,13 +256,14 @@ describe("Error Boundary Hydration", () => {
230256
() => "children content",
231257
(err: any) => `fallback: ${err()}`
232258
);
233-
result = read();
259+
result = read;
234260
},
235261
{ id: "t" }
236262
);
237263
flush();
264+
// The display settles in the flush after creation (#4273).
238265

239-
expect(result).toBe("fallback: string error");
266+
expect(result()).toBe("fallback: string error");
240267
});
241268

242269
test("Errored component reads serialized error during hydration", () => {
@@ -336,13 +363,14 @@ describe("Error Boundary Hydration", () => {
336363
},
337364
(err: any) => `outer-fallback: ${err().message}`
338365
);
339-
result = read();
366+
result = read;
340367
},
341368
{ id: "t" }
342369
);
343370
flush();
371+
// The display settles in the flush after creation (#4273).
344372

345-
expect(result).toBe("outer-fallback: outer error");
373+
expect(result()).toBe("outer-fallback: outer error");
346374
});
347375

348376
test("ID alignment: boundary after memo during hydration", () => {
@@ -359,16 +387,17 @@ describe("Error Boundary Hydration", () => {
359387
() => "children",
360388
(err: any) => `fallback: ${err().message}`
361389
);
362-
boundaryResult = read();
390+
boundaryResult = read;
363391
},
364392
{ id: "t" }
365393
);
366394
flush();
395+
// The display settles in the flush after creation (#4273).
367396

368397
// Memo should have loaded serialized value 42 (not computed 99)
369398
expect(memoResult).toBe(42);
370399
// Boundary should have loaded serialized error
371-
expect(boundaryResult).toBe("fallback: boundary error");
400+
expect(boundaryResult()).toBe("fallback: boundary error");
372401
});
373402

374403
test("Errored shows async rejection fallback after Loading resolves during hydration", async () => {
@@ -510,7 +539,7 @@ describe("Error Boundary Hydration", () => {
510539
expect(read(result)).toBe("Test Item");
511540
});
512541

513-
test("Loading inner Errored hydrated with a server-only error renders client content once hydration ends", async () => {
542+
test("Loading inner Errored reset recovers after hydrated server error", async () => {
514543
let resolveLoading!: () => void;
515544
const loading = new Promise<void>(r => {
516545
resolveLoading = r;
@@ -556,12 +585,8 @@ describe("Error Boundary Hydration", () => {
556585
await Promise.resolve();
557586
flush();
558587

559-
// The resume ends hydration: the content runs once for real and, since
560-
// the error was server-only, the client's answer replaces it (#4273).
561-
expect(resetFn).toBeDefined();
562-
expect(read(result)).toBe("Bad Item");
588+
expect(read(result)).toBe("ItemError: Item bad-item not found");
563589

564-
// The content is live: it tracks its inputs, and a reset is harmless.
565590
setId("1");
566591
resetFn!();
567592
flush();

0 commit comments

Comments
 (0)