Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/errored-async-outcome-4287.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"solid-js": patch
"@solidjs/web": patch
---

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).
2 changes: 1 addition & 1 deletion documentation/solid-2.0/12-ssr-http.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ renderToStream(code, { onError });
handleServerFunctionRequest(request, { onError });
```

- **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"`.
- **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"`.
- **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.
- **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.
- **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.
Expand Down
52 changes: 33 additions & 19 deletions packages/solid/src/client/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ let _snapshotRootOwner: Owner | null = null;
// The boundary owner whose resume window is open (null during a root pass,
// which claims everything). Reached as `sharedConfig.isClaiming`.
let _claimOwner: Owner | null = null;
// The claim window over no owner: a hydrated `<Errored>`'s content run,
// where nothing claims (hydratedCreateErrorBoundary).
const NO_CLAIM = {} as Owner;
function isClaiming(): boolean {
if (!_claimOwner) return true;
let owner: Owner | null = getOwner();
Expand Down Expand Up @@ -1293,9 +1296,7 @@ function hydratedCreateErrorBoundary<T, U>(
// A later run claims nothing either way: claims are read only while
// hydrating.
() =>
_hydratingValue
? hydrateWindow("", fn, undefined, ((o = getOwner()), {} as Owner))
: fn(),
_hydratingValue ? hydrateWindow("", fn, undefined, ((o = getOwner()), NO_CLAIM)) : fn(),
(e, reset) => ((o = 0), fallback(e, reset))
);
// No fallback yet: the content ran clean. Hold the server's fallback
Expand Down Expand Up @@ -2853,7 +2854,18 @@ function hydratedCreateLoadingBoundary<T, U>(
// A parked fragment has settled but not swapped: it waits like a pending one.
const s = fr && typeof fr === "object" ? (fr.s === 1 && fragmentParked(id) ? 0 : fr.s) : 0;

if (s === 1 && !assetPromise && !fragmentSuperseded(id)) {
// Inside a hydrated `<Errored>`'s content run, which claims no DOM (the
// markup is the server's fallback for the failure, #4287), the
// fragment is never claimed: the content runs now with its data
// adopted, so the Errored holds the client's own error and tracks its
// inputs (#4273).
const unclaimed = _claimOwner === NO_CLAIM;
// Nothing else consumes a rejected `_fr` promise then — swallow it so
// a rejected fragment (error finalize or a client-hole handoff) doesn't
// surface as an unhandled rejection.
if (s === 2 || unclaimed) (fr as Promise<never>).catch?.(noop);

if (unclaimed || (s === 1 && !assetPromise && !fragmentSuperseded(id))) {
// Fragment already settled and swapped in ($df ran before hydration):
// the content is in the DOM, so hydrate straight through. The fallback
// only hydrates when it is actually showing — rendering it here would
Expand Down Expand Up @@ -2895,13 +2907,9 @@ function hydratedCreateLoadingBoundary<T, U>(

if (s === 1 || s === 2) {
if (s === 2) {
// Rejected stream fragments swap to an empty template; any outer error fallback
// has to be created as fresh client DOM, not claimed from server markup.
// Nothing else consumes the settled-rejected `_fr` promise once this
// branch takes over — swallow it so a rejected fragment (error
// finalize or a client-hole handoff) doesn't surface as an
// unhandled rejection.
(fr as Promise<never>).catch?.(noop);
// Rejected stream fragments swap to an empty template (or, under a
// hydrated `<Errored>`, the fallback it claims — above); any outer
// error fallback here is created as fresh client DOM.
// Observe: the fresh render is a "recovery" record; prod folds the
// branch and `recover` with it.
const resumeRejected = IS_OBSERVE
Expand Down Expand Up @@ -2930,6 +2938,12 @@ function hydratedCreateLoadingBoundary<T, U>(
// for this resume to claim instead of holding it; if the swap already
// arrived and was held awaiting a claimant, replay it now.
claimFragment(id);
const hy = (globalThis as any)._$HY;
// Revealed ahead of its `_fr` settlement (two scripts on the wire; an
// async entry can run between them, #4074): the fallback is gone and
// the content is in the DOM, so render nothing until the resume
// adopts it.
const revealed = hy.v?.[id];
waitAndResume(
fr,
shouldHydrate =>
Expand All @@ -2944,18 +2958,18 @@ function hydratedCreateLoadingBoundary<T, U>(
const assets = boundaryAssets(id);
assets ? waitAndResume(true, resume, assets) : resume(true);
})
: IS_OBSERVE
? recover(id, registeredAt, resume)
: resume(false),
: // Rendered fresh. A rejected fragment swapped in over the
// claimed fallback left its nodes — the server's outcome for
// the failure, which `$dfr` lists in the reveal record
// (#4287) — beside the range the fresh render takes: clear
// them first.
(revealed || hy.v?.[id]?.forEach?.((n: ChildNode) => n.remove()),
IS_OBSERVE ? recover(id, registeredAt, resume) : resume(false)),
assetPromise,
false,
fragmentAbort(id)
);
// Revealed ahead of its `_fr` settlement (two scripts on the wire; an
// async entry can run between them, #4074): the fallback is gone and
// the content is in the DOM, so render nothing until the resume
// adopts it.
return (globalThis as any)._$HY.v?.[id] ? undefined : fallback();
return revealed ? undefined : fallback();
}

if (assetPromise && !sharedConfig.has!(id)) {
Expand Down
16 changes: 10 additions & 6 deletions packages/solid/src/server/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,12 @@ function ssrLoadingBoundary(

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