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
8 changes: 8 additions & 0 deletions .changeset/errored-nested-loading-preflush-fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"solid-js": patch
"@solidjs/web": patch
---

During `renderToStream`, an `<Errored>` whose `<Loading>` fails before the shell is sent now draws its error fallback in the shell even when the `<Loading>` is not all of its content: markup between them (`<Errored><section><Loading>`), a sibling beside the `<Loading>`, or another `<Loading>` inside the `<Errored>`. The fallback replaces the `<Errored>`'s whole content, as a client render shows it, and the error is serialized at the `<Errored>`, so the hydrating client adopts that markup without asking again and recovers when the content's inputs change (#4287). The server error hook hears `fallback`. Before, the shell kept the content with the failed `<Loading>`'s position empty (`<b></b>` for `<Errored><b><Loading>`), and the fallback appeared only once the client hydrated, or never without JavaScript. The same applies to the awaited form (`await renderToStream(...)`).

The fallback is drawn only when it is ready as it renders; the shell never waits for it. A fallback with an async or client-only hole, or one that throws with no `<Errored>` above it, leaves the shell as before: the client renders the failure, and the hook hears `client`. So does an `<Errored>` whose markup has no place of its own in the shell: one that put no element and no `<Loading>` placeholder into the page (text alone, or nothing), or that was itself never inserted. A fallback that throws inside an outer `<Errored>` is that boundary's failure, drawn in the shell the same way. When the hook maps the error to another value, a fallback that reads the error is rendered again with that value; if that second render throws or is not ready, nothing is drawn, although the hook has already heard `fallback`. Whatever the replaced content still had pending goes with it. A `<Loading>` still pending inside it streams nothing, and its head tags (`useHead`) never reach the document head, whether they come from a settled `<Loading>` or from the content itself. A later failure inside the replaced content is still reported to the hook as `client`. After the shell has been sent, a nested `<Loading>` failure still streams an empty fragment and the client renders the failure, as before.
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 @@ -199,7 +199,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 — 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 — reported for a render that passed `onError`, as seroval's own `onError` always was. With the default serializer, synchronous and asynchronous failures follow the same rule **before the first flush** ([#4709](https://github.com/solidjs/solid/issues/4709)) — an awaited `renderToStream`, or a stream whose shell has not gone out — the render **fails** like a rejection there: nothing is sent, `onCompleteAll` does not fire, and the hook hears only `serialize`; **after the first flush** (midstream, where the response cannot be failed cleanly) the key — and any promise or async iterable inside its value that already went out — rejects on the client with an error that says the value could not be serialized and names the key, so the reading computation throws into the nearest `<Errored>`, whose fallback replaces the server's content until `reset()` or an input change reruns it as a client fetch — uncaught like any rejected key with no `<Errored>` above — and the render completes ([#4647](https://github.com/solidjs/solid/issues/4647)). A synchronous failure after the first flush still completes the render: the value is omitted if nothing of it was published, or rejected if part of it already went out ([#4227](https://github.com/solidjs/solid/issues/4227)). An error an `<Errored>` contained is not such a value. When the default serializer cannot encode it (a field it cannot serialize, kept by the dev tier's fidelity, by `markSafeError` or by a hook's mapping), its record and the rejection of the source it came from, `deferStream` or streamed, are sent as a plain `Error` with its message: the fallback stays and the render completes, every record of the same error carries the same replacement, and the hook hears `serialize` once. A rejection no `<Errored>` contained is a value like any other and fails the render before the first flush. A custom `serializer`'s late failure is its own to settle; the render completes. `renderToString` refuses async values outright, so it has no late case). `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). Before the shell is sent, an `<Errored>` with other content around the `<Loading>` does the same in the shell, its fallback in place of all that content, when the fallback is ready without waiting and the `<Errored>` put an element or a `<Loading>` placeholder of its own into the page), `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 — reported for a render that passed `onError`, as seroval's own `onError` always was. With the default serializer, synchronous and asynchronous failures follow the same rule **before the first flush** ([#4709](https://github.com/solidjs/solid/issues/4709)) — an awaited `renderToStream`, or a stream whose shell has not gone out — the render **fails** like a rejection there: nothing is sent, `onCompleteAll` does not fire, and the hook hears only `serialize`; **after the first flush** (midstream, where the response cannot be failed cleanly) the key — and any promise or async iterable inside its value that already went out — rejects on the client with an error that says the value could not be serialized and names the key, so the reading computation throws into the nearest `<Errored>`, whose fallback replaces the server's content until `reset()` or an input change reruns it as a client fetch — uncaught like any rejected key with no `<Errored>` above — and the render completes ([#4647](https://github.com/solidjs/solid/issues/4647)). A synchronous failure after the first flush still completes the render: the value is omitted if nothing of it was published, or rejected if part of it already went out ([#4227](https://github.com/solidjs/solid/issues/4227)). An error an `<Errored>` contained is not such a value. When the default serializer cannot encode it (a field it cannot serialize, kept by the dev tier's fidelity, by `markSafeError` or by a hook's mapping), its record and the rejection of the source it came from, `deferStream` or streamed, are sent as a plain `Error` with its message: the fallback stays and the render completes, every record of the same error carries the same replacement, and the hook hears `serialize` once. A rejection no `<Errored>` contained is a value like any other and fails the render before the first flush. A custom `serializer`'s late failure is its own to settle; the render completes. `renderToString` refuses async values outright, so it has no late case). `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`, which has no wire, and for `serialize`, whose only wire — the midstream key rejection ([#4647](https://github.com/solidjs/solid/issues/4647)) — is the fixed message naming the key, never the serializer's error or a mapping of it. 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
20 changes: 19 additions & 1 deletion packages/solid/src/server/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ function ssrLoadingBoundary(
// 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
// arm), unless the shell is still held, where an enclosing `<Errored>`
// draws in place of its own markup (`failFragment`). 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 Down Expand Up @@ -446,6 +447,23 @@ function ssrLoadingBoundary(
outcomeRendered = true;
return true;
}
// Before the shell is handed to the sink, with nothing drawn at this
// position: an enclosing `<Errored>` whose markup is in the shell
// meets the failure there when its fallback is finished as it
// renders, the fallback in place of that markup and of whatever sits
// between the two boundaries, as on the client (#4287). Settled first,
// as above. Not met: the paths below, where settling again changes
// nothing.
if (!outcome && parentHandler && ctx.shell && ctx.shell.open) {
done!(undefined, err);
// Met only by a boundary that contained it: one that absorbs it
// (its subtree already discarded) leaves it reported as before.
const contained = ctx.shell.contained;
if (parentHandler(err, ESCALATE) === true && ctx.shell.contained !== contained) {
outcomeRendered = true;
return true;
}
}
if (report && !outcomeRendered) {
reportServerError(
err,
Expand Down
7 changes: 6 additions & 1 deletion packages/solid/src/server/shared.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getOwner, getNextChildId, getContext, devPeekNextChildId } from "./signals.js";
import type { Context } from "./signals.js";
import type { Context, ShellContainer } from "./signals.js";
import type { BoundaryEvent } from "./observe.js";
import type { NavigationRef, Observe } from "@solidjs/signals";

Expand Down Expand Up @@ -140,6 +140,11 @@ export type HydrationContext = {
* renderToStream.
*/
flushed?: () => boolean;
/**
* @internal The shell while the renderer still holds it (see
* `ShellContainer`). Set by @solidjs/web's renderToStream.
*/
shell?: ShellContainer;
/**
* @internal Per-request memo of `resolveAssets(moduleUrl)` results, keyed
* by moduleUrl. lazy() consults it so a component re-created across
Expand Down
Loading
Loading