question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 out

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:open
  • Created 6 years ago
  • Reactions:78
  • Comments:66 (6 by maintainers)

github_iconTop GitHub Comments

50reactions
arunodacommented, Mar 28, 2018

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:

  • Cache the data locally and render the page from that cache. You can even update the data in behind the scene. In this case data will be there as the user hit the back button.
  • Control scrolling manually via 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.

41reactions
hardwitcommented, Jul 1, 2019

My solution for restoring scroll position when the browser back button clicked:

_app.js

componentDidMount() {
    const cachedPageHeight = []
    const html = document.querySelector('html')

    Router.events.on('routeChangeStart', () => {
      cachedPageHeight.push(document.documentElement.offsetHeight)
    })

    Router.events.on('routeChangeComplete', () => {
      html.style.height = 'initial'
    })

    Router.beforePopState(() => {
      html.style.height = `${cachedPageHeight.pop()}px`

      return true
    })
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found