How to save scroll position while navigating between pages using Link from gatsby? #26654
-
SummaryAs I remember, before, the scroll position was always saved while navigating between pages using |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 2 replies
-
Has there been any further explanation for this? I'm also using gatsby to build a website, which requires that the scroll position is retained. |
Beta Was this translation helpful? Give feedback.
-
I'm actually trying to do the opposite - make a traditional website experience where scroll is not retained. I was under the impression that this was fixed? |
Beta Was this translation helpful? Give feedback.
-
@benjmo please open a different issue if you need help with that. @ezvr and @SilencerWeb gatsby should be retaining the scroll position by default with the same behavior that a browser does when javascript is not involved. One thing you can do to check is look at localStorage when you're navigating. You should see some keys added for each history element and it's scroll position. Do you not see that? What version of gatsby are you running? Maybe more specifically what version of |
Beta Was this translation helpful? Give feedback.
-
Nothing is being added to my localStorage. I do have @@scroll keys in sessionStorage though, and they seem to be working. i.e. being updated. You can observe the problematic website on https://kisik.si/tehnologija/. Try clicking on the menu items on the left, while being scrolled. Okay, so every time I open the page, a new key seems to be created. It's as though it's not recognizing the website has been already opened before. I must be creating pages programmatically wrong? Here is my page generation code snippet:
I followed this tutorial. However I didn't do the first part, where it says to create slugs for pages. Because even when skipping that part of the tutorial, the pages seemed to be created and worked just fine.
|
Beta Was this translation helpful? Give feedback.
-
Are you guys scrolling on the window? When setting body to something like You can use the |
Beta Was this translation helpful? Give feedback.
-
Adding the following in // gatsby-browser.js
exports.shouldUpdateScroll = () => false |
Beta Was this translation helpful? Give feedback.
-
I'm using Gatsby 4 and @piducancore 's answer worked for me, I just had to tweak it a bit:
|
Beta Was this translation helpful? Give feedback.
-
Gatsby v5 this works, but there are caveats if you want to do your own scrolling via a component i.e. |
Beta Was this translation helpful? Give feedback.
Adding the following in
gatsby-browser.js
is working for me in gatsby 3