Skip to content

fix(hydration): discard and report a server arm under a client arm that renders nothing - #1580

Merged
trueadm merged 3 commits into
mainfrom
fix/empty-arm-hydration-report
Oct 2, 2026
Merged

trueadm merged 3 commits into
mainfrom
fix/empty-arm-hydration-report

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

When the server rendered one arm of an @if/@switch and the client's arm renders nothing (@else { <></> }), hydration left the server's arm in the DOM and reported nothing. An @if with no client arm at all (body === null) already discards that content and reports it as "an empty branch". This makes the two consistent.

#1564's discardArmTail skipped an arm that claimed nothing, because the cursor is still on the arm's first node and cannot tell adoption from no adoption. That ambiguity only exists for elements and text, which a template adopts without moving the cursor. Every range an arm claims moves the cursor past it. So when the cursor is still on the arm's first node and that node opens a server range, the arm rendered nothing there, and the whole range is the server's other arm. It is now discarded, with one structural onRecoverableError and a development diagnostic at the directive: "the client expected an empty branch". A dormant <Hydrate> boundary whose captures changed still discards silently, as before. Element and text first nodes stay as they were.

Stacked on #1579, which fixes the router binding that relied on this silence. Its scroll restoration script arm hid exactly this mismatch on every website route. Until that lands, this branch carries its commit, and the diff narrows once it merges.

Validation

  • pnpm format:check: not run repo-wide; pnpm format:files:check on the changed files passes
  • pnpm typecheck: not run repo-wide; pnpm typecheck:files on the changed files passes
  • pnpm sync: no generated changes
  • pnpm test: not run in full locally
  • targeted tests:
    • packages/octane/tests/hydration/branch-arm-tail.test.ts, dev and production compile. fix(hydration): discard server ranges a shorter @if/@switch arm leaves unclaimed #1564's "does not report a client arm that renders nothing" case becomes: discards and reports once for an empty @else and for an @if without an else (the same report text). It also checks a matching empty arm adopts without a report, and a pending sibling's root replay reports once. 60/60 pass in octane + octane-prod; on main's runtime, 8 fail.
    • octane and octane-prod Vitest projects in full: 1337/1338 files and 37048/37049 tests pass. The one failure was a 5s timeout in hmr.test.ts (octane-prod) on a heavily loaded machine; that file passes alone (74/74 across both projects).
    • website/tests/ssr-hydration.e2e.test.ts --maxWorkers=1: 69/69 pass (dev SSR and production preview, no hydration-mismatch warnings) with this change and fix(tanstack-router): hydrate the scroll restoration script arm the server rendered #1579.
  • Bundle (node benchmarks/bundle-size/run-minimal.mjs, baseline → candidate, bytes): hydrate-root raw 161538→161531, gzip 51736→51735, brotli 44991→45010; deferred-hydration raw 223967→223960, gzip 71234→71230, brotli 61465→61440; suspense-transition unchanged (166494 / 53485 / 46736). The change removes a comparison; the new message is development-only.

Risk / follow-ups

  • Server content that this silently kept is now removed and reported. The only source @else { <></> } arms are the two in the router binding fixed by fix(tanstack-router): hydrate the scroll restoration script arm the server rendered #1579. An arm that renders nothing at runtime through a nested @if or an early return already took the null-body path.
  • A client arm that renders nothing over a server arm starting with an element or text node is still not reported. The cursor cannot distinguish that case from template adoption without per-adoption bookkeeping on the hydration hot path.

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 SSR hydration control-flow handling and will surface recoverable errors where mismatches were previously silent; router scroll-restoration scripts now depend on adopt-then-remove during hydrate.

Overview
Hydration now treats a client @if/@switch arm that renders nothing (for example @else { <></> } or no else) the same as a missing client arm: discardArmTail no longer bails out when the cursor is still on the branch start, so stale server content is removed and a single structural onRecoverableError is raised, with a dev message of "an empty branch" instead of silently leaving the server's other arm in the DOM. Element/text-first server arms are unchanged.

@octanejs/tanstack-router aligns with that behavior: ScrollRestorationScript keeps the server-side arm when router-core's browser build returns no script body, and ScriptOnce adopts the server <script> during hydration (with suppressHydrationWarning), drops it after useHydrated, and renders nothing on client-only mounts like upstream React Router.

Coverage adds branch-arm-tail cases and a scroll-restoration-hydration conformance suite.

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

trueadm and others added 2 commits October 2, 2026 16:34
…erver rendered

router-core's browser build of getScrollRestorationScriptForRouter always
returns null, so the client hydrated ScrollRestorationScript's empty arm over
the server's ScriptOnce arm and the server <script> stayed in the document
with no report. The client now takes the arm the server took, ScriptOnce
adopts the server script without comparing its content and drops it after
hydration, and a client-only ScriptOnce renders nothing as upstream's does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…at renders nothing

An @if with no client arm discards the server's other arm and reports it as an
empty branch, but a client arm with an empty body (@else { <></> }) claimed
nothing and left the server arm on screen with no report: #1564's tail discard
skipped an arm whose cursor never left its first node. That cursor position is
only ambiguous at elements and text, which a template adopts without moving
the cursor; every range an arm claims moves it. When the arm's first node opens
a server range, discard it and report an empty branch, as the null-body path
does.

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 15:46
@trueadm
trueadm merged commit c016c2d into main Oct 2, 2026
22 of 41 checks passed
@trueadm
trueadm deleted the fix/empty-arm-hydration-report branch October 2, 2026 15:53
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