Skip to content
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

Merged
merged 3 commits into from
Dec 1, 2020
Merged

fix(gatsby): scroll restoration issue in browser API #27384

merged 3 commits into from
Dec 1, 2020

Conversation

vrabe
Copy link
Contributor

@vrabe vrabe commented Oct 11, 2020

Description

getSavedScrollPosition() in shouldUpdateScroll doesn't pass key to SessionStorage, so the storage can't find the stored position.
I pass args.key as key so that SessionStorage can generate the correct key.

Related Issues

relates to #27349

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Oct 11, 2020
@vladar vladar added topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine) status: needs core review Currently awaiting review from Core team member and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Oct 12, 2020
Copy link
Contributor

@vladar vladar left a 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

@vrabe
Copy link
Contributor Author

vrabe commented Oct 12, 2020

It seems that the storage was stored coordination ([0, Y_value]) but now it only stores y values.

If you modify line 20 in gatsby-browser.js to:

window.setTimeout(
  () => window.scrollTo(...([0, savedPosition] || [0, 0])),
  transitionDelay
)

It'll work.

@vladar
Copy link
Contributor

vladar commented Oct 12, 2020

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)

@vrabe
Copy link
Contributor Author

vrabe commented Oct 12, 2020

You are right. I change a little to keep the old behavior.

@vrabe vrabe requested a review from vladar October 22, 2020 11:50
@vrabe vrabe requested a review from a team as a code owner November 2, 2020 18:26
@vladar vladar self-assigned this Nov 23, 2020
@vladar
Copy link
Contributor

vladar commented Nov 23, 2020

Sorry, missed your last comment. I Will review it later this week. Thanks!

Copy link
Contributor

@vladar vladar left a 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.

@vladar vladar added the bot: merge on green Gatsbot will merge these PRs automatically when all tests passes label Dec 1, 2020
@vladar vladar merged commit 4a7a324 into gatsbyjs:master Dec 1, 2020
pieh pushed a commit that referenced this pull request Dec 4, 2020
* fix scroll restoration issue

* keep the behavior same

* fix the lint error

(cherry picked from commit 4a7a324)
pieh pushed a commit that referenced this pull request Dec 4, 2020
* fix scroll restoration issue

* keep the behavior same

* fix the lint error

(cherry picked from commit 4a7a324)
pieh pushed a commit that referenced this pull request Dec 4, 2020
* fix scroll restoration issue

* keep the behavior same

* fix the lint error

(cherry picked from commit 4a7a324)

Co-authored-by: vrabe <[email protected]>
@pieh
Copy link
Contributor

pieh commented Dec 7, 2020

Published in [email protected]

@trevorblades
Copy link
Contributor

trevorblades commented Dec 17, 2020

It seems that the storage was stored coordination ([0, Y_value]) but now it only stores y values.

If you modify line 20 in gatsby-browser.js to:

window.setTimeout(
  () => window.scrollTo(...([0, savedPosition] || [0, 0])),
  transitionDelay
)

It'll work.

Edit: I misread the above comments, and looks like my comment below doesn't make sense anymore if the original behaviour was preserved.

The statement [0, savedPosition] || [0, 0] will always return the left side of the || operator, since an array is never falsey. I think what you meant to write was [0, savedPosition || 0]. However, since savedPosition is no longer an array, we can just pass the Y value directly to the second argument of window.scrollTo now:

() => window.scrollTo(0, savedPosition || 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: merge on green Gatsbot will merge these PRs automatically when all tests passes status: needs core review Currently awaiting review from Core team member topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants