Repository navigation
fix(tanstack-router): hydrate the scroll restoration script arm the server rendered - #1579
Merged
Merged
Conversation
…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]>
trueadm
marked this pull request as ready for review
October 2, 2026 15:35
Contributor
Benchmark report🟢 No size increases and no timing regressions outside noise. Compares js-framework: 1k/10k row operations (ms, lower is better)All 20 timed operations (20 within noise)
Application bundle size (bytes)No changes across 45 measured values. Public-import reachability bundles (bytes)No changes across 105 measured values. |
Merged
3 of 6 tasks
…n-script-hydration
Collaborator
Author
|
The js-framework 🟡 rows are runner noise, not this change. This PR only touches |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
With
createRouter({ scrollRestoration: true }), every hydrated route kept the server's scroll restoration<script>in the document, and nothing was reported.ScrollRestorationScriptchose its arm fromgetScrollRestorationScriptForRouter(router). router-core's server build of that helper returns the inline restore script; its browser build always returnsnull. So the server rendered theScriptOncearm, and the client hydrated the empty@elsearm 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-routeravoids this because its server script ends withdocument.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 (ascrollRestorationfunction evaluated againstrouter.latestLocation) when the browser build returnsnull.ScriptOnce.tsrx: the hydration render adopts the server<script>withsuppressHydrationWarning. The client never runs that script and may not have its source, as here. The post-hydration render then drops it.useHydrated()replaces theuseState(true)+useEffectpair, so a client-only mount renders nothing, matching upstream'sScriptOnce, which returnsnulloff 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:checkon the changed files passespnpm typecheck: not run repo-wide;pnpm typecheck:fileson the changed.tsrx/.tsfiles passespnpm sync: no generated changespnpm test: not run in full locallypackages/tanstack-router/tests/conformance/scroll-restoration-hydration.test.ts(server render, thenhydrateRootthroughRouterClient). It mocks the subpath to router-core's realbrowserbuild, 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, fortrueand for accepting/decliningscrollRestorationfunctions. Also no script is inserted on a client navigation, and a client-onlyScriptOncerenders no script.hmr: falseclient,dev: falseserver fixture). Before the fix, the two script cases fail in both modes with a stale<script>. With only the arm change and the oldScriptOnce, 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-rsbuildVitest projects: 32 files, 166 tests pass.router-client.test.tsalso passes under the production compile.Risk / follow-ups
scrollRestorationfunction 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
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith 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 whencreateRouter({ scrollRestoration: true }). router-core’s server build emits the inline restore script while the browser build returnsnull, so the client used to hydrate the empty@elsearm and leave the server script in the DOM.ScrollRestorationScriptnow picks the same branch as the server by falling back to router-core’s predicate (scrollRestorationfunction vsrouter.latestLocation) when the browser helper returnsnull, and passes an empty string intoScriptOncewhen needed.ScriptOnceswitches touseHydrated()(matching upstream’s client-only “render nothing” behavior), addssuppressHydrationWarningso hydration can adopt the server<script>without content comparison, then drops it after hydration commits.Adds conformance coverage (server render →
hydrateRootviaRouterClient, including declining/acceptingscrollRestorationfunctions, client navigation, and client-onlyScriptOnce) 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.