-
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
[v2] Navigating to previously visited pages with <Link> retains scroll position #7454
Comments
I can confirm this same behavior is happening on a couple sites I'm working on. I just noticed it today, but have no idea how long it has been happening. (I came here to search for the same issue or create a new one.) |
Could be related to #7450 ? |
I'd rather it default to the top of the page rather than the previous scroll position, it would create a more predictable experience. |
Could be related: #5656 The logic discussed for determining if something is a pop could be the issue. We want to restore scroll on pop (back button pushed), not if you're just going to a link you've been to before. There is no action on Reach Router so this probably doesn't work anymore: https://github.com/gatsbyjs/gatsby/pull/3775/files |
Verified, Here's a simple repro using the Blog-Starter . |
So it seems like this might be blocked by a necessary API change to reach-router? Otherwise it sounds like we have to keep a hash of seen routing keys in order to determine push vs pop |
FYI I'm seeing this not just with previously visited links, but with all links. Unless “previously visited” includes visiting the URL in a previous browser session. |
Same behavior over here. |
I too can confirm this behavior when using gatsby |
We came across this issue on https://www.zego.com and developed a work around by using our own custom // components/Link.js
import { Link as BaseLink } from 'gatsby';
if (typeof window !== 'undefined') {
window.__navigatingToLink = false;
}
const Link = ({ children, ...props }) => (
<BaseLink onClick={() => { window.__navigatingToLink = true; }} {...props}>
{children}
</BaseLink>
); Then in
It's pretty hacky but it works. All our page links scroll to the top of the page like expected and the back button still maintains the scroll position on the previous page. |
great thanks for the tip @jkimbo that works 👍 |
we like to jump to the menu on page navigations, excerpt for browser made ones like back button I had to fix this behaviour as reach/router does not (yet) provide the used action (POP, PUSH) using a quick method suggested by gatsbyjs/gatsby#7454 (comment)
@jkimbo great solution! I'll add that to core while we're awaiting a fix upstream in @reach/router. |
Here's the link to the @reach/router issue reach/router#119 |
I'm using v2.0.0-rc.24 and it seems to be having similar issue again. |
Did your temporary fix get reverted or canceled out somehow? I am seeing the undesirable scrolling behavior again using |
Some navigation code was rewritten recently which caused this behaviour to appear again - I think this is on @pieh's todo list edit: see #7812 (comment) |
@davidbailey00 and @pieh, Thanks for the update and background info! Hopefully this gets resolved in the near future as Let me know if you need help with anything. |
Yeah, I probably broke it and will take a look on this |
Hi guys, debugging this for a few hours last night, to no avail. However, this morning I was blessed with an epiphany sent by the software engineering gods! 🕊️ I had some pretty funky styles to make a togglable navigation drawer / menu appear and disappear by pushing away the app's main containing element. To make this work, I had added:
In my case, removing this style fixed the behaviour!! Hope this helps someone else. |
@davidbailey00 I'm currently having this issue. Here's a repo that reproduces this issue on gatsby https://github.com/thundernixon/gatsby-link-scroll-test Not only is it linking to previously-scrolled-to spots in pages (bad), but it’s also linking to the current location on unvisited pages (worse). That is, the pagination at the bottom of a “blog post” type of page will link to the bottom of the next page. I also have a header drop-down menu with links to different pages, and these also don't link to the tops of these pages. UPDATE: Nevermind; it was an issue with how I was using Gatsby Transition Link. It was resolved at TylerBarnes/gatsby-plugin-transition-link#21. |
@thundernixon Thanks for your update! I'll presume there's no further issue @AllanPooley Please could you share your repo, or a reproduction? It sounds like this could still be an issue, even if it only happens with the body styled as |
Hi, |
@ergunpp Please could you share your repo, or a minified reproduction, so we can investigate this? |
@davidbailey00 repo name: ergunpp/chefschoice |
The same issue currently exists when using semantic-ui-react and it's because of the following css code:
So i fixed it with the following:
|
Hi, my repo is ergunpp/chefschoice
…On 9 Mar 2019 Sat at 18:05 David Bailey ***@***.***> wrote:
@ergunpp <https://github.com/ergunpp> Please could you share your repo,
or a minified reproduction, so we can investigate this?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#7454 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AXv1VxuNMbJQ0E4PjF64PAMM8g5OKYxKks5vU82qgaJpZM4WDB7b>
.
|
@ergunpp I just had a look at your site - it seems the problem is caused by your |
I think after @pieh's fixes, any problems occurring are due to the |
If scroll-behavior has problems, we can PR fixes there. We've done that several times. |
@KyleAMathews I think it's not even a problem with |
Hi @davidbailey00, Your suggestions worked like a charm. Thanks for the great help! |
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open! Thanks for being a part of the Gatsby community! 💪💜 |
Closing, since this has been fixed (or else caused by dodgy plugins / CSS). Please open a new issue if any further problems occur :) |
This worked for me. To scroll to the top, make sure your path starts with // this works
<Link to="/some-path" /> I realized it wasn't scrolling to the top when I didn't have a // this doesn't work
<Link to="some-path" /> Not sure if that works for every case, but it worked for me. Hope it helps. |
@t2ca your I'm also using Semantic UI React and have added the following to html {
height: initial;
} |
After wasting a whole hour going through all the issues and references, nothing worked except manually setting the export const shouldUpdateScroll = () => {
document.body.scrollTop = 0
} Works for both refresh and page visits. |
This worked for me. gatsby-browser.js exports.onInitialClientRender = () => {
window.scrollTo(0, 0)
} I found this solution on |
It does not work, and seems it can't - definition of this function looks like this:
In my case it work great, no more scroll retain issue when navigating using Link from Gatsby.
|
This worked pretty well: // gatsby-browser.js
export const shouldUpdateScroll = () => {
return false
} Source: https://www.gatsbyjs.com/docs/reference/config-files/gatsby-browser/#shouldUpdateScroll edit: ^ this fucks up /#anchors (duh!), don't recommend, 0/10 edit2: ended up with this // gatsby-browser.js
export const shouldUpdateScroll = ({ routerProps }) => {
const hasAnchor = !!routerProps.location.hash; // (ie. /#features)
return hasAnchor
} |
Description
Clicking on a
<Link>
in v2 (111) appears to scroll you to a previous scroll position (if it exists) rather than scroll you to the top.Steps to reproduce
I've been able to recreate this in
gatsby-starter-default
:https://gatsby-v2-beta-111-scroll.netlify.com/
(The only change is artificial padding added to the first page above the
/page-2
<Link>
)page-2
link and click itpage-2
, now click 'Go back to the homepage'Expected result
You are taken back to the top of the home page
Actual result
You are taken back to the home page but scrolled down the page (at the exact same position you were when you linked the
page-2
link)Environment
Other notes
reach-router
, could this be linked?window.scrollTo(0, 0) / window.scroll({top: 0})
in pagecomponentDidMount
had no effect, though it would work after wrapping it in an arbitrarysetTimeout
- though this yields an initial flash of content in the wrong scroll position.By default, I imagine the desired behaviour would be for all clicked
<Link>
elements to scroll you to the top of that target page and that only using the browser back / forward buttons would retain scroll position state in this manner.The text was updated successfully, but these errors were encountered: