next.js: Link component does not persist scroll position
When using Link for routing and then pressing the browser’s back button, the page will not remember the previous scroll position.
For example, go from page1 to page2 and then click the back button. page1 is rendered at the top of the viewport.
The browser is keeping track of the scroll position because if the page is refreshed it will refresh at the appropriate position. Is this an issue with the History API?
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 21
- Comments: 23 (15 by maintainers)
@migueloller I don’t think I quite understood the solution @rauchg suggested. The premise is to cache the data on client-side to expedite rendering speed? Did you ever figure out a solution?
Also, @arunoda, you suggested someone could make a PR. Do you have any pointers as to which files or folders I should look at? I would love to make that contribution.
Currently, this is how I’m persisting scroll position with a route listener instance:
This function is called when I bootstrap client. This only works sometimes because data may take too long to load and the browser will scroll to top by default.
I would like a way to persist the scroll position without having to optimizing my render function (I mean I could, but I want to know if there’s a functional way of solving this issue).
Sorry @karanmartian, I saw your message and forgot to reply. Today I found your message again.
I am not working on the project now, but I did this hack to solve it. This code is in my
_app.jsfile, but outside of my App class.@stacygohyunsi You can use
scrollprop on link component for your use case like this<Link href="/path" scroll />Ref: https://github.com/zeit/next.js/blob/master/lib/link.js#L55-L69Hi, I hope this helps someone.
My requirements:
backorforwardbutton clicked)Linkcomponent)My solution:
Oh, I didn’t know that router return a promise 😃 Thanks!
@coluccini Yes