Skip to content

Commit f14ce8d

Browse files
committed
fix(solid): a fragment failing while parked in a pending fragment goes to the nearest boundary (#4316)
A <Loading> that failed after the shell while its fragment was parked in an enclosing <Loading> still pending was settled as a blank region that reported success. The renderer (web/src/server.ts registerFragment) parks a settled child into its pending parent and resolves its `_fr` whether or not it failed. Nothing upstream heard of the failure: the error was routed only to an <Errored> that could draw at exactly that position (errorOutcome). The client then hydrated the blank region as success and halted or drew nothing. Now the failure is the enclosing fragment's content's, as when the same read fails in that fragment's own render pass. Handlers get an escalation mode (ESCALATE, server-internal) for a failure that surfaced after the render passed them: - A <Loading> whose fragment is still pending fails it, the way a hole failure in its own pass does. - An <Errored> whose finished markup sits raw in such a fragment renders its fallback in place of that markup (FragmentContainer.region): the fallback is waited for, its error is serialized once it is finished, and its own failure goes up the same way. An <Errored> still pending shows the fallback on its next pull, and one already showing its fallback absorbs the failure. - A boundary already settled with a failure absorbs it. - Otherwise the failure goes to the next boundary up. The enclosing <Loading> applies the regions to its markup before it settles, outermost first. If a region is not found, or its fallback never finishes, that fragment fails with the region's error. This replaces the `escalating` counter from 7ce3c98: an <Errored> fallback that fails while the server waits for it uses the same mode, so it is drawn one level up too. Not covered: - The late case. If the enclosing fragment settled before the inner read failed, its markup is already out. The inner fragment rejects on its own and the client renders it, and the client's <Errored> meets the failure, unchanged. - An <Errored> inside a nested fragment that settled into the pending one first. Its markup is out of the enclosing fragment's own markup, so the enclosing fragment fails instead of drawing. Tests: parked shapes with an <Errored> between, around the inner <Loading> with a failing fallback, and around everything, each against a twin with no inner <Loading> (the failure met in the enclosing pass). The server harness holds the stream text to the twin's. The hydrate spec, loaded and streamed, holds the client outcome to the twin's and checks that the drawn fallback is claimed. Server specs cover the late case and a nested fragment that settled first. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 7ce3c98 commit f14ce8d

25 files changed

Lines changed: 625 additions & 35 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Server: a `<Loading>` that fails after the shell while its fragment waits inside an outer `<Loading>` that is still pending no longer arrives as a blank region reported as a success. The failure now goes to the nearest enclosing boundary, as it does when the same read fails inside the outer `<Loading>`'s own render. An `<Errored>` between them draws its fallback in the outer fragment, the client adopts it, and its error is serialized with it. With no `<Errored>` between them, the outer fragment fails, and an `<Errored>` whose whole content is that outer `<Loading>` draws its fallback there. This also covers an `<Errored>` fallback that fails in that position. Before, the client hydrated the blank region and either halted or rendered nothing there (#4316).

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

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import {
1616
onCleanup,
1717
inServerComponentScope,
1818
edgeHooks,
19-
MAX_RESUME_PASSES
19+
MAX_RESUME_PASSES,
20+
ESCALATE,
21+
fragmentContainer,
22+
setFragmentContainer,
23+
type FragmentContainer
2024
} from "./signals.js";
2125
import { OBSERVE } from "@solidjs/signals";
2226
import { sharedConfig, NoHydrateContext, callerRenderContext } from "./shared.js";
@@ -155,6 +159,21 @@ function ssrLoadingBoundary(
155159
(o as any).id = contentId;
156160

157161
let done: ReturnType<HydrationContext["registerFragment"]> | undefined;
162+
// This boundary's fragment as the boundaries rendered into it see it
163+
// (`FragmentContainer`): open from registration until it settles, and
164+
// the descendant `<Errored>` fallbacks to draw in place of their markup
165+
// when it does (#4316). `discarded` once it settled with a failure: a
166+
// later failure from inside it has nothing left to fail.
167+
const container: FragmentContainer = {
168+
open: false,
169+
region: (from, to, err) => void regions.push({ from, to, err })
170+
};
171+
let regions: {
172+
from: string;
173+
to: string | undefined | (() => Promise<string | undefined>);
174+
err: any;
175+
}[] = [];
176+
let discarded = false;
158177
let handledRenderError: any;
159178
let retryPromise: Promise<any> | undefined;
160179

@@ -406,7 +425,21 @@ function ssrLoadingBoundary(
406425
// errorOutcome): the fragment settles when it lands.
407426
function failFragment(err: any, report: boolean, wait?: boolean): boolean | Promise<boolean> {
408427
const settle = (outcome: Outcome) => {
428+
discarded = true;
409429
outcomeRendered = !!outcome && outcome.value !== undefined && !outcome.escaped;
430+
// Parked in an enclosing fragment still pending, with nothing drawn at
431+
// this position: the failure is that fragment's content's, as a
432+
// failure in its own pass would be (#4316) — the nearest boundary up
433+
// meets it there (an `<Errored>` draws its fallback in place of its
434+
// markup; else the enclosing fragment fails). Settled first, so this
435+
// fragment parks before the enclosing one settles; the boundary that
436+
// meets it reports it.
437+
if (!outcome && parentHandler && fragmentContainer(parent, true)) {
438+
done!(undefined, err);
439+
parentHandler(err, ESCALATE);
440+
outcomeRendered = true;
441+
return true;
442+
}
410443
if (report && !outcomeRendered) {
411444
reportServerError(
412445
err,
@@ -428,6 +461,15 @@ function ssrLoadingBoundary(
428461
o,
429462
render,
430463
(err: any, handler, outcome) => {
464+
// A failure from inside this boundary's content that surfaced after
465+
// its pass (see `ESCALATE`): still pending, the fragment fails with
466+
// it; already failed, there is nothing left to fail; otherwise its
467+
// content is out or rendered into the enclosing output — up.
468+
if (outcome === ESCALATE) {
469+
if (container.open) return failOpen(err);
470+
if (discarded) return true;
471+
return handler ? handler(err, ESCALATE) : undefined;
472+
}
431473
// The outcome question (see errorOutcome) is a `<Loading>`'s to pass
432474
// up: it owns no fallback for an error.
433475
if (outcome !== undefined) return handler ? handler(err, outcome) : undefined;
@@ -464,6 +506,44 @@ function ssrLoadingBoundary(
464506
);
465507
}
466508

509+
// An escalated failure fails the fragment while it is still pending, as a
510+
// failure in its own pass does (the hole path above); the resume loop
511+
// stops at its next wake.
512+
function failOpen(err: any): true {
513+
container.open = false;
514+
discarded = true;
515+
const settling = failFragment(err, true, true);
516+
const settled = (streamed: boolean) => {
517+
if (!outcomeRendered) reportRouted(err, "client");
518+
record("error", streamed, err);
519+
};
520+
settling instanceof Promise ? settling.then(settled) : settled(settling);
521+
return true;
522+
}
523+
524+
// The settled markup with each descendant `<Errored>` fallback drawn in
525+
// place of its markup (`container.region`), waited for until finished.
526+
// Outermost first: a region inside one already replaced has gone with it.
527+
// A region not found, or a fallback that never finished, fails the
528+
// fragment with the region's error instead (thrown to the caller).
529+
async function drawRegions(value: string): Promise<string> {
530+
for (let i = 0; i < regions.length; i++) {
531+
const r = regions[i];
532+
if (typeof r.to === "function") r.to = await r.to();
533+
if (!container.open) return value;
534+
}
535+
const drawn: string[] = [];
536+
for (let i = regions.length - 1; i >= 0; i--) {
537+
const { from, to, err } = regions[i];
538+
const at = value.indexOf(from);
539+
if (at < 0 && drawn.some(d => d.includes(from))) continue;
540+
if (at < 0 || to === undefined) throw err;
541+
value = value.slice(0, at) + to + value.slice(at + from.length);
542+
drawn.push(from);
543+
}
544+
return value;
545+
}
546+
467547
// Only ever called from the async resume loop below — there is nothing on
468548
// the stack to catch a throw from here. The initial (synchronous) discovery
469549
// pass propagates unhandled errors directly out of runLoadingPhase to the
@@ -561,6 +641,7 @@ function ssrLoadingBoundary(
561641
function runDiscovery(): SSRTemplateObject | undefined {
562642
disposeOwner(o, false);
563643
serializeBuffer = [];
644+
regions = [];
564645
retryPromise = undefined;
565646
resolveCount = 0;
566647
passes++;
@@ -672,7 +753,10 @@ function ssrLoadingBoundary(
672753

673754
if (ctx.async) {
674755
const regOpts = revealGroup ? { revealGroup: revealGroup.id } : undefined;
675-
done = ctx.registerFragment(id, regOpts);
756+
const settle = ctx.registerFragment(id, regOpts);
757+
done = (value, error, escaped) => ((container.open = false), settle(value, error, escaped));
758+
container.open = true;
759+
setFragmentContainer(o, container);
676760
let owners = fragmentOwners.get(ctx.registerFragment);
677761
if (!owners) fragmentOwners.set(ctx.registerFragment, (owners = {}));
678762
owners[id] = done;
@@ -683,7 +767,11 @@ function ssrLoadingBoundary(
683767
// registered in the same re-run owns the entry and its reveal slot.
684768
let disposed = false;
685769
let superseded = false;
686-
onCleanup(() => (disposed = true));
770+
onCleanup(() => {
771+
disposed = true;
772+
container.open = false;
773+
setFragmentContainer(o, null);
774+
});
687775
const abandon = () => {
688776
if (owners![id] !== done) return (superseded = true);
689777
delete owners![id];
@@ -739,6 +827,7 @@ function ssrLoadingBoundary(
739827
if (IS_DEV) waits!.start([retryPromise]);
740828
await retryPromise.catch(() => {});
741829
if (disposed) return abandon();
830+
if (discarded) return;
742831
if (IS_DEV) waits!.landed();
743832
ret = runDiscovery();
744833
}
@@ -750,12 +839,19 @@ function ssrLoadingBoundary(
750839
if (IS_DEV) waits!.start(pending.p);
751840
await Promise.all(pending.p).catch(() => {});
752841
if (disposed) return abandon();
842+
if (discarded) return;
753843
if (IS_DEV) waits!.landed();
754844
passes++;
755845
ret = runLoadingPhase(() => resolveIn(() => ctx.ssr(pending.t, ...pending.h))) as any;
756846
}
757847
flushSerializeBuffer();
758-
const streamed = done!(ret && Array.isArray(ret.t) ? ret.t[0] : ((ret && ret.t) as any));
848+
let value = ret && Array.isArray(ret.t) ? ret.t[0] : ((ret && ret.t) as any);
849+
if (regions.length) {
850+
value = await drawRegions(value);
851+
if (disposed) return abandon();
852+
if (discarded) return;
853+
}
854+
const streamed = done!(value);
759855
record("settled", streamed);
760856
} catch (err) {
761857
// A failure whose outcome is still resolving settles when it lands.

0 commit comments

Comments
 (0)