Skip to content

Commit 6c35714

Browse files
committed
Merge origin/next into claude/project-thread-x1ore2-hydrate-reask
Keeps both sides of the harness scenarios and the 4306 spec imports. #4318's new artifacts and its rejected-fragment expectations take this branch's empty outcome template (`e></template>`, no lone space). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018LY7F9czoRqdMEvk2QwPr1
2 parents c2e38f5 + 8f1d9d2 commit 6c35714

40 files changed

Lines changed: 1302 additions & 25 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Server: when an `<Errored>` renders its fallback for a `<Loading>` that failed after the shell, and a hole in that fallback rejects while the server waits for it, the rejection now goes to the next boundary up, as it does when a fallback throws synchronously. Inside a pending outer `<Loading>`, that outer fragment fails; under an outer `<Errored>` whose whole content is that position, the outer fallback is drawn there and the client adopts it. Before, the region streamed blank and the hydrating client halted (#4316). A synchronous throw in that fallback no longer writes an error record for an outer `<Errored>` without sending its fallback.
6+
7+
The server's wait for that fallback now stops after the same pass budget as the `<Loading>` resume loop and then streams what it has. It also stops as soon as the render is torn down, for example when the request is aborted (#4317).
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+
An `<Errored>` whose fallback fails while rendering (a hole that rejects, or a synchronous throw) now passes that failure to the enclosing boundary on the server, in the shell as well as in a streamed fragment. An outer `<Errored>` draws its fallback in place of the region, and with none the request fails. Before, the region was sent blank, with the errors recorded and no markup for them.
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: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ import {
1515
ownerId,
1616
onCleanup,
1717
inServerComponentScope,
18-
edgeHooks
18+
edgeHooks,
19+
MAX_RESUME_PASSES,
20+
ESCALATE,
21+
fragmentContainer,
22+
setFragmentContainer,
23+
type FragmentContainer
1924
} from "./signals.js";
2025
import { OBSERVE } from "@solidjs/signals";
2126
import { sharedConfig, NoHydrateContext, callerRenderContext } from "./shared.js";
@@ -154,6 +159,21 @@ function ssrLoadingBoundary(
154159
(o as any).id = contentId;
155160

156161
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;
157177
let handledRenderError: any;
158178
let retryPromise: Promise<any> | undefined;
159179

@@ -405,7 +425,21 @@ function ssrLoadingBoundary(
405425
// errorOutcome): the fragment settles when it lands.
406426
function failFragment(err: any, report: boolean, wait?: boolean): boolean | Promise<boolean> {
407427
const settle = (outcome: Outcome) => {
428+
discarded = true;
408429
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+
}
409443
if (report && !outcomeRendered) {
410444
reportServerError(
411445
err,
@@ -427,6 +461,15 @@ function ssrLoadingBoundary(
427461
o,
428462
render,
429463
(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+
}
430473
// The outcome question (see errorOutcome) is a `<Loading>`'s to pass
431474
// up: it owns no fallback for an error.
432475
if (outcome !== undefined) return handler ? handler(err, outcome) : undefined;
@@ -463,6 +506,44 @@ function ssrLoadingBoundary(
463506
);
464507
}
465508

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+
466547
// Only ever called from the async resume loop below — there is nothing on
467548
// the stack to catch a throw from here. The initial (synchronous) discovery
468549
// pass propagates unhandled errors directly out of runLoadingPhase to the
@@ -560,6 +641,7 @@ function ssrLoadingBoundary(
560641
function runDiscovery(): SSRTemplateObject | undefined {
561642
disposeOwner(o, false);
562643
serializeBuffer = [];
644+
regions = [];
563645
retryPromise = undefined;
564646
resolveCount = 0;
565647
passes++;
@@ -671,7 +753,10 @@ function ssrLoadingBoundary(
671753

672754
if (ctx.async) {
673755
const regOpts = revealGroup ? { revealGroup: revealGroup.id } : undefined;
674-
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);
675760
let owners = fragmentOwners.get(ctx.registerFragment);
676761
if (!owners) fragmentOwners.set(ctx.registerFragment, (owners = {}));
677762
owners[id] = done;
@@ -682,7 +767,11 @@ function ssrLoadingBoundary(
682767
// registered in the same re-run owns the entry and its reveal slot.
683768
let disposed = false;
684769
let superseded = false;
685-
onCleanup(() => (disposed = true));
770+
onCleanup(() => {
771+
disposed = true;
772+
container.open = false;
773+
setFragmentContainer(o, null);
774+
});
686775
const abandon = () => {
687776
if (owners![id] !== done) return (superseded = true);
688777
delete owners![id];
@@ -717,15 +806,15 @@ function ssrLoadingBoundary(
717806
};
718807
(async () => {
719808
try {
720-
// Convergence budget: each pass should retire at least one async
721-
// slot, so passes are bounded by the boundary's async-slot count in
722-
// any converging render — real trees sit far below this. A shape
809+
// Convergence budget (`MAX_RESUME_PASSES`): each pass should retire
810+
// at least one async slot, so passes are bounded by the boundary's
811+
// async-slot count in any converging render. A shape
723812
// that plants a fresh pending source every pass (an async read whose
724813
// answer is never adoptable at the re-created slot) would otherwise
725814
// loop at microtask speed, serializing a new deferred per pass until
726815
// the process OOMs (#3003). Fail the boundary loudly instead.
727816
const checkBudget = () => {
728-
if (passes <= 10000) return;
817+
if (passes <= MAX_RESUME_PASSES) return;
729818
throw new Error(
730819
`<Loading> boundary discovery did not converge after ${passes} passes — ` +
731820
`an async source produces a new pending answer on every retry. Ensure repeated ` +
@@ -738,6 +827,7 @@ function ssrLoadingBoundary(
738827
if (IS_DEV) waits!.start([retryPromise]);
739828
await retryPromise.catch(() => {});
740829
if (disposed) return abandon();
830+
if (discarded) return;
741831
if (IS_DEV) waits!.landed();
742832
ret = runDiscovery();
743833
}
@@ -749,12 +839,19 @@ function ssrLoadingBoundary(
749839
if (IS_DEV) waits!.start(pending.p);
750840
await Promise.all(pending.p).catch(() => {});
751841
if (disposed) return abandon();
842+
if (discarded) return;
752843
if (IS_DEV) waits!.landed();
753844
passes++;
754845
ret = runLoadingPhase(() => resolveIn(() => ctx.ssr(pending.t, ...pending.h))) as any;
755846
}
756847
flushSerializeBuffer();
757-
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);
758855
record("settled", streamed);
759856
} catch (err) {
760857
// A failure whose outcome is still resolving settles when it lands.

0 commit comments

Comments
 (0)