Skip to content

Commit f5cf675

Browse files
ryansolidclaude
andauthored
fix(solid): an Errored around a failing streamed Loading draws its fallback on the server (#4303)
* fix(solid,web): draw an Errored fallback for a Loading's async failure, and adopt it (#4287) Outside a server component, a <Loading> whose async read failed during a streamed render settled its fragment blank, and the hydrating client rendered the failed boundary from scratch: it asked again and showed the loading fallback. Server: the failing <Loading> now asks the enclosing <Errored> for its outcome outside server components too, passing its own id. The Errored answers only when that Loading is all of its content (its resolved markup is the Loading's placeholder): its fallback is drawn into the fragment, or inlined pre-flush, and the error is serialized at the Errored id. Deeper in the content it answers nothing, as before. A rejected fragment's template is marked `e`, and `$dfr` records the nodes it swaps in for it. Client: inside a hydrated Errored's no-claim content run, a <Loading> runs its content with its data adopted instead of waiting on its fragment, so the Errored holds the client's own error and tracks the content's inputs (#4273). A boundary that claimed its fallback and then renders a rejected fragment fresh clears the nodes the swap put in first, so nothing of the server's outcome is left beside the client's render. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * fix(solid): serialize an Errored's error only when its fallback markup is sent When a streamed <Loading> fails after the shell and asks the enclosing <Errored> for its outcome, the fallback may hold an async hole that has not resolved yet. renderOutcome then returns no markup (the fragment settles as a plain rejection), but handleError had already serialized the error at the Errored id. A client hydrating after the failure read that record, claimed a fallback that was never sent, and went blank with a hydration key miss. The outcome path now defers the record and writes it only once the fallback resolved to finished markup. Without it the client renders the failure itself, as on `next`. Server-only; no client bytes. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_018LY7F9czoRqdMEvk2QwPr1 --------- Co-authored-by: Claude <[email protected]>
1 parent ee94440 commit f5cf675

71 files changed

Lines changed: 554 additions & 157 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
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+
An `<Errored>` around a `<Loading>` whose async read fails during a streamed render now shows its fallback in the server HTML outside server components too, and the client adopts it. Before, the boundary's HTML was empty until hydration (neither the loading nor the error fallback), and the hydrating client rendered the failed boundary from scratch: it asked again and showed the loading fallback. Now, when the `<Loading>` is all of the `<Errored>`'s content, the server draws the error fallback in its place (in the shell, or in the streamed fragment after the shell) and serializes the error at the `<Errored>`, so a client that hydrates after the failure arrived claims that markup and adopts its data without asking again, and recovers when the content's inputs change, as for any hydrated failure (#4273). A client that hydrated before the failure arrived renders it once, with nothing of the server's swap left beside it. With markup between the two boundaries the server still leaves the position blank and the client renders the failure (#4287).

‎documentation/solid-2.0/12-ssr-http.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ renderToStream(code, { onError });
193193
handleServerFunctionRequest(request, { onError });
194194
```
195195

196-
- **Called once per error object per `kind` per request, at first sight** (per render when no request is in scope — never once per process: an error object reused across requests is heard, and mapped, by each), with where the failure was met. A direct server-function call that throws during SSR inside an `<Errored>` is heard twice, as `server-function` and then as `render`, so `kind: "render"` alone tells you a render failed; the wire value is the first one the hook returned. `kind: "render"` — `fallback` (an `<Errored>` rendered its fallback; `boundary` is its hydration id and `boundaryPath` its component labels — where the error was **met** — while `ownerPath` is where it was **thrown**, the labels up the owner chain it escaped, when the compiler emitted them), `client` (a `<Loading>` fragment rejected, the client re-renders the subtree), `failed` (nothing contained it; the request fails — including a synchronous throw out of `renderToString` or `renderToStream`'s first pass, reported before it reaches the caller), `serialize` (a hydration value would not serialize and the render went on without it — reported for a render that passed `onError`, as seroval's own `onError` always was). `kind: "server-function"` — `thrown` (the body threw; `functionId`, and `direct: true` for an in-process call during SSR) or `channel` (a rejection or throw escaping through the result graph, the head already committed). `kind: "request"` is always `failed`: a failure that fails the request outside any render or server function the runtime reports (a middleware throw, a setup step), reported by the framework's request handler through `reportRequestFailure(error, event)`. A render failure, synchronous throws included, is reported as `render`, so a handler that catches one and passes it to `reportRequestFailure` adds nothing. It has no per-request tier, so only the ambient hook hears it. `event` is the request, when the failure happened inside one, and always for `kind: "request"`.
196+
- **Called once per error object per `kind` per request, at first sight** (per render when no request is in scope — never once per process: an error object reused across requests is heard, and mapped, by each), with where the failure was met. A direct server-function call that throws during SSR inside an `<Errored>` is heard twice, as `server-function` and then as `render`, so `kind: "render"` alone tells you a render failed; the wire value is the first one the hook returned. `kind: "render"` — `fallback` (an `<Errored>` rendered its fallback; `boundary` is its hydration id and `boundaryPath` its component labels — where the error was **met** — while `ownerPath` is where it was **thrown**, the labels up the owner chain it escaped, when the compiler emitted them), `client` (a `<Loading>` fragment rejected, the client re-renders the subtree — unless the `<Loading>` is all of an `<Errored>`'s content: that `<Errored>` draws its fallback into the fragment, reports `fallback`, and the hydrating client adopts it, [#4287](https://github.com/solidjs/solid/issues/4287)), `failed` (nothing contained it; the request fails — including a synchronous throw out of `renderToString` or `renderToStream`'s first pass, reported before it reaches the caller), `serialize` (a hydration value would not serialize and the render went on without it — reported for a render that passed `onError`, as seroval's own `onError` always was). `kind: "server-function"` — `thrown` (the body threw; `functionId`, and `direct: true` for an in-process call during SSR) or `channel` (a rejection or throw escaping through the result graph, the head already committed). `kind: "request"` is always `failed`: a failure that fails the request outside any render or server function the runtime reports (a middleware throw, a setup step), reported by the framework's request handler through `reportRequestFailure(error, event)`. A render failure, synchronous throws included, is reported as `render`, so a handler that catches one and passes it to `reportRequestFailure` adds nothing. It has no per-request tier, so only the ambient hook hears it. `event` is the request, when the failure happened inside one, and always for `kind: "request"`.
197197
- **The return is the wire value**: rendered into the fallback, serialized for hydration, sent as the RPC error. `undefined` leaves the default policy in place (generic outside dev, fidelity in dev). A returned value is the author's intent — like a `wrapInvocation` mapping — and is not sanitized again; `markSafeError` is not needed on it. Ignored for `failed` and `serialize`, which have no wire. One road a mapping does not reach: a rejected async source's serialized rejection is encoded the moment the source rejects, ahead of the boundary, and carries the default policy's value; the hydrating client renders from the boundary's record, which carries the mapping.
198198
- **Once means once across roads.** A direct server-function call that throws during SSR is met first by the invocation (`kind: "server-function"`, `direct: true`) and then by the `<Errored>` that contains it; the boundary reuses the verdict — the same replacement in the fallback and the record — and does not report again. A `<Loading>` re-pull recurring the same throw is the same object, the same verdict. The observe tier keeps the multi-event picture (an `"invocation"` record _and_ an `SSR_RENDER_ERROR_CONTAINED` finding) for consumers that want it.
199199
- **Two tiers, as `wrapInvocation` has.** Ambient: `configureServerErrors({ onError })`, once per process, registered on `globalThis` under a registered symbol so a bundled server build and an instrumented `--import`ed module share it — and the only tier that sees direct in-process calls. Per request, overriding it: `onError` on `renderToStream`/`renderToString`, and on `handleServerFunctionRequest` for the call it dispatches. A throwing hook is reported on the console and treated as silent. With no hook anywhere, a failure that fails the request goes to `console.error` — never silent. A synchronous render throw is the exception: it is rethrown to the caller, whose own handling (or `reportRequestFailure`) decides.

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

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ let _snapshotRootOwner: Owner | null = null;
306306
// The boundary owner whose resume window is open (null during a root pass,
307307
// which claims everything). Reached as `sharedConfig.isClaiming`.
308308
let _claimOwner: Owner | null = null;
309+
// The claim window over no owner: a hydrated `<Errored>`'s content run,
310+
// where nothing claims (hydratedCreateErrorBoundary).
311+
const NO_CLAIM = {} as Owner;
309312
function isClaiming(): boolean {
310313
if (!_claimOwner) return true;
311314
let owner: Owner | null = getOwner();
@@ -1293,9 +1296,7 @@ function hydratedCreateErrorBoundary<T, U>(
12931296
// A later run claims nothing either way: claims are read only while
12941297
// hydrating.
12951298
() =>
1296-
_hydratingValue
1297-
? hydrateWindow("", fn, undefined, ((o = getOwner()), {} as Owner))
1298-
: fn(),
1299+
_hydratingValue ? hydrateWindow("", fn, undefined, ((o = getOwner()), NO_CLAIM)) : fn(),
12991300
(e, reset) => ((o = 0), fallback(e, reset))
13001301
);
13011302
// No fallback yet: the content ran clean. Hold the server's fallback
@@ -2859,7 +2860,18 @@ function hydratedCreateLoadingBoundary<T, U>(
28592860
// A parked fragment has settled but not swapped: it waits like a pending one.
28602861
const s = fr && typeof fr === "object" ? (fr.s === 1 && fragmentParked(id) ? 0 : fr.s) : 0;
28612862

2862-
if (s === 1 && !assetPromise && !fragmentSuperseded(id)) {
2863+
// Inside a hydrated `<Errored>`'s content run, which claims no DOM (the
2864+
// markup is the server's fallback for the failure, #4287), the
2865+
// fragment is never claimed: the content runs now with its data
2866+
// adopted, so the Errored holds the client's own error and tracks its
2867+
// inputs (#4273).
2868+
const unclaimed = _claimOwner === NO_CLAIM;
2869+
// Nothing else consumes a rejected `_fr` promise then — swallow it so
2870+
// a rejected fragment (error finalize or a client-hole handoff) doesn't
2871+
// surface as an unhandled rejection.
2872+
if (s === 2 || unclaimed) (fr as Promise<never>).catch?.(noop);
2873+
2874+
if (unclaimed || (s === 1 && !assetPromise && !fragmentSuperseded(id))) {
28632875
// Fragment already settled and swapped in ($df ran before hydration):
28642876
// the content is in the DOM, so hydrate straight through. The fallback
28652877
// only hydrates when it is actually showing — rendering it here would
@@ -2892,13 +2904,9 @@ function hydratedCreateLoadingBoundary<T, U>(
28922904

28932905
if (s === 1 || s === 2) {
28942906
if (s === 2) {
2895-
// Rejected stream fragments swap to an empty template; any outer error fallback
2896-
// has to be created as fresh client DOM, not claimed from server markup.
2897-
// Nothing else consumes the settled-rejected `_fr` promise once this
2898-
// branch takes over — swallow it so a rejected fragment (error
2899-
// finalize or a client-hole handoff) doesn't surface as an
2900-
// unhandled rejection.
2901-
(fr as Promise<never>).catch?.(noop);
2907+
// Rejected stream fragments swap to an empty template (or, under a
2908+
// hydrated `<Errored>`, the fallback it claims — above); any outer
2909+
// error fallback here is created as fresh client DOM.
29022910
// Observe: the fresh render is a "recovery" record; prod folds the
29032911
// branch and `recover` with it.
29042912
waitAndResume(
@@ -2920,6 +2928,12 @@ function hydratedCreateLoadingBoundary<T, U>(
29202928
// for this resume to claim instead of holding it; if the swap already
29212929
// arrived and was held awaiting a claimant, replay it now.
29222930
claimFragment(id);
2931+
const hy = (globalThis as any)._$HY;
2932+
// Revealed ahead of its `_fr` settlement (two scripts on the wire; an
2933+
// async entry can run between them, #4074): the fallback is gone and
2934+
// the content is in the DOM, so render nothing until the resume
2935+
// adopts it.
2936+
const revealed = hy.v?.[id];
29232937
waitAndResume(
29242938
fr,
29252939
shouldHydrate =>
@@ -2934,18 +2948,18 @@ function hydratedCreateLoadingBoundary<T, U>(
29342948
const assets = boundaryAssets(id);
29352949
assets ? waitAndResume(true, resume, assets) : resume(true);
29362950
})
2937-
: IS_OBSERVE
2938-
? recover(id, registeredAt, resume)
2939-
: resume(false),
2951+
: // Rendered fresh. A rejected fragment swapped in over the
2952+
// claimed fallback left its nodes — the server's outcome for
2953+
// the failure, which `$dfr` lists in the reveal record
2954+
// (#4287) — beside the range the fresh render takes: clear
2955+
// them first.
2956+
(revealed || hy.v?.[id]?.forEach?.((n: ChildNode) => n.remove()),
2957+
IS_OBSERVE ? recover(id, registeredAt, resume) : resume(false)),
29402958
assetPromise,
29412959
false,
29422960
fragmentAbort(id)
29432961
);
2944-
// Revealed ahead of its `_fr` settlement (two scripts on the wire; an
2945-
// async entry can run between them, #4074): the fallback is gone and
2946-
// the content is in the DOM, so render nothing until the resume
2947-
// adopts it.
2948-
return (globalThis as any)._$HY.v?.[id] ? undefined : fallback();
2962+
return revealed ? undefined : fallback();
29492963
}
29502964

29512965
if (assetPromise && !sharedConfig.has!(id)) {

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,12 @@ function ssrLoadingBoundary(
361361

362362
// What the fragment carries for a failure once it is registered (its
363363
// channel owns the routing — see runLoadingPhase). Outside a server
364-
// component: nothing — the blank the client twin renders fresh over, as
365-
// always (`hydratedCreateLoadingBoundary`'s rejected arm). Inside one
364+
// component (asked with this boundary's id, #4287): the fallback of an
365+
// `<Errored>` this boundary is all the content of — drawn where the client
366+
// twin draws it, its error serialized at the Errored id so the hydrating
367+
// client adopts that markup; otherwise nothing — the blank the client
368+
// twin renders fresh over (`hydratedCreateLoadingBoundary`'s rejected
369+
// arm). Inside one
366370
// (frames-rulings 3.3, A0 corollary 4 inward — the position shows what the
367371
// SERVER rendered for the outcome, never a blank, never a client-invented
368372
// state; there is no twin): the nearest server `<Errored>`'s fallback for
@@ -375,10 +379,10 @@ function ssrLoadingBoundary(
375379
// hydration-free scope's; its head and asset registrations drop with the
376380
// error, as the error path drops them today.
377381
function errorOutcome(err: any): { value?: string; escaped?: { frame?: string } } | undefined {
378-
if (!inServerComponentScope(o as any)) return undefined;
379-
const rendered = parentHandler ? parentHandler(err, true) : undefined;
382+
const scoped = inServerComponentScope(o as any);
383+
const rendered = parentHandler ? parentHandler(err, scoped || id) : undefined;
380384
if (typeof rendered === "string") return { value: rendered };
381-
return { value: plainFallback(), escaped: { frame: ctx.frameId } };
385+
if (scoped) return { value: plainFallback(), escaped: { frame: ctx.frameId } };
382386
}
383387
// Settle the fragment with the failure. The server error hook hears of it
384388
// first (the `_fr` rejection and a transport sink's error chunk read the
@@ -412,7 +416,7 @@ function ssrLoadingBoundary(
412416
(err: any, handler, outcome) => {
413417
// The outcome question (see errorOutcome) is a `<Loading>`'s to pass
414418
// up: it owns no fallback for an error.
415-
if (outcome) return handler ? handler(err, true) : undefined;
419+
if (outcome !== undefined) return handler ? handler(err, outcome) : undefined;
416420
handledRenderError = err;
417421
if (done) {
418422
// Once the fragment is registered, its channel owns error routing:

0 commit comments

Comments
 (0)