Skip to content

fix(hydration): rebuild component calls in an adopted element with other server content - #1563

Merged
trueadm merged 4 commits into
mainfrom
fix/adopted-host-component-range
Oct 2, 2026
Merged

trueadm merged 4 commits into
mainfrom
fix/adopted-host-component-range

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

When hydration adopts an element whose children are all component calls, but the server rendered other content inside it, the first call now reports the server node once, the element's server children are discarded, and the components are built inside the adopted element.

Repro (server renders the first arm, client the second):

function Leaf() @{ <i>{label() as string}</i> }
export function ElementHost(props: { server?: boolean }) @{
	<div>
		@if (props.server) {
			<section><b class="server">{'server'}</b><s>{'x'}</s></section>
		} @else {
			<section><Leaf /></section>
		}
	</div>
}

Before, in dev and prod: the <section> was adopted, the diagnostic said "the client expected <i> but the server rendered <section>", and the DOM ended as <section><b class="server">server</b><s>x</s><i>ok</i></section>. The lite variant (<LiteLeaf text={LABEL} />) removed the adopted <section> entirely. After: one diagnostic, "the client expected a component range but the server rendered <b>", and <section><i>ok</i></section> with the same adopted <section>.

Why

The compiler omits a component call's anchor only when every child of the host is a component, and the server frames each such call in <!--[-->…<!--]-->. On a miss, the cursor branch of componentSlotImpl / componentSlotLite looked at host.firstChild, but it never moved the cursor there:

  • A single-root call took the markerless single-root path, so its body's clone() compared against the cursor still parked on the host (or an ancestor, for a nested host). The "cursor outside the target parent" guard in adopt() then built a fresh root without discarding anything.
  • A lite call's clone ran under the enclosing block, whose parent is the host's parent, so recovery discarded the host itself.

Changes

  • componentSlotImpl: an anchorless call (anchor === undefined) that finds no server range no longer takes the markerless single-root path during hydration. It takes the missing-range path that non-single-root components already use: render between fresh markers without adopting, report once, and discard the stale server nodes. Anchored and null-anchor calls are unchanged.
  • componentSlotLite: the same case goes through a new cold mountUnframedLite, which re-enters componentSlotLite through renderUnframed.
  • HydrationCapability.renderUnframed takes a render callback plus the fresh markers. It skips the report and the discard when its claim is client-built (an earlier rebuild already reported and discarded the server's content). This stops a later anchorless sibling from destroying an earlier sibling's rebuilt output, which also happened on main for fragment components.
  • An element the server left empty now reports "the server rendered nothing" instead of naming the element.

Happy-path cost is two comparisons on a slot's first mount. componentSlotImpl bytecode grows 38 bytes with unchanged registers and frame size. Bundle: +480 B minified / +126 B gzip for the full client entry.

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 runtime.ts, the test, and the .tsrx fixture passes)
  • pnpm test (not run in full; the octane and octane-prod projects were run, see below)
  • targeted tests: new packages/octane/tests/hydration/adopted-host-component-clone.test.ts (dev + prod compile) covers single-root, lite, nested-host, sibling (single-root / lite / fragment), and empty-host rebuilds, asserting final DOM, adopted-host identity, and exactly one structural diagnostic, plus matching-server adoption controls and post-hydration arm switches. All 16 rebuild cases fail on main. Removing the client-built check fails the sibling cases, and disabling range adoption fails the adoption controls.
  • octane + octane-prod Vitest projects on the current main (95421bb): 36,836 of 36,837 tests passed. The one failure was a 15 s timeout in behavior-root.test.ts under a load average of about 25–45; that file passed 414/414 in both projects on re-run. Earlier runs under heavier load timed out in up to 8 files, and each passed when re-run alone.

Timing benchmarks (hydration-range-compaction, hydration-render-phase-queue) were not trustworthy for the final candidate: other workloads held the machine's load average at 30–70. An earlier quieter alternating run of an intermediate candidate showed every delta inside about ±7% noise.

Risk / follow-ups

  • The diagnostic for this case changes from a template-level one to the component-range one. A rebuilt component sits between <!--comp--> markers, like other missing-range rebuilds.
  • An anchored call whose compiled anchor walk ran off the end of its host (legacy server output without the empty-text stand-in) still takes the old path.
  • Separate follow-up: componentSlotImpl's inline fresh-subtree closure makes V8 allocate a 14-slot context on every call.

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
Touches first-render hydration paths in the core runtime (componentSlotImpl, lite slots, renderUnframed), changing DOM teardown and mismatch messaging for structural SSR/client divergence.

Overview
Fixes hydration when an adopted host element should only contain component calls, but the server left different markup inside (e.g. a mismatched @if arm). The client now treats the first anchorless call as a missing component range: emit one structural mismatch, drop the server children, and render components inside the adopted element instead of appending after stale nodes or removing the host (lite).

componentSlotImpl no longer uses the markerless single-root path during hydration when anchor is undefined and no <!--[--> range is found; it follows the existing unframed rebuild path. componentSlotLite adds mountUnframedLite and routes that case through HydrationCapability.renderUnframed, which now accepts a render callback and fresh markers and skips a second discard/report when the claimed node is already client-built—so later sibling anchorless calls do not tear down an earlier sibling’s rebuild. Empty server hosts report “nothing” in dev diagnostics.

Adds a changeset (patch) and hydration tests (single-root, lite, nested, siblings, empty host; dev/prod compile).

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

…her server content

When every child of an element is a component call, the compiler omits the
calls' anchors and the server frames each call's output. If hydration adopted
the element but the server rendered other content inside it, the first call
found no range at the element's first child. A single-root call then cloned
against the cursor still parked on the element (or an ancestor): the diagnostic
named the element, the stale server children stayed, and the rebuilt root was
appended after them. A lite call's recovery removed the adopted element itself.

An anchorless call that finds no range now takes the missing-range path that
non-single-root components use: render between fresh markers without adopting,
report the server node once, and discard the element's server content. Lite
calls get the same path through mountUnframedLite. renderUnframed skips the
report and discard when its claim is client-built, so a later sibling call no
longer destroys an earlier sibling's rebuilt content.

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
@trueadm
trueadm marked this pull request as ready for review October 2, 2026 13:21
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

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

Compares 15c7b97a2 with 151b694b3 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.02 3.98 −1.0% ±38% ⚪ within noise
octane-tsrx replace 8.67 9.77 +12.7% ±27% ⚪ within noise
octane-tsrx add 3.62 3.91 +8.0% ±17% ⚪ within noise
octane-tsrx update 0.50 0.47 −6.0% ±41% ⚪ within noise
octane-tsrx select 0.17 0.23 +35.3% ±75% ⚪ within noise
octane-tsrx swap 0.40 0.51 +27.5% ±37% ⚪ within noise
octane-tsrx remove 0.38 0.44 +15.8% ±64% ⚪ within noise
octane-tsrx runlots 33.97 33.17 −2.4% ±28% ⚪ within noise
octane-tsrx select_lots 0.24 0.26 +8.3% ±44% ⚪ within noise
octane-tsrx clear 48.37 48.37 −0.0% ±10% ⚪ within noise
octane-jsx run 3.94 3.91 −0.8% ±10% ⚪ within noise
octane-jsx replace 8.11 9.82 +21.1% ±29% ⚪ within noise
octane-jsx add 3.34 3.48 +4.2% ±9% ⚪ within noise
octane-jsx update 0.62 0.70 +12.9% ±39% ⚪ within noise
octane-jsx select 0.60 0.48 −20.0% ±84% ⚪ within noise
octane-jsx swap 0.84 0.80 −4.8% ±59% ⚪ within noise
octane-jsx remove 0.69 0.77 +11.6% ±43% ⚪ within noise
octane-jsx runlots 30.48 32.07 +5.2% ±10% ⚪ within noise
octane-jsx select_lots 2.95 2.85 −3.4% ±48% ⚪ within noise
octane-jsx clear 50.57 52.59 +4.0% ±15% ⚪ within noise

Application bundle size (bytes)

target metric base head Δ
octane-tsrx weather_js_raw 176,620 176,674 +54 (+0.0%) 🔴
octane-tsrx weather_js_gzip 57,721 57,732 +11 (+0.0%) 🔴
octane-tsrx weather_js_brotli 50,194 50,126 −68 (−0.1%) 🟢
octane-tsrx weather_app_brotli 7,603 7,563 −40 (−0.5%) 🟢
octane-tsrx weather_fw_raw 149,885 149,939 +54 (+0.0%) 🔴
octane-tsrx weather_fw_gzip 48,678 48,689 +11 (+0.0%) 🔴
octane-tsrx weather_fw_brotli 42,591 42,563 −28 (−0.1%) 🟢
octane-jsx js_raw 225,234 225,289 +55 (+0.0%) 🔴
octane-jsx js_gzip 71,821 71,838 +17 (+0.0%) 🔴
octane-jsx js_brotli 62,155 62,192 +37 (+0.1%) 🔴
octane-jsx app_brotli 2,503 2,502 −1 (−0.0%) 🟢
octane-jsx fw_raw 216,767 216,822 +55 (+0.0%) 🔴
octane-jsx fw_gzip 68,878 68,895 +17 (+0.0%) 🔴
octane-jsx fw_brotli 59,652 59,690 +38 (+0.1%) 🔴

31 other measured values are unchanged.

Public-import reachability bundles (bytes)

target metric base head Δ
root-static raw 201,557 201,612 +55 (+0.0%) 🔴
root-static gzip 63,970 63,985 +15 (+0.0%) 🔴
root-static brotli 55,457 55,478 +21 (+0.0%) 🔴
context raw 109,931 109,986 +55 (+0.1%) 🔴
context gzip 36,363 36,379 +16 (+0.0%) 🔴
context brotli 32,155 32,167 +12 (+0.0%) 🔴
hydrate-root raw 161,234 161,241 +7 (+0.0%) 🔴
hydrate-root gzip 51,677 51,671 −6 (−0.0%) 🟢
hydrate-root brotli 44,954 44,991 +37 (+0.1%) 🔴
deferred-hydration raw 223,608 223,669 +61 (+0.0%) 🔴
deferred-hydration gzip 71,134 71,157 +23 (+0.0%) 🔴
deferred-hydration brotli 61,405 61,419 +14 (+0.0%) 🔴
suspense-transition raw 166,440 166,494 +54 (+0.0%) 🔴
suspense-transition gzip 53,466 53,485 +19 (+0.0%) 🔴
suspense-transition brotli 46,650 46,736 +86 (+0.2%) 🔴
binding-base-ui-vite raw 211,751 211,806 +55 (+0.0%) 🔴
binding-base-ui-vite gzip 67,549 67,565 +16 (+0.0%) 🔴
binding-base-ui-vite brotli 58,549 58,565 +16 (+0.0%) 🔴
binding-base-ui-esbuild raw 209,724 209,779 +55 (+0.0%) 🔴
binding-base-ui-esbuild gzip 67,544 67,566 +22 (+0.0%) 🔴
binding-base-ui-esbuild brotli 58,632 58,648 +16 (+0.0%) 🔴
binding-radix-vite raw 213,680 213,735 +55 (+0.0%) 🔴
binding-radix-vite gzip 68,104 68,121 +17 (+0.0%) 🔴
binding-radix-vite brotli 59,167 59,135 −32 (−0.1%) 🟢
binding-radix-esbuild raw 211,612 211,667 +55 (+0.0%) 🔴
binding-radix-esbuild gzip 68,102 68,122 +20 (+0.0%) 🔴
binding-radix-esbuild brotli 59,070 59,172 +102 (+0.2%) 🔴
binding-mantine-hooks-esbuild brotli 29,059 29,062 +3 (+0.0%) 🔴
binding-apollo-client-vite raw 356,801 356,858 +57 (+0.0%) 🔴
binding-apollo-client-vite gzip 111,682 111,703 +21 (+0.0%) 🔴
binding-apollo-client-vite brotli 97,448 97,582 +134 (+0.1%) 🔴
binding-apollo-client-esbuild raw 359,779 359,836 +57 (+0.0%) 🔴
binding-apollo-client-esbuild gzip 113,914 113,935 +21 (+0.0%) 🔴
binding-apollo-client-esbuild brotli 99,259 99,262 +3 (+0.0%) 🔴
binding-usehooks-ts-esbuild gzip 32,912 32,911 −1 (−0.0%) 🟢
binding-usehooks-ts-esbuild brotli 29,268 29,258 −10 (−0.0%) 🟢

69 other measured values are unchanged.

CI job durations

  • 🟡 9 job(s) slower than every recent main run: test shard (Node 24, 1/4), test shard (Node 24, 2/4), test shard (Node 24, 3/4), test shard (Node 24, 4/4), lint checks, website integration suite, validate publishable packages, example apps (3/3), example apps (1/3)
  • 🟢 3 job(s) faster than every recent main run: example apps (2/3), typecheck checks, React parity checks

Compares the CI run for 37e16062a (run) with the last 1 successful main push runs. A job is only slower or faster outside the whole main range, by at least 10% and 15s. Job time excludes runner setup.

job main median main range this PR Δ
wall clock (first job start to last job end) 8m 14s 8m 14s–8m 14s 13m 25s +5m 11s (+63%) ⚪ partial run
total job time 89m 19s 89m 19s–89m 19s 147m 27s +58m 08s (+65%) ⚪ partial run
test shard (Node 24, 1/4) 5m 28s 5m 28s–5m 28s 12m 47s +7m 19s (+134%) 🟡 slower
test shard (Node 24, 2/4) 5m 32s 5m 32s–5m 32s 12m 42s +7m 10s (+130%) 🟡 slower
test shard (Node 24, 3/4) 5m 27s 5m 27s–5m 27s 11m 20s +5m 53s (+108%) 🟡 slower
test shard (Node 24, 4/4) 6m 15s 6m 15s–6m 15s 11m 07s +4m 52s (+78%) 🟡 slower
lint checks 3m 35s 3m 35s–3m 35s 8m 02s +4m 27s (+124%) 🟡 slower
website integration suite 3m 59s 3m 59s–3m 59s 6m 13s +2m 14s (+56%) 🟡 slower
validate publishable packages 2m 22s 2m 22s–2m 22s 4m 03s +1m 41s (+71%) 🟡 slower
example apps (3/3) 2m 57s 2m 57s–2m 57s 3m 39s +42s (+24%) 🟡 slower
example apps (1/3) 2m 55s 2m 55s–2m 55s 3m 24s +29s (+17%) 🟡 slower
example apps (2/3) 4m 33s 4m 33s–4m 33s 3m 38s −55s (−20%) 🟢 faster
typecheck checks 4m 33s 4m 33s–4m 33s 2m 02s −2m 31s (−55%) 🟢 faster
React parity checks 2m 57s 2m 57s–2m 57s 12s −2m 45s (−93%) 🟢 faster

Not compared (skipped, failed, or not run on this pull request): Three compatibility (minimum r156), heavy integration (astro), heavy integration (browser), heavy integration (runtime-loaders), heavy integration (eval-corpus), test shard (Node 22.22.2, 4/4), test shard (Node 22.22.2, 3/4), Three compatibility (current), Lynx compatibility (current), Lynx compatibility (minimum), test shard (Node 22.22.2, 2/4), heavy integration (package-builds), test shard (Node 22.22.2, 1/4), test (22).

All 26 timed jobs
job main median main range this PR Δ
heavy integration (browser - chromium) – – 12m 24s – 🆕 new
React parity shard (2/4) – – 10m 33s – 🆕 new
heavy integration (package builds, eval corpus, runtime loaders, Astro) – – 5m 38s – 🆕 new
React parity shard (1/4) – – 11m 34s – 🆕 new
React parity shard (4/4) – – 11m 29s – 🆕 new
React parity shard (3/4) – – 11m 18s – 🆕 new
Three compatibility – – 2m 59s – 🆕 new
Lynx compatibility – – 1m 52s – 🆕 new
test shard (Node 24, 1/4) 5m 28s 5m 28s–5m 28s 12m 47s +7m 19s (+134%) 🟡 slower
test shard (Node 24, 2/4) 5m 32s 5m 32s–5m 32s 12m 42s +7m 10s (+130%) 🟡 slower
test shard (Node 24, 3/4) 5m 27s 5m 27s–5m 27s 11m 20s +5m 53s (+108%) 🟡 slower
test shard (Node 24, 4/4) 6m 15s 6m 15s–6m 15s 11m 07s +4m 52s (+78%) 🟡 slower
lint checks 3m 35s 3m 35s–3m 35s 8m 02s +4m 27s (+124%) 🟡 slower
website integration suite 3m 59s 3m 59s–3m 59s 6m 13s +2m 14s (+56%) 🟡 slower
validate publishable packages 2m 22s 2m 22s–2m 22s 4m 03s +1m 41s (+71%) 🟡 slower
example apps (3/3) 2m 57s 2m 57s–2m 57s 3m 39s +42s (+24%) 🟡 slower
example apps (1/3) 2m 55s 2m 55s–2m 55s 3m 24s +29s (+17%) 🟡 slower
classify changeset release 9s 9s–9s 14s +5s (+56%) ⚪ within noise
examples 1s 1s–1s 3s +2s (+200%) ⚪ within noise
lint 2s 2s–2s 4s +2s (+100%) ⚪ within noise
test (24) 1s 1s–1s 3s +2s (+200%) ⚪ within noise
CI provenance 2s 2s–2s 4s +2s (+100%) ⚪ within noise
typecheck 2s 2s–2s 3s +1s (+50%) ⚪ within noise
example apps (2/3) 4m 33s 4m 33s–4m 33s 3m 38s −55s (−20%) 🟢 faster
typecheck checks 4m 33s 4m 33s–4m 33s 2m 02s −2m 31s (−55%) 🟢 faster
React parity checks 2m 57s 2m 57s–2m 57s 12s −2m 45s (−93%) 🟢 faster

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