Next.js: Using getServerSideProps breaks scroll restoration

Created on 5 May 2020  路  5Comments  路  Source: vercel/next.js

Bug report

Describe the bug

When using getServerSideProps, a server roundtrip is made every time the user navigates to a page, to load the page's data json (generated by getServerSideProps).

This _also_ happens when the page is navigated to _via the browser's back and forward buttons_. The latency of the request, even when cached, breaks browser-native scroll restoration.

As there is no way to intervene with the request on the client side (using getServerSideProps, the request is made Next-internally), it can't be blocked. Using getInitialProps, it was possible to cache data on the client side and return it immediately. This is not possible anymore with getServerSideProps.

To Reproduce

The issue is sufficiently documented here.

Expected behavior

Pages using getServerSideProps do not break scroll restoration when navigated to via the browser's back/forward buttons.

bug 3 needs investigation

All 5 comments

This is a big problem, basically breaks every website running nextjs. Is there no general solution for this?

Here's a band-aid fix for that problem:
https://gist.github.com/claus/992a5596d6532ac91b24abe24e10ae81

Reverted this while we figure out a forward nav bug. Will be relanded soon!

Wouldn't the "proper" fix be to cache getServerSideProps responses and use them for back/forward navigation? This would not only fix the scroll restoration issue, but also make back/forward navigation work without additional requests, which matches native browser (and also getStaticProps) behavior.

It would be nice to make it configurable:

  1. Use cached data
  2. Hold for backend refresh
Was this page helpful?
0 / 5 - 0 ratings

Related issues

wagerfield picture wagerfield  路  3Comments

flybayer picture flybayer  路  3Comments

sospedra picture sospedra  路  3Comments

YarivGilad picture YarivGilad  路  3Comments

formula349 picture formula349  路  3Comments