Skip to content

fix(tanstack-router): hydrate the scroll restoration script arm the server rendered - #1579

Merged
trueadm merged 2 commits into
mainfrom
fix/scroll-restoration-script-hydration
Oct 2, 2026
Merged

trueadm merged 2 commits into
mainfrom
fix/scroll-restoration-script-hydration

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

With createRouter({ scrollRestoration: true }), every hydrated route kept the server's scroll restoration <script> in the document, and nothing was reported.

ScrollRestorationScript chose its arm from getScrollRestorationScriptForRouter(router). router-core's server build of that helper returns the inline restore script; its browser build always returns null. So the server rendered the ScriptOnce arm, and the client hydrated the empty @else arm over it. The empty arm claims nothing, so the server <script> and its markers stayed in the client DOM until an arm swap that never comes. @tanstack/react-router avoids this because its server script ends with document.currentScript.remove() and React has no structural markers to match.

  • scroll-restoration.tsrx: the client takes the arm the server took. It falls back to the same predicate router-core's server helper uses (a scrollRestoration function evaluated against router.latestLocation) when the browser build returns null.
  • ScriptOnce.tsrx: the hydration render adopts the server <script> with suppressHydrationWarning. The client never runs that script and may not have its source, as here. The post-hydration render then drops it. useHydrated() replaces the useState(true) + useEffect pair, so a client-only mount renders nothing, matching upstream's ScriptOnce, which returns null off the server. Before, a client-only mount inserted the script for one commit.

This was found while landing #1564. That PR's tail discard deliberately skips an arm that claimed nothing; without that guard, every website route reported a mismatch at this site.

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 .tsrx/.ts files passes
  • pnpm sync: no generated changes
  • pnpm test: not run in full locally
  • targeted tests:
    • New packages/tanstack-router/tests/conformance/scroll-restoration-hydration.test.ts (server render, then hydrateRoot through RouterClient). It mocks the subpath to router-core's real browser build, because the jsdom project resolves the server build, which is why existing tests missed this. Cases: no stale <script>, no recoverable error, no console warning, server layout adopted, for true and for accepting/declining scrollRestoration functions. Also no script is inserted on a client navigation, and a client-only ScriptOnce renders no script.
    • It passes in the dev compile and in a production compile (hmr: false client, dev: false server fixture). Before the fix, the two script cases fail in both modes with a stale <script>. With only the arm change and the old ScriptOnce, four cases fail: a dev hydration mismatch on the script content, and a script inserted on client-only mount.
    • tanstack-router, tanstack-router-ssr, tanstack-router-ssr-query, tanstack-start, tanstack-start-rsbuild Vitest projects: 32 files, 166 tests pass. router-client.test.ts also passes under the production compile.

Risk / follow-ups

  • The server output is unchanged and still has no self-removal, so the script stays in the document until hydration commits rather than being removed during parsing. That is not observable to the router: the script runs once either way.
  • On the server, a scrollRestoration function that declines the location is called twice per render: once by router-core's helper and once by the fallback. The client calls it once. It is a predicate over the location, and router-core itself calls it again on every navigation.

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 SSR hydration and DOM adoption for inline scripts; behavior is narrow and server HTML is unchanged, but mismatches here can leave scripts in the document or cause hydration warnings.

Overview
Fixes stale scroll-restoration <script> tags after SSR hydration when createRouter({ scrollRestoration: true }). router-core’s server build emits the inline restore script while the browser build returns null, so the client used to hydrate the empty @else arm and leave the server script in the DOM.

ScrollRestorationScript now picks the same branch as the server by falling back to router-core’s predicate (scrollRestoration function vs router.latestLocation) when the browser helper returns null, and passes an empty string into ScriptOnce when needed.

ScriptOnce switches to useHydrated() (matching upstream’s client-only “render nothing” behavior), adds suppressHydrationWarning so hydration can adopt the server <script> without content comparison, then drops it after hydration commits.

Adds conformance coverage (server render → hydrateRoot via RouterClient, including declining/accepting scrollRestoration functions, client navigation, and client-only ScriptOnce) plus a patch changeset entry.

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

…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]>
@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:35
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No size increases and no timing regressions outside noise.

Compares 416882fc3 with 22b814a22 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.35 4.18 −3.9% ±32% ⚪ within noise
octane-tsrx replace 9.14 9.86 +7.9% ±19% ⚪ within noise
octane-tsrx add 3.93 4.19 +6.6% ±22% ⚪ within noise
octane-tsrx update 0.53 0.55 +3.8% ±49% ⚪ within noise
octane-tsrx select 0.19 0.23 +21.1% ±74% ⚪ within noise
octane-tsrx swap 0.40 0.43 +7.5% ±49% ⚪ within noise
octane-tsrx remove 0.44 0.44 +0.0% ±39% ⚪ within noise
octane-tsrx runlots 35.92 38.47 +7.1% ±23% ⚪ within noise
octane-tsrx select_lots 0.28 0.30 +7.1% ±50% ⚪ within noise
octane-tsrx clear 50.53 49.85 −1.3% ±10% ⚪ within noise
octane-jsx run 4.64 4.81 +3.7% ±40% ⚪ within noise
octane-jsx replace 9.44 8.87 −6.0% ±26% ⚪ within noise
octane-jsx add 4.24 3.87 −8.7% ±36% ⚪ within noise
octane-jsx update 0.75 0.59 −21.3% ±39% ⚪ within noise
octane-jsx select 0.51 0.50 −2.0% ±67% ⚪ within noise
octane-jsx swap 0.86 0.71 −17.4% ±31% ⚪ within noise
octane-jsx remove 0.84 0.68 −19.0% ±39% ⚪ within noise
octane-jsx runlots 32.59 33.92 +4.1% ±13% ⚪ within noise
octane-jsx select_lots 3.09 3.53 +14.2% ±28% ⚪ within noise
octane-jsx clear 54.98 51.59 −6.2% ±12% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

@trueadm

trueadm commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author

The js-framework 🟡 rows are runner noise, not this change. This PR only touches packages/tanstack-router (source, tests, audit manifest) and a changeset; the js-framework fixtures never import the router, and the same report shows no change across all 150 measured bundle values. The flagged operations are sub-millisecond (0.4–1.0 ms) with ±28–35% noise.

@trueadm
trueadm merged commit 7c6cd95 into main Oct 2, 2026
30 checks passed
@trueadm
trueadm deleted the fix/scroll-restoration-script-hydration branch October 2, 2026 16:00
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