Skip to content

fix(hydration): discard the server tail of an adopted component range - #1576

Merged
trueadm merged 2 commits into
mainfrom
fix/adopted-component-tail
Oct 2, 2026
Merged

trueadm merged 2 commits into
mainfrom
fix/adopted-component-tail

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

During hydration, a component's server range can hold more than the client renders into it, even when server and client run the same source:

  • a component whose identity differs on the client (const C = props.server ? A : B; <C />);
  • a hookless component in a renderable hole ({props.server ? <A /> : <B />}, lite frames in a dev compile);
  • a component that returns less on the client (if (props.server) return <><i/><s/></>; return <i/>;), rendered into its return slot's range;
  • a component rendered as a de-opt list item, which lends its item range to the component.

When the server's content starts with what the client renders, the client adopted that prefix and kept the rest on screen, with no report.

Now the first render into an adopted range opens a claim. The template that adopts the range's first node records where its roots end (claimRoots). After the render, hydration removes the server content left before the range's end and reports it once: code 51 through onRecoverableError, plus a located development warning. The nodes the client adopted keep their identity.

Claims cover:

  • componentSlotImpl's adopted frame;
  • componentSlotLite's adopted frame (the lite scope is matched through liteRanges);
  • childSlot's adopted and lent ranges whose value is a component. claimRange already settles a host value's range, so hosts are skipped.

To find where a multi-root template's roots end without parsing it in production, the compiler now passes the template's root count as template()'s frag argument. The runtime still reads frag only as truthy elsewhere.

A passthrough root's owner (HYDRATION_RANGE_BOUNDARY) is excluded. By design it adopts whatever range the server rendered first in the container, so its output need not fill that range; range-boundary.test.ts covers this.

Relation to other tail fixes

Validation

  • pnpm format:check: not run repo-wide; pnpm format:files:check on every changed file passes.
  • pnpm typecheck: not run repo-wide; pnpm typecheck:files on every changed file, including the .tsrx fixture, passes.
  • pnpm test: not run on this head, per the current local-load rule; CI covers it. Before the rebase onto fix(hydration): rebuild component calls in an adopted element with other server content #1563/fix(hydration): discard server ranges a shorter @if/@switch arm leaves unclaimed #1564, the full octane project passed apart from a stale-install issue, and only this PR's new tests triggered the new discard.
  • targeted tests: packages/octane/tests/hydration/adopted-component-tail.test.ts covers 5 shapes in dev compile, prod compile, and prod compile with the prod runtime (vi.stubEnv('NODE_ENV', 'production')). Each case asserts final DOM, adopted-node identity, removal, exactly one recoverable error and one located dev warning, and an update afterwards. Controls assert that matching server output reports nothing and parses no template in production. 15 cases fail on main, and disabling each owner's hook turns its cases red. 45/45 pass on this head (octane), and the file also passed in octane-prod before the rebase. packages/octane/tests/compiler/template-root-count.test.ts checks that the compiler's root count matches the parsed roots for every fixture template.
  • pnpm sync: clean.

Performance

  • Bundle (node benchmarks/bundle-size/run-minimal.mjs vs main): hydrate-root +1,437 raw / +413 gzip, deferred-hydration +1,491 / +473, client-only scenarios +0 to +116 raw. These numbers predate a later trim of about 100 raw bytes.
  • News suite (node benchmarks/bench.mjs news --ratios): ratios.json has no news guards, so octane/svelte hydrate ratios were compared by hand. Four interleaved base/candidate rounds on prebuilt dists gave a median paired octane-tsrx hydrate delta of +0.02 ms, inside noise; the load average was 40–55. The news page has only three hydration markers, so it barely reaches the new path.
  • Frame-heavy check: both runtimes in one Chromium page, interleaved, hydrating 4,000 items (about 12,000 claimed ranges) per run. The paired median ratio was 1.00–1.04, inside the run-to-run noise band.

Risk / follow-ups

  • The settle discards only server content inside the adopted range, after the client's content. It stops at anything mismatch recovery built and leaves anything less certain in place. When no template recorded the end, it falls back to the base patch's check that the last slot left the cursor right past its own range.
  • @for items and @try bodies (different-source deployments only) remain.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Changes core hydration DOM reconciliation and compiler template() metadata; incorrect root counts or claim boundaries could remove valid nodes or miss mismatches, though tests cover multiple shapes and compile/runtime modes.

Overview
Hydration now removes leftover server DOM when a component adopts a range but the client renders less than the server put there (dynamic component identity, lite holes, shorter returns, list-item ranges). A claim tracks where the client’s roots end (beginClaim / claimRoots / settleClaim); anything still inside the frame before the range end is removed, reported once via onRecoverableError (error #51), and surfaced in dev with a located structural warning. Adopted nodes keep their identity.

The compiler passes each multi-root template’s actual root count as template()’s third argument (replacing a constant 1) so production hydration can bound adopted fragment roots without parsing the template HTML. Inspect output and types expose ns / frag for tooling.

Component, lite, and child slot paths route adopted-frame first renders through renderClaimed; passthrough range owners are unchanged. New hydration and compiler tests cover five mismatch shapes and root-count correctness across fixtures.

Reviewed by Cursor Bugbot for commit 4cef4bb. Bugbot is set up for automated code reviews on this repo. Configure here.

A component's server range can hold more than the client renders into it
from the same source: a component whose identity differs on the client, a
hookless component in a renderable hole, or a component that returns less.
The client adopted the matching prefix and kept the rest on screen without
a report.

The first render into an adopted range now opens a claim. The template that
adopts the range's first node records where its roots end (claimRoots),
using the root count the compiler now passes to template(), so production
hydration stays template-free. After the render, the server content left
before the range's end is removed and reported once (code 51, plus a
located development warning). Claims cover componentSlotImpl's adopted
frame, componentSlotLite's adopted frame, and childSlot's adopted or lent
ranges whose value is a component. A passthrough root's owner is excluded,
since it adopts whatever range the server rendered first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 2, 2026
@github-actions
github-actions Bot marked this pull request as draft October 2, 2026 15:24
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🔴 bundle-size: 11 value(s) increased
  • 🔴 bundle-reachability: 32 value(s) increased

Compares 416882fc3 with 9db1d0b7e on the same runner. Timing verdicts require a change beyond the combined margin of error (at least 5%) and one 0.1ms timer tick, with the fastest sample also moving beyond that margin. Workflow run

js-framework: 1k/10k row operations (ms, lower is better)

All 20 timed operations (20 within noise)
target operation base head Δ noise
octane-tsrx run 4.16 3.65 −12.3% ±31% ⚪ within noise
octane-tsrx replace 8.76 9.56 +9.1% ±18% ⚪ within noise
octane-tsrx add 4.02 3.99 −0.7% ±36% ⚪ within noise
octane-tsrx update 0.64 0.51 −20.3% ±33% ⚪ within noise
octane-tsrx select 0.21 0.24 +14.3% ±64% ⚪ within noise
octane-tsrx swap 0.44 0.51 +15.9% ±25% ⚪ within noise
octane-tsrx remove 0.52 0.51 −1.9% ±49% ⚪ within noise
octane-tsrx runlots 33.55 35.47 +5.7% ±26% ⚪ within noise
octane-tsrx select_lots 0.27 0.31 +14.8% ±41% ⚪ within noise
octane-tsrx clear 48.32 48.49 +0.4% ±8% ⚪ within noise
octane-jsx run 4.62 3.82 −17.3% ±37% ⚪ within noise
octane-jsx replace 10.99 9.63 −12.4% ±47% ⚪ within noise
octane-jsx add 4.29 3.72 −13.3% ±32% ⚪ within noise
octane-jsx update 0.72 0.77 +6.9% ±25% ⚪ within noise
octane-jsx select 0.84 0.61 −27.4% ±79% ⚪ within noise
octane-jsx swap 1.26 0.87 −31.0% ±66% ⚪ within noise
octane-jsx remove 1.09 0.73 −33.0% ±65% ⚪ within noise
octane-jsx runlots 33.39 32.25 −3.4% ±20% ⚪ within noise
octane-jsx select_lots 3.31 3.02 −8.8% ±15% ⚪ within noise
octane-jsx clear 51.17 49.59 −3.1% ±14% ⚪ within noise

Application bundle size (bytes)

target metric base head Δ
octane-tsrx todo_js_brotli 40,833 40,830 −3 (−0.0%) 🟢
octane-tsrx todo_app_brotli 2,123 2,120 −3 (−0.1%) 🟢
octane-tsrx weather_js_raw 176,859 176,889 +30 (+0.0%) 🔴
octane-tsrx weather_js_gzip 57,827 57,839 +12 (+0.0%) 🔴
octane-tsrx weather_js_brotli 50,265 50,251 −14 (−0.0%) 🟢
octane-tsrx weather_app_gzip 9,044 9,046 +2 (+0.0%) 🔴
octane-tsrx weather_app_brotli 7,562 7,575 +13 (+0.2%) 🔴
octane-tsrx weather_fw_raw 150,124 150,154 +30 (+0.0%) 🔴
octane-tsrx weather_fw_gzip 48,783 48,793 +10 (+0.0%) 🔴
octane-tsrx weather_fw_brotli 42,703 42,676 −27 (−0.1%) 🟢
octane-jsx js_raw 225,416 225,516 +100 (+0.0%) 🔴
octane-jsx js_gzip 71,903 71,969 +66 (+0.1%) 🔴
octane-jsx js_brotli 62,287 62,233 −54 (−0.1%) 🟢
octane-jsx app_gzip 2,942 2,944 +2 (+0.1%) 🔴
octane-jsx fw_raw 216,949 217,049 +100 (+0.0%) 🔴
octane-jsx fw_gzip 68,961 69,025 +64 (+0.1%) 🔴
octane-jsx fw_brotli 59,784 59,730 −54 (−0.1%) 🟢

28 other measured values are unchanged.

Public-import reachability bundles (bytes)

target metric base head Δ
root-static raw 201,739 201,835 +96 (+0.0%) 🔴
root-static gzip 64,085 64,135 +50 (+0.1%) 🔴
root-static brotli 55,513 55,528 +15 (+0.0%) 🔴
context raw 110,113 110,155 +42 (+0.0%) 🔴
context gzip 36,436 36,441 +5 (+0.0%) 🔴
context brotli 32,240 32,286 +46 (+0.1%) 🔴
hydrate-root raw 161,738 163,077 +1,339 (+0.8%) 🔴
hydrate-root gzip 51,798 52,190 +392 (+0.8%) 🔴
hydrate-root brotli 45,096 45,453 +357 (+0.8%) 🔴
deferred-hydration raw 224,294 225,687 +1,393 (+0.6%) 🔴
deferred-hydration gzip 71,346 71,729 +383 (+0.5%) 🔴
deferred-hydration brotli 61,550 61,861 +311 (+0.5%) 🔴
suspense-transition raw 166,620 166,664 +44 (+0.0%) 🔴
suspense-transition gzip 53,542 53,544 +2 (+0.0%) 🔴
suspense-transition brotli 46,767 46,714 −53 (−0.1%) 🟢
binding-base-ui-vite raw 211,933 212,030 +97 (+0.0%) 🔴
binding-base-ui-vite gzip 67,666 67,738 +72 (+0.1%) 🔴
binding-base-ui-vite brotli 58,668 58,704 +36 (+0.1%) 🔴
binding-base-ui-esbuild raw 209,907 209,999 +92 (+0.0%) 🔴
binding-base-ui-esbuild gzip 67,617 67,688 +71 (+0.1%) 🔴
binding-base-ui-esbuild brotli 58,605 58,752 +147 (+0.3%) 🔴
binding-radix-vite raw 213,862 213,960 +98 (+0.0%) 🔴
binding-radix-vite gzip 68,224 68,273 +49 (+0.1%) 🔴
binding-radix-vite brotli 59,103 59,261 +158 (+0.3%) 🔴
binding-radix-esbuild raw 211,795 211,888 +93 (+0.0%) 🔴
binding-radix-esbuild gzip 68,183 68,284 +101 (+0.1%) 🔴
binding-radix-esbuild brotli 59,089 59,226 +137 (+0.2%) 🔴
binding-apollo-client-vite raw 356,985 357,094 +109 (+0.0%) 🔴
binding-apollo-client-vite gzip 111,787 111,844 +57 (+0.1%) 🔴
binding-apollo-client-vite brotli 97,552 97,602 +50 (+0.1%) 🔴
binding-apollo-client-esbuild raw 359,963 360,072 +109 (+0.0%) 🔴
binding-apollo-client-esbuild gzip 114,003 114,034 +31 (+0.0%) 🔴
binding-apollo-client-esbuild brotli 99,309 99,310 +1 (+0.0%) 🔴

72 other measured values are unchanged.

Resolve runtime.ts conflicts with #1567: keep both the in-place adoption
(renderInPlace, the inPlace check in adopt()) and the component-range
claims (beginClaim/endClaim/renderClaimed/settleClaim, the claimRoots
check in adopt()).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm
trueadm marked this pull request as ready for review October 2, 2026 15:42
@trueadm
trueadm merged commit fbf6e40 into main Oct 2, 2026
49 checks passed
@trueadm
trueadm deleted the fix/adopted-component-tail branch October 2, 2026 15:58
trueadm pushed a commit that referenced this pull request Oct 2, 2026
Resolve runtime.ts against #1561, #1567 and #1576: keep main's adopted-range
claims (renderClaimed) and in-place lite rendering (renderInPlace) beside the
unframed-claim changes.

#1567 renders a lite call that found no range against the cursor, so a
hookless component that renders nothing leaves the server element to the next
call. Drop this branch's routing of such calls through the missing-range
path, which contradicted that. Fix the over-sweep where it happens instead:
when such a call rebuilds its fragment, placeRebuilt now discards only the
server content in its place, up to the next marker, and drainFrag inserts the
fragment there, ahead of later siblings' ranges. The development diagnostic
names the call site. renderUnframed shares the same bounded sweep
(discardInPlace).

The fixture's FragChild gets a hook so it is a full component call in both
compiles, which is the path that case covers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LqA51dmsggKyu7uH3zc9N2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant