Scroll restoration happens too early before the page gets rendered after hitting browser back button
See original GitHub issue- I have searched the issues of this repository and believe that this is not a duplicate.
After transiting from one page to another page via next <Link />
, if user clicks the back button and the previous page has getInitialProps
method that takes some time to finish, the scroll position of the previous will be restored before the previous pages gets rendered.
Demo
source code can be found here
After clicking back button, the “go back” text should still be visible (not scroll down to previous position) until the previous page gets rendered
Issue Analytics
- State:
- Created 6 years ago
- Reactions:78
- Comments:66 (6 by maintainers)
Top Results From Across the Web
Restore the scroll position when browser back button is clicked
When I've looked at the product and clicked the Google Chrome back button, the scroll position is back at the beginning of the...
Read more >Take User Back to Where They Scrolled to on previous page ...
If the content is loaded after page "load" event firing, then the back button would not take you back to the position you...
Read more >Implementing scroll restoration in ecommerce React apps
When we go back, the browser is going to reinstate the most recent URL that we were on prior to the navigation event,...
Read more >Browsers are pretty good at loading pages - Hacker News
Because it's faster. If you don't have to download all the content again, force the browser to re-render everything, then by design you...
Read more >Modern client-side routing: the Navigation API
The browser begins by capturing the scroll position for the current state, so it can be optionally restored later, then it calls your...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We’ve a similar issues reported here: https://spectrum.chat/thread/3d65b436-b085-4eef-ad84-0941c473e09d
Next.js doesn’t hijack scrolling. But this is a real issue. Here’s what’s happening. This is because of browser’s default scrolling behavior. See: https://github.com/zeit/next.js/issues/3303#issuecomment-354225323
When you go back, browser tries to go to the previous scroll position for better UX. But in the ^^ case, the data is not loaded yet. But the data will be there in a bit with the latency of
getInitialProps()
. When the data rendered, it’s not the correct position as before.So, in order fix this you can do a few things:
Router.push()
. (This will return a promise and it’ll get resolved when the page is rendered)We’ve no plan to manage scrolling inside Next.js yet. But you can do it in the userland and publish a new Link component like
LinkBetterScrolling
.My solution for restoring scroll position when the browser back button clicked:
_app.js