Scroll restoration scrolls the window to the top on the first render after SSR hydration
维护者通常 1 天内回复
评估
- 难度
- 3/5
- 预计耗时
- 半天
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- playwright, react, typescript, vite
- 领域
- frontend
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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
npm install && npx playwright install chromium webkit && npx playwright test- The test holds the client scripts, scrolls the server-rendered page to 2000px, releases the scripts and waits for the router's first
onRendered. window.scrollYis0. 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-router1.170.46,@tanstack/router-core1.171.39,@tanstack/react-start1.169.5 (mainat 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.
- 主要语言
- TypeScript
- 星标
- 15.3k
- 派生
- 1.9k
- 平均合并
- 1 天 5 小时
- 30 天内合并 PR
- 46
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TanStack/router 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
Masked navigation drops the first character of the hash each time (__tempLocation hash missing '#')可能已有人在做 @hassanabrar202 于 8 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
router-generator: lazy-load Prettier (costs ~300–450ms per build even when formatting is off)可能已有人在做 @Sheraff 于 27 天前认领。 未关闭package: router-generator performance
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
router-generator: sibling `physical()` subtrees in a virtual route config generate duplicate identifiers可能已有人在做 @Sheraff 于 27 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
docs(router): clarify static title fallback in SPA mode可能已有人在做 @Rohan-Dedhia 于 17 天前认领。 未关闭
难度 1/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
相似的 Issue
-
[BUG]: Shared links drop the configured basePath on GitHub Pages可能已有人在做 @anshulchikhale30-p 今天认领。 未关闭bug
难度 1/5 1 小时以内 新手友好度 88/100
StabilityNexus/IdentityTokens-EVM-Frontend#182 · 1 条评论 ·
维护者通常 1 天内回复
-
Add: GTrekker未关闭check:passed streams:add
难度 1/5 1 小时以内 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
Koenkk/zigbee-herdsman-converters#13900 ·
维护者通常 1 天内回复
-
area: ui Bug Payload OSS
难度 1/5 1 小时以内 新手友好度 88/100
payloadcms/payload#19839 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
jeanibarz/knowledge-base-mcp-server#1291 ·
维护者通常 1 天内回复