Skip to content

fix(hydration): adopt a component hole's own server node in a template adopted in place - #1602

Merged
trueadm merged 1 commit into
mainfrom
claude/determined-vaughan-82f876
Oct 2, 2026
Merged

trueadm merged 1 commit into
mainfrom
claude/determined-vaughan-82f876

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A component call can find no server range of its own because the server rendered another @if/@switch arm there. Hydration then adopts the server markup in place. If that component's template contains another component call (a hole), the hole now renders in place of the server node its template walk found. Before, it used the server node at the hydration cursor.

Repro, broken on main:

let label = 'p';
function Em() @{ <em>{'e' as string}</em> }
function IEm() @{ <><i>{label as string}</i><Em /></> }
export function HoleRaw(props: { on: boolean }) @{
	<div id="r">
		@if (props.on) { <><IEm /><Em /></> } @else { <><i>{label as string}</i><em>{'e' as string}</em><Em /></> }
	</div>
}

Server-render with on: false, then hydrateRoot with on: true.

  • Before: <em>e</em><em>e</em>. The server <i> was replaced and a mismatch was reported.
  • Now: <i>p</i><em>e</em><em>e</em>. Every server element is adopted and nothing is reported.

Cause

When a call with an anchor found no range, it took its in-place root from the cursor (or host.firstChild). For a hole in an adopted template, the cursor is still where the adoption left it:

  • Fragment template: the virtual-fragment adoption doesn't move the cursor, so it stays on the first root.
  • Element template: the cursor stays on the element itself, so the lookup falls back to its first child.

When the server framed the inner call, the anchor was its <!--[--> and the framed path hid the bug. When the server rendered that markup inline, the inner call rendered against a node the outer component had already adopted, reported a mismatch, and replaced it. Calls with hooks (componentSlotImpl's single-root path) had the same defect.

Changes

  • componentSlotLite, anchored call with no range: the call renders in place of its anchor and parks the cursor there. Two anchors keep the old behaviour:
    • the block's own end marker: the body is template-less, so the cursor is already the call's server node;
    • null: a walk that ran off the end of its host, which keeps main's mismatch report.
  • componentSlotImpl, single-root branch: the same rule.

Compiled anchors are only ever a walked <!> placeholder, __block.endMarker, or undefined (anchorNodeFor), so the end-marker comparison separates template holes from template-less bodies. Framed calls never reach either branch, and adopt() is unchanged.

Validation

  • pnpm format:check (not run; scoped pnpm format:files:check on the changed files passes)
  • pnpm typecheck (not run; scoped pnpm typecheck:files on the changed files passes)
  • pnpm test (not run, because of local machine load)
  • targeted tests:
    • New: tests/hydration/in-place-fragment-hole.test.ts (12 cases, dev and prod compiles).
      • Inline server markup in a fragment template, an element template, and a hook component's hole: these 6 fail on main.
      • The framed control and the "walk runs off the end of the element" cases pass on main; they pin that behaviour.
    • Mutations: each of six fails at least one test. They drop the cursor move, treat every anchor as walked, or walk null anchors, in both slots, and remove the full-slot change.
    • On the rebased head: 15 owning and neighbouring hydration files pass, 553 tests, in the octane project. Among them: in-place component sibling, lite host mismatch, hole-first fragment arm, component range siblings, unframed component siblings, adopted component tail, branch arm tails, hydration, prod-mode-hydrate.
  • Bundle size: every bundle that includes the component slots grows by 49 B raw (+10 to +37 B gzip, up to +85 B brotli in the PR benchmark report), client-only bundles included, because the new branches sit in componentSlotLite and componentSlotImpl. Locally, benchmarks/bundle-size/run-minimal.mjs gave deferred-hydration +49 B raw / +16 B gzip / −31 B brotli. I ran no timing benchmark: the runtime changes are on cold no-range branches, plus one hydration !== null check on a client-side single-root mount.

Risk / follow-ups

  • Unchanged from main: a server arm with fewer roots, whose walk lands exactly on the parent block's end marker. That looks the same as a template-less body, so the adopted first root is still replaced.
  • Not changed: a non-single-root component with hooks as the hole (the renderUnframed path). That path stops discarding at the anchor, so pointing it at the walked node would leave the stale server node in place. It needs its own design.
  • Fragment cursor follow-up: a static root after the hole (<i/><Em/><b/>) is fully clean only together with the in-place fragment cursor fix on fix/unframed-fragment-root (c8cff03afb, unmerged). That fix moves the cursor past every root of an in-place fragment. I checked that the two compose by cherry-picking it locally. On this PR alone the server <i> is kept, but the next sibling still rebuilds over <b>.

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 core hydration slot logic on no-range and single-root paths; behavior is narrow but incorrect fixes could cause silent DOM corruption or extra mismatches.

Overview
Fixes hydration mismatches when the server rendered a different @if/@switch arm and the client adopts that markup in place, but a nested component call in the adopted template must hydrate against its own server node—not the cursor still sitting on the template’s first root.

In componentSlotLite, an anchored call with no server range now treats a walked anchor (a template hole) as in-place at that anchor and moves the hydration cursor there; null or the block end marker still use the previous in-place root behavior. The componentSlotImpl single-root hydration path applies the same cursor rule.

Adds a patch changeset, fixture in-place-fragment-hole.tsrx, and in-place-fragment-hole.test.ts (dev/prod compiles) covering inline vs framed server markup, hook components, element templates, and the case where the server element ends before the hole (still rebuilds and reports).

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

…e adopted in place

A component call that finds no server range of its own, because the server
rendered another @if/@switch arm there, adopts the server markup in place.
A component call that is a hole in that template found its in-place root
from the hydration cursor, which still sits on the template's first root
(a fragment's virtual adoption, or an element template's root), rather
than from its walked anchor. When the server rendered the inner call's
markup inline, the inner call rendered against that first root, reported a
mismatch and replaced a node the outer component had already adopted.

- componentSlotLite: an anchored call without a range renders in place of
  its anchor unless the anchor is its block's end marker (a templateless
  body, whose cursor is right) or null (a walk that ran off its host).
- componentSlotImpl: the single-root branch applies the same rule.

Framed calls and adopt() take no extra work.

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

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Benchmark report

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

Compares 5ef4f9a49 with 419d4e5c4 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.46 4.33 −2.9% ±32% ⚪ within noise
octane-tsrx replace 10.62 10.88 +2.4% ±62% ⚪ within noise
octane-tsrx add 4.35 4.55 +4.6% ±21% ⚪ within noise
octane-tsrx update 0.53 0.51 −3.8% ±69% ⚪ within noise
octane-tsrx select 0.25 0.22 −12.0% ±54% ⚪ within noise
octane-tsrx swap 0.49 0.56 +14.3% ±62% ⚪ within noise
octane-tsrx remove 0.54 0.48 −11.1% ±87% ⚪ within noise
octane-tsrx runlots 42.08 41.72 −0.9% ±34% ⚪ within noise
octane-tsrx select_lots 0.31 0.28 −9.7% ±52% ⚪ within noise
octane-tsrx clear 46.61 57.09 +22.5% ±37% ⚪ within noise
octane-jsx run 4.29 4.85 +13.1% ±24% ⚪ within noise
octane-jsx replace 8.55 10.54 +23.3% ±21% ⚪ within noise
octane-jsx add 3.81 4.22 +10.8% ±23% ⚪ within noise
octane-jsx update 0.62 0.83 +33.9% ±43% ⚪ within noise
octane-jsx select 0.46 0.56 +21.7% ±51% ⚪ within noise
octane-jsx swap 0.72 1.13 +56.9% ±50% ⚪ within noise
octane-jsx remove 0.74 0.88 +18.9% ±43% ⚪ within noise
octane-jsx runlots 37.17 37.32 +0.4% ±19% ⚪ within noise
octane-jsx select_lots 3.07 2.84 −7.5% ±33% ⚪ within noise
octane-jsx clear 50.82 49.33 −2.9% ±15% ⚪ within noise

Application bundle size (bytes)

target metric base head Δ
octane-tsrx weather_js_raw 177,593 177,642 +49 (+0.0%) 🔴
octane-tsrx weather_js_gzip 58,066 58,085 +19 (+0.0%) 🔴
octane-tsrx weather_js_brotli 50,546 50,488 −58 (−0.1%) 🟢
octane-tsrx weather_app_gzip 9,044 9,045 +1 (+0.0%) 🔴
octane-tsrx weather_app_brotli 7,590 7,582 −8 (−0.1%) 🟢
octane-tsrx weather_fw_raw 150,858 150,907 +49 (+0.0%) 🔴
octane-tsrx weather_fw_gzip 49,022 49,040 +18 (+0.0%) 🔴
octane-tsrx weather_fw_brotli 42,956 42,906 −50 (−0.1%) 🟢
octane-jsx js_raw 226,031 226,080 +49 (+0.0%) 🔴
octane-jsx js_gzip 72,141 72,151 +10 (+0.0%) 🔴
octane-jsx js_brotli 62,398 62,480 +82 (+0.1%) 🔴
octane-jsx app_gzip 2,944 2,943 −1 (−0.0%) 🟢
octane-jsx app_brotli 2,502 2,499 −3 (−0.1%) 🟢
octane-jsx fw_raw 217,564 217,613 +49 (+0.0%) 🔴
octane-jsx fw_gzip 69,197 69,208 +11 (+0.0%) 🔴
octane-jsx fw_brotli 59,896 59,981 +85 (+0.1%) 🔴

29 other measured values are unchanged.

Public-import reachability bundles (bytes)

target metric base head Δ
root-static raw 202,351 202,400 +49 (+0.0%) 🔴
root-static gzip 64,323 64,336 +13 (+0.0%) 🔴
root-static brotli 55,751 55,749 −2 (−0.0%) 🟢
context raw 110,385 110,434 +49 (+0.0%) 🔴
context gzip 36,548 36,585 +37 (+0.1%) 🔴
context brotli 32,347 32,353 +6 (+0.0%) 🔴
deferred-hydration raw 228,777 228,826 +49 (+0.0%) 🔴
deferred-hydration gzip 72,592 72,607 +15 (+0.0%) 🔴
suspense-transition raw 166,916 166,965 +49 (+0.0%) 🔴
suspense-transition gzip 53,658 53,674 +16 (+0.0%) 🔴
suspense-transition brotli 46,871 46,837 −34 (−0.1%) 🟢
binding-base-ui-vite raw 212,541 212,590 +49 (+0.0%) 🔴
binding-base-ui-vite gzip 67,904 67,917 +13 (+0.0%) 🔴
binding-base-ui-vite brotli 58,859 58,850 −9 (−0.0%) 🟢
binding-base-ui-esbuild raw 210,506 210,555 +49 (+0.0%) 🔴
binding-base-ui-esbuild gzip 67,896 67,909 +13 (+0.0%) 🔴
binding-base-ui-esbuild brotli 58,891 58,870 −21 (−0.0%) 🟢
binding-radix-vite raw 214,470 214,519 +49 (+0.0%) 🔴
binding-radix-vite gzip 68,444 68,457 +13 (+0.0%) 🔴
binding-radix-vite brotli 59,365 59,321 −44 (−0.1%) 🟢
binding-radix-esbuild raw 212,394 212,443 +49 (+0.0%) 🔴
binding-radix-esbuild gzip 68,496 68,510 +14 (+0.0%) 🔴
binding-radix-esbuild brotli 59,446 59,394 −52 (−0.1%) 🟢
binding-apollo-client-vite raw 357,594 357,643 +49 (+0.0%) 🔴
binding-apollo-client-vite gzip 112,014 112,028 +14 (+0.0%) 🔴
binding-apollo-client-vite brotli 97,791 97,765 −26 (−0.0%) 🟢
binding-apollo-client-esbuild raw 360,546 360,595 +49 (+0.0%) 🔴
binding-apollo-client-esbuild gzip 114,237 114,247 +10 (+0.0%) 🔴
binding-apollo-client-esbuild brotli 99,510 99,484 −26 (−0.0%) 🟢

76 other measured values are unchanged.

@trueadm
trueadm merged commit 3ec43da into main Oct 2, 2026
35 of 61 checks passed
@trueadm
trueadm deleted the claude/determined-vaughan-82f876 branch October 2, 2026 20:06
trueadm added a commit that referenced this pull request Oct 2, 2026
… the changeset

Main's #1602 and #1600 now handle the single-root and lite calls at a
component hole in an adopted arm, so this branch's runtime change is reduced
to the unframed (keyed or dynamic) path. A keyed call after the adopted static
root pins that its stale node comes from its anchor rather than the lagging
cursor, which would delete the adopted root.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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