Scroll restoration scrolls the window to the top on the first render after SSR hydration

Đang mở
#8,933 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@pjborowiecki đang làm issue này rồi.

Từ ngày 14/11/2026.

  • #8934 của @pjborowiecki — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
Nửa ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
playwright, react, typescript, vite
Lĩnh vực
frontend

Hướng nghiên cứu

Start from packages/router-core/src/router.ts:1148-1155, where RouterCore._scroll is initialised to { n: true }, then read packages/router-core/src/scroll-restoration.ts:253 and :353-361 to see the onRendered listener acting on that flag and calling scrollTo({ top: 0 }). The hydration path to check is packages/router-core/src/load-client.ts after router.ssr = … (line 2224), with packages/react-router/src/Transitioner.tsx:76-88 showing where onRendered fires on the first render. Done means the reproducer's Playwright test (hold scripts, scroll to 2000, release, wait for onRendered) reports a preserved scrollY in Chromium and WebKit, with later navigations still resetting scroll. Note an open PR (#8934) already proposes this change.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Which project does this relate to?

Router

Describe the bug

With SSR and scrollRestoration: true, the router's first render after hydration scrolls the window to the top. The page is readable and scrollable before the client bundle hydrates it, so a visitor who scrolls during that time is thrown back to the top once the app hydrates.

Complete minimal reproducer

https://github.com/pjborowiecki/tanstack-router-hydration-scroll-repro (StackBlitz: https://stackblitz.com/github/pjborowiecki/tanstack-router-hydration-scroll-repro)

Steps to Reproduce the Bug
  1. npm install && npx playwright install chromium webkit && npx playwright test
  2. The test holds the client scripts, scrolls the server-rendered page to 2000px, releases the scripts and waits for the router's first onRendered.
  3. window.scrollY is 0. Fails on every run in Chromium and WebKit.

By hand: npm run build && npm run preview, throttle the network in DevTools so the scripts load slowly, scroll down as soon as the page appears, and the page jumps to the top when it hydrates.

Expected behavior

The first render after hydration leaves the scroll position alone. Hydration is not a navigation: the browser and the inline restoration script set the initial position, and the visitor owns it after that.

Screenshots or Videos

No response

Platform
  • Router / Start version: @tanstack/react-router 1.170.46, @tanstack/router-core 1.171.39, @tanstack/react-start 1.169.5 (main at 0b6d4f3 has the same code)
  • OS: macOS 15
  • Browser: Chromium and WebKit (Playwright 1.65.1)
  • Bundler: Vite 8.3.8
Additional context

RouterCore._scroll starts as { n: true } ("reset scroll on the next render", packages/router-core/src/router.ts:1148-1155). On hydration, Transitioner emits onRendered for the resolved location (packages/react-router/src/Transitioner.tsx:76-88). The onRendered listener in setupScrollRestoration reads scroll.n as true (packages/router-core/src/scroll-restoration.ts:253) and, with no cached entry on a first visit, calls scrollTo({ top: 0, left: 0 }) (:353-361).

Setting the flag in hydrate() (packages/router-core/src/load-client.ts, after router.ssr = … on line 2224) fixes it:

router._scroll.n = false

onRendered sets scroll.n back to true after that render, so later navigations are unchanged. With this line applied to @tanstack/router-core 1.171.39 in the reproducer, the test passes 10 of 10 runs (5 per browser).

I can open a PR with the change, an e2e test and a changeset.

Disclosure: this report was investigated and written with AI assistance. The reproducer was run against the published packages, with and without the change, before posting.

Ngôn ngữ chính
TypeScript
Star
15.3k
Fork
1.9k
Merge trung bình
1 ngày 5 giờ
Pull request đã merge (30 ngày)
46

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của TanStack/router

Tất cả issue của TanStack/router

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.