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(scroll): use location.key for scroll behaviours #12403

Merged
merged 7 commits into from
Mar 28, 2019

Conversation

hentielouw
Copy link
Contributor

Description

This is a simple fix to use location.key for saving scroll state, if it's available.

Based on the suggestions found in this discussion: taion/scroll-behavior#135 (comment)

Related Issues

Fixes #12390.

@KyleAMathews
Copy link
Contributor

What does locationKey look like when you browse around?

Also I'm assuming you verified the bug I posted doesn't happen any longer?

@KyleAMathews
Copy link
Contributor

And thanks for jumping on this!

@hentielouw
Copy link
Contributor Author

I've tested it locally and looks like it's working as expected. I haven't added any automated tests since the package doesn't seem to have any tests at the moment - happy to add tests if you think that's needed, but might need a hand!

To illustrate how locationKey changes, I've added a console.log for locationKey and set up a test site (Gatsby Default Starter with some Lorem Ipsum). I took the screenshot below after navigating using "Go to page 2" and "Go back to the homepage" links a few times (without using "back"). I then used browser "back" to go to previous pages.

On first load the locationKey value is initial. Scrolling up and down logs the same value multiple times. On each navigation locationKey changes to a new integer value. Pressing "back" uses previous key values.

Hope that makes sense! Gets a bit muddy talking about this scrolling/navigation behaviour.

screenshot 2019-03-08 at 17 25 41

@pieh
Copy link
Contributor

pieh commented Mar 28, 2019

I've added tests for scroll behavior simulating example scenario with some changes (from taion/scroll-behavior#135 (comment) )

visit /
PUSH to /long-page
Scroll to middle-of-the-page
PUSH to /
PUSH to /long-page
Scroll to bottom-of-the-page
PUSH to /
POP to /long-page
  assert path is /long page
  assert we are scrolled to bottom-of-the-page
  assert we are NOT scrolled to middle-of-the-page
POP to /
POP to /long-page
  assert path is /long page
  assert we are scrolled to middle-of-the-page
  assert we are NOT scrolled to bottom-of-the-page

Which looks like this in cypress logs - hopefully in works in CI correctly:
Screenshot 2019-03-28 at 02 59 29

@pieh pieh changed the title Use location.key for scroll behaviours, if available fix(scroll): use location.key for scroll behaviours Mar 28, 2019
Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @hentielouw!

@pieh pieh merged commit 853ceb9 into gatsbyjs:master Mar 28, 2019
@gatsbot
Copy link

gatsbot bot commented Mar 28, 2019

Holy buckets, @hentielouw — we just merged your PR to Gatsby! 💪💜

Gatsby is built by awesome people like you. Let us say “thanks” in two ways:

  1. We’d like to send you some Gatsby swag. As a token of our appreciation, you can go to the Gatsby Swag Store and log in with your GitHub account to get a coupon code good for one free piece of swag. (Currently we’ve got a couple t-shirts available, plus some socks that are really razzing our berries right now.)
  2. We just invited you to join the Gatsby organization on GitHub. This will add you to our team of maintainers. Accept the invite by visiting https://github.com/orgs/gatsbyjs/invitation. By joining the team, you’ll be able to label issues, review pull requests, and merge approved pull requests.

If there’s anything we can do to help, please don’t hesitate to reach out to us: tweet at @gatsbyjs and we’ll come a-runnin’.

Thanks again!

@hentielouw hentielouw deleted the fix/location-scroll branch March 28, 2019 16:07
@t2ca
Copy link
Contributor

t2ca commented Apr 1, 2019

Looking for some help, this update stopped the following code from working on my project.

exports.shouldUpdateScroll = () => {
  if (window.__navigatingToLink === true) {
    return [0, 0]
  }
  return true
}

exports.onRouteUpdate = () => {
  window.__navigatingToLink = false
}

I added this code to gatsby-browser.js to fix an issue I was having, but since this update, this code no longer works for me. Would love some help, thanks!

@KyleAMathews
Copy link
Contributor

What was the issue you were having and what was the above code supposed to do and how does it behave now? Can you open a new issue with answers to those questions? Thanks! That'll help us help you.

@t2ca
Copy link
Contributor

t2ca commented Apr 1, 2019

Thanks Kyle for taking the time to respond, i opened a new issue #12997

@narration-sd
Copy link

narration-sd commented Apr 5, 2019

Please see where the fix rolled in at 2.0.7 is breaking importantly expected 2.0.6 behavior, so that we need this regression optionable-out, thanks: #12997 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve location key for scroll-behaviors so it's not purely derived from the path
5 participants