Fix scroll restore when using browser back after hash links - #8572
Closed
moltresIn wants to merge 1 commit into
Closed
Fix scroll restore when using browser back after hash links#8572moltresIn wants to merge 1 commit into
moltresIn wants to merge 1 commit into
Conversation
With Next.js scrollRestoration set to manual, going back from an in-page fragment left the URL correct but kept the hash scroll position. Save scrollY on the history entry before hash navigation and restore it on popstate. Fixes reactjs#787. Co-authored-by: Cursor <cursoragent@cursor.com>
Size changesDetails📦 Next.js Bundle Analysis for react-devThis analysis was generated by the Next.js Bundle Analysis action. 🤖
|
| Page | Size (compressed) |
|---|---|
global |
115.46 KB (🟡 +319 B) |
Details
The global bundle is the javascript bundle that loads alongside every page. It is in its own category because its impact is much higher - an increase to its size means that every page on your website loads slower, and a decrease means every page loads faster.
Any third party scripts you have added directly to your app using the <script> tag are not accounted for in this analysis
If you want further insight into what is behind the changes, give @next/bundle-analyzer a try!
Author
|
Closing — opened by mistake; not ready for review. |
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
#fragment link, browser Back updates the URL but does not restore scroll whenhistory.scrollRestorationismanual(Chrome/Firefox).scrollYon the current history entry viareplaceStatebefore hash navigation, then restores it onpopstatewhen the hash is cleared.scrollRestoration = 'auto'path unchanged so Next.js page navigations stay on manual restoration elsewhere.Test plan
yarn check-all/learn/describing-the-ui: scroll mid-page → click heading anchor → Back → scroll returns to prior position (verified with Playwright againstyarn dev)Made with Cursor