-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(gatsby): scroll restoration issue in browser API #27384
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
Have you tried it with the original reproduction? When I run it, I get this runtime error (in gatsby develop):
Uncaught TypeError: Invalid attempt to spread non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.
at _nonIterableSpread (nonIterableSpread.js:2)
at _toConsumableArray (toConsumableArray.js:6)
at gatsby-browser.js:20
It seems that the storage was stored coordination ( If you modify line 20 in window.setTimeout(
() => window.scrollTo(...([0, savedPosition] || [0, 0])),
transitionDelay
) It'll work. |
Yeah, but this way it is a breaking change. We should try to preserve the old behavior (which is also documented here: https://www.gatsbyjs.com/docs/browser-apis/#shouldUpdateScroll) |
You are right. I change a little to keep the old behavior. |
Sorry, missed your last comment. I Will review it later this week. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried it with the reproduction and it works 🎉 Thanks for the PR and sorry for the delay with the review!
Also adding some context on why it has to be done this way. We save the state here and only save the Y
coordinate:
this._stateStorage.save(this.props.location, key, window.scrollY) |
Also, the initial key is taken from the location.key itself.
* fix scroll restoration issue * keep the behavior same * fix the lint error (cherry picked from commit 4a7a324)
* fix scroll restoration issue * keep the behavior same * fix the lint error (cherry picked from commit 4a7a324)
* fix scroll restoration issue * keep the behavior same * fix the lint error (cherry picked from commit 4a7a324) Co-authored-by: vrabe <[email protected]>
Published in |
Edit: I misread the above comments, and looks like my comment below doesn't make sense anymore if the original behaviour was preserved.
() => window.scrollTo(0, savedPosition || 0) |
Description
getSavedScrollPosition()
inshouldUpdateScroll
doesn't passkey
to SessionStorage, so the storage can't find the stored position.I pass
args.key
askey
so that SessionStorage can generate the correct key.Related Issues
relates to #27349