Skip to content

Preserve scroll during initial hydration - #1533

Merged
manudeli merged 1 commit into
mainfrom
manudeli/preserve-scroll-on-hydration
Nov 22, 2026
Merged

manudeli merged 1 commit into
mainfrom
manudeli/preserve-scroll-on-hydration

Conversation

@manudeli

@manudeli manudeli commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

What changed

Scrolling during initial page load can send the visitor back to the top when hydration finishes. On the homepage preview in Chrome, scrolling to 3,860 px before hydration finished reset the position to 0 px without a URL or page height change.

Patch the pinned @tanstack/router-core to skip the default window reset on the first SSR-rendered location. Keep the current offset available for Back navigation and preserve stored window and element offsets, hash links, and normal navigation resets.

Related upstream fix: TanStack/router#8934. Remove the local patch once the site adopts a Router release with the fix.

Verification

  • Reproduced the reset against the unpatched Router. The same regression check passed with the patch for both ESM and CJS, covering hydration, forward navigation, Back, cached offsets, hash links, and an initial client-only load.
  • Frozen lockfile validation, type checking, lint, and desktop tests passed.
  • The broader suites ran with the Node TS loader after the TSX CLI hit a socket permission error. Five tests across the unit and chat suites were blocked by local socket or Chrome launch restrictions.

Summary by CodeRabbit

  • Bug Fixes
    • Preserved the current scroll position during initial hydration instead of unexpectedly scrolling the window to the top.
    • Kept nonzero window scroll positions available for scroll tracking; other navigation and scroll-to-top behavior remains unchanged.

@manudeli
manudeli requested a review from a team November 22, 2026 13:12
@changeset-bot

changeset-bot Bot commented Nov 22, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: a76e501

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The router-core patch identifies the first render with identical source and destination locations as initial hydration. It skips scrolling the window to the top when the window has not been restored. The workspace file registers the patch for the specified router-core version.

Changes: Hydration scroll restoration

Layer / File(s) Summary
Preserve scroll during initial hydration
patches/@tanstack__router-core@1.171.34.patch, pnpm-workspace.yaml
The source implementation and CJS and ESM bundles skip window scroll-to-top on initial hydration when the window has not been restored. When scroll tracking is enabled and either window coordinate is nonzero, the document is tracked. Other renders retain the existing window scroll behavior, and scroll-to-top elements are still processed. The workspace registers the patch for @tanstack/router-core@1.171.34.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: tannerlinsley, kevinvandy

Merge Risk: Minimal · up to a76e5

This adds a local patch that stops the first SSR hydration from resetting the window scroll to the top. Normal navigation behavior is unchanged. No concrete merge-blocking risk was found, and the patch should be removed once an upstream Router release includes the fix.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive Direct issue #8934 requires that hydrate() set router._scroll.n = false only in the !needsClientLoad branch, so the first render after hydration skips the scroll reset. The PR instead patches @tanstac… Confirm from the patch hunks in src/scroll-restoration.ts (CJS and ESM bundles) that the skip applies only when the hydrated match does not need a client load. Confirm that element scroll restoration still runs for a scroll container render…
Passed Passed checks: 3 passed

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Preserve scroll during initial hydration" matches the main change. The raw summary says the first onRendered event during hydration no longer scrolls the window to the top when the window h…
Out of Scope Changes check Passed The changed files in the summary are the router-core patch file and the pinned patch entry in pnpm-workspace.yaml. The patch edits the scroll restoration logic that issue #8934 covers. The PR descript…
Full details: Title check

Explanation

The title "Preserve scroll during initial hydration" matches the main change. The raw summary says the first onRendered event during hydration no longer scrolls the window to the top when the window has not already been restored. The linked issue #8934 asks for the same behavior: hydration keeps scroll position when it resolves the server-rendered location. The title is short, specific, and free of noise.

Full details: Out of Scope Changes check

Explanation

The changed files in the summary are the router-core patch file and the pinned patch entry in pnpm-workspace.yaml. The patch edits the scroll restoration logic that issue #8934 covers. The PR description states that the patch is temporary and points to the upstream fix. The pnpm-lock.yaml change is excluded from review under the ignored_files rule, so it is not assessed here. No unrelated changes appear in the summary.

Full details: Linked Issues check

Explanation

Direct issue #8934 requires that hydrate() set router._scroll.n = false only in the !needsClientLoad branch, so the first render after hydration skips the scroll reset. The PR instead patches @tanstack/router-core (patches/@tanstack__router-core@1.171.34.patch). Per the raw summary, the patch treats the first onRendered event as hydration when fromLocation and toLocation match. It skips the window scroll-to-top when the window is not already restored. It adds the document to tracked scroll targets when scroll tracking is enabled and a window coordinate is nonzero. The summary does not show whether the patch gates on needsClientLoad, so the ssr: false and 'data-only' behavior is unverified. The summary also does not show whether element scroll entries are restored when a scroll container renders after the inline restoration script. The issue names this as an untested regression. No test files appear in the summary. The issue's hydrate.test.ts and scroll-restoration-reload e2e expectations are therefore not shown as implemented. The PR description reports local checks, but those are not committed automated tests in the reviewed change.

Resolution

Confirm from the patch hunks in src/scroll-restoration.ts (CJS and ESM bundles) that the skip applies only when the hydrated match does not need a client load. Confirm that element scroll restoration still runs for a scroll container rendered after the inline script. Identify where the regression tests for the no-scroll-reset case and the reload restoration case are committed, or state that they are not part of this PR.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f6c12ae5-f92e-420d-84dd-00f1a7d1830c
Commits

Reviewing files that changed from the base of the PR and between 9cf50da and a76e501.

Files ignored due to path filters: 1

  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
Files selected for processing: 2

  • patches/@tanstack__router-core@1.171.34.patch
  • pnpm-workspace.yaml

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 0 remain after this review.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
tanstack-com a76e501 Commit Preview URL

Branch Preview URL
Nov 22 2026, 01:13 PM

@manudeli
manudeli merged commit 5668b04 into main Nov 22, 2026
8 checks passed
@manudeli
manudeli deleted the manudeli/preserve-scroll-on-hydration branch November 22, 2026 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants