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

Back button from anchor link does not scroll to previous location #8019

Closed
wodin opened this issue Sep 10, 2018 · 26 comments
Closed

Back button from anchor link does not scroll to previous location #8019

wodin opened this issue Sep 10, 2018 · 26 comments
Labels
help wanted Issue with a clear description that the community can help with. status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer.

Comments

@wodin
Copy link

wodin commented Sep 10, 2018

Description

Scrolling to an anchor on the current page works, but clicking the browser's Back button does not scroll back to the previous location.

This is the same as bug #3116, which was closed because of lack of "recent activity", but I can still reproduce it on Firefox and Chromium on Linux and macOS. I have not tried Windows again recently. Opening a new issue as suggested in the last comment.

Steps to reproduce

On https://www.gatsbyjs.org/docs/deploy-gatsby/, scrolled all the way to the top, click on the GitLab Pages link. That scrolls down the page to the right section. Now click the browser's Back button.

Expected result

The browser should scroll back to the top of the page.

Actual result

Instead, the scroll position is not altered.

Demonstration

Environment

This is happening on gatsbyjs.org. I am not using Gatsby myself.

@kakadiadarpan kakadiadarpan added the help wanted Issue with a clear description that the community can help with. label Sep 10, 2018
@kakadiadarpan
Copy link
Contributor

I just tried this on macOS and could reproduce on Chrome (v 69.0.3497.81).

Fixes welcome if anyone can investigate further!

@kakadiadarpan kakadiadarpan added status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. status: inkteam to review labels Sep 10, 2018
@tjgilpin
Copy link

tjgilpin commented Sep 11, 2018

Can confirm this issue on the following browsers on Mac OS 10.12.6

  • Google Chrome Version 69.0.3497.81 (Official Build) (64-bit)
  • Firefox Quantum 62.0 (64-bit)
  • Safari Version 11.1.2 (12605.3.8.1)

Steps to reproduce

  1. gatsby new gatsby-v2-issue-8019 https://github.com/gatsbyjs/gatsby-starter-default#v2
  2. Paste following into src/pages/index.js between the Layout tags
    <ul>
      <li>
        <Link to="#bacon">Bacon</Link> 
      </li>
      <li>
        <Link to="#drumstick">Drumstick</Link> 
      </li>
      <li>
        <Link to="#porkchop">Pork Chop</Link> 
      </li>
      <li>
        <Link to="#ribeye">Rib Eye</Link> 
      </li>
      <li>
        <Link to="#sausage">Sausage</Link> 
      </li>
    </ul>
    <div id="bacon">
      <h1>Bacon</h1>
      <p>
        Spicy jalapeno bacon ipsum dolor amet leberkas exercitation tempor shankle cillum strip steak officia ham hock enim drumstick in. Boudin rump tongue, ball tip brisket pig hamburger esse duis lorem commodo laboris pastrami cow. Beef cillum doner do. In cillum turkey, beef duis pork belly turducken. Tail beef flank, aliqua culpa shoulder pastrami.
        Spicy jalapeno bacon ipsum dolor amet leberkas exercitation tempor shankle cillum strip steak officia ham hock enim drumstick in. Boudin rump tongue, ball tip brisket pig hamburger esse duis lorem commodo laboris pastrami cow. Beef cillum doner do. In cillum turkey, beef duis pork belly turducken. Tail beef flank, aliqua culpa shoulder pastrami.
        Spicy jalapeno bacon ipsum dolor amet leberkas exercitation tempor shankle cillum strip steak officia ham hock enim drumstick in. Boudin rump tongue, ball tip brisket pig hamburger esse duis lorem commodo laboris pastrami cow. Beef cillum doner do. In cillum turkey, beef duis pork belly turducken. Tail beef flank, aliqua culpa shoulder pastrami.
      </p>
    </div>  
    <div id="drumstick">
      <h1>Drumstick</h1>
      <p>
        Drumstick doner buffalo id picanha ham. Brisket shoulder dolore, velit sint ex pork chicken fugiat eiusmod. Do capicola swine filet mignon. T-bone ribeye minim, capicola nostrud in laboris sint turkey esse nisi ut officia tempor tail. Swine aliqua elit aliquip consequat ground round culpa ut esse officia doner jowl pork chop cow.
        Drumstick doner buffalo id picanha ham. Brisket shoulder dolore, velit sint ex pork chicken fugiat eiusmod. Do capicola swine filet mignon. T-bone ribeye minim, capicola nostrud in laboris sint turkey esse nisi ut officia tempor tail. Swine aliqua elit aliquip consequat ground round culpa ut esse officia doner jowl pork chop cow.
        Drumstick doner buffalo id picanha ham. Brisket shoulder dolore, velit sint ex pork chicken fugiat eiusmod. Do capicola swine filet mignon. T-bone ribeye minim, capicola nostrud in laboris sint turkey esse nisi ut officia tempor tail. Swine aliqua elit aliquip consequat ground round culpa ut esse officia doner jowl pork chop cow.
      </p>
    </div> 
    <div id="porkchop">
      <h1>Pork Chop</h1>
      <p>
        Pork chop deserunt ham hock, pork occaecat ipsum burgdoggen pastrami picanha bacon minim dolore. Ullamco ribeye ut, burgdoggen ground round jerky veniam corned beef. Pariatur officia pork corned beef jerky. Labore leberkas laboris tempor jerky meatloaf aliqua sint bresaola prosciutto officia shankle. Short ribs culpa shank corned beef.
        Pork chop deserunt ham hock, pork occaecat ipsum burgdoggen pastrami picanha bacon minim dolore. Ullamco ribeye ut, burgdoggen ground round jerky veniam corned beef. Pariatur officia pork corned beef jerky. Labore leberkas laboris tempor jerky meatloaf aliqua sint bresaola prosciutto officia shankle. Short ribs culpa shank corned beef.
        Pork chop deserunt ham hock, pork occaecat ipsum burgdoggen pastrami picanha bacon minim dolore. Ullamco ribeye ut, burgdoggen ground round jerky veniam corned beef. Pariatur officia pork corned beef jerky. Labore leberkas laboris tempor jerky meatloaf aliqua sint bresaola prosciutto officia shankle. Short ribs culpa shank corned beef.
      </p>
    </div> 
    <div id="ribeye">
      <h1>Ribeye</h1>
      <p>
        Lorem ribeye laboris sirloin, pastrami picanha tail shoulder. Eu aute chuck ham hock tri-tip swine, dolor shoulder occaecat sirloin cupim minim drumstick bresaola. Shoulder consectetur sint buffalo. Consequat incididunt ea capicola sausage. Consequat velit tenderloin ground round exercitation jowl. Beef ribs ribeye aliqua laborum consequat t-bone buffalo. Pastrami aliqua pariatur mollit biltong, leberkas in nisi magna officia est t-bone in.
        Lorem ribeye laboris sirloin, pastrami picanha tail shoulder. Eu aute chuck ham hock tri-tip swine, dolor shoulder occaecat sirloin cupim minim drumstick bresaola. Shoulder consectetur sint buffalo. Consequat incididunt ea capicola sausage. Consequat velit tenderloin ground round exercitation jowl. Beef ribs ribeye aliqua laborum consequat t-bone buffalo. Pastrami aliqua pariatur mollit biltong, leberkas in nisi magna officia est t-bone in.
        Lorem ribeye laboris sirloin, pastrami picanha tail shoulder. Eu aute chuck ham hock tri-tip swine, dolor shoulder occaecat sirloin cupim minim drumstick bresaola. Shoulder consectetur sint buffalo. Consequat incididunt ea capicola sausage. Consequat velit tenderloin ground round exercitation jowl. Beef ribs ribeye aliqua laborum consequat t-bone buffalo. Pastrami aliqua pariatur mollit biltong, leberkas in nisi magna officia est t-bone in.
      </p>
    </div> 
    <div id="sausage">
      <h1>Sausage</h1>
      <p>
        Swine sausage prosciutto, occaecat dolore beef incididunt irure leberkas. Ullamco qui burgdoggen labore, culpa porchetta corned beef adipisicing. Biltong anim irure corned beef veniam reprehenderit ribeye doner, dolor ea prosciutto. Qui eiusmod do est non reprehenderit, sunt exercitation salami meatloaf in short loin aliqua pastrami velit. Andouille frankfurter et short loin picanha kielbasa pastrami salami eu pork irure beef qui. Ribeye est rump tongue cillum strip steak pariatur ut qui.
        Swine sausage prosciutto, occaecat dolore beef incididunt irure leberkas. Ullamco qui burgdoggen labore, culpa porchetta corned beef adipisicing. Biltong anim irure corned beef veniam reprehenderit ribeye doner, dolor ea prosciutto. Qui eiusmod do est non reprehenderit, sunt exercitation salami meatloaf in short loin aliqua pastrami velit. Andouille frankfurter et short loin picanha kielbasa pastrami salami eu pork irure beef qui. Ribeye est rump tongue cillum strip steak pariatur ut qui.
        Swine sausage prosciutto, occaecat dolore beef incididunt irure leberkas. Ullamco qui burgdoggen labore, culpa porchetta corned beef adipisicing. Biltong anim irure corned beef veniam reprehenderit ribeye doner, dolor ea prosciutto. Qui eiusmod do est non reprehenderit, sunt exercitation salami meatloaf in short loin aliqua pastrami velit. Andouille frankfurter et short loin picanha kielbasa pastrami salami eu pork irure beef qui. Ribeye est rump tongue cillum strip steak pariatur ut qui.
      </p>
    </div> 

(FWIW replacing the Link tag with a standard hyperlink does not fix the issue)

  1. cd gatsby-v2-issue-8019
  2. gatsby develop
  3. Open http://localhost:8000
  4. Click on sausage link
  5. Click on back button

Environment

  System:
    OS: macOS Sierra 10.12.6
    CPU: x64 Intel(R) Core(TM) i5-4288U CPU @ 2.60GHz
    Shell: 2.6.0 - /usr/local/bin/fish
  Binaries:
    Node: 10.2.1 - /usr/local/bin/node
    Yarn: 1.7.0 - /usr/local/bin/yarn
    npm: 6.2.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 69.0.3497.81
    Firefox: 62.0
    Safari: 11.1.2
  npmPackages:
    gatsby: next => 2.0.0-rc.19
    gatsby-plugin-manifest: next => 2.0.2-rc.1
    gatsby-plugin-offline: next => 2.0.0-rc.6
    gatsby-plugin-react-helmet: next => 3.0.0-rc.1
  npmGlobalPackages:
    gatsby-cli: 1.1.58
    gatsby: 1.9.277

Issue title

I'd suggest that Back button from anchor link does not scroll to previous location is a more descriptive issue title.

@stefanprobst
Copy link
Contributor

AFAICT this is because hash link scrolling is handled by <Link />, which is not invoked when navigating with browser buttons. Moving this to shouldUpdateScroll does seem to work, and also seems like a better place to do this.

@wodin wodin changed the title Broken scroll behaviour within a page Back button from anchor link does not scroll to previous location Sep 11, 2018
@hackily
Copy link

hackily commented Oct 27, 2018

Can confirm the issue is still present on v1 documentation
https://v1.gatsbyjs.org/docs/deploy-gatsby/

But appears fixed in current (v2) documentation on all browsers below
https://www.gatsbyjs.org/docs/recipes/

Recreation: Click the anchor links and hit back.

Windows 10 Version 10.0.17134 Build 17134

  • Firefox Quantum 63.0 (64-bit)
  • Chrome Version 69.0.3497.100 (Official Build) (64-bit)
  • Microsoft Edge 42.17134.1.0

@wodin
Copy link
Author

wodin commented Oct 27, 2018

The original bug is no longer reproducible for me, however there's a new one. The back button still does not scroll to the correct location. It just scrolls to the top of the page.

On https://www.gatsbyjs.org/docs/recipes/ scroll down until the Transforming data link is at the top of the page. Click on the Transforming data link. The browser correctly scrolls to the Transforming data section at the bottom of the page. Now click the back button and instead of scrolling back to the Transforming data link, it scrolls right to the very top of the page.

Reproduced on:
Firefox Quantum 62.0.3 (64-bit) (macOS High Sierra)
Firefox Quantum 63.0 (32-bit) (Windows 10)
Firefox Quantum 63.0 (32-bit) (Ubuntu 18.04)
Google Chrome 69.0.3497.100 (Official Build) (64-bit) (macOS High Sierra)
Chromium 70.0.3538.67 (Official Build) (Ubuntu 18.04)
Microsoft Edge 42.17677.1000.0 (Windows 10)
Safari 11.1 (13605.1.33.1.4) (macOS High Sierra)

As before, the scrolling works correctly in IE11.

@hackily
Copy link

hackily commented Oct 27, 2018

I believe that behavior is expected with anchor links. There is no logic to save your the previous scroll location, that is just the browser's default behavior.

@wodin
Copy link
Author

wodin commented Oct 28, 2018

I believe that behavior is expected with anchor links. There is no logic to save your the previous scroll location, that is just the browser's default behavior.

No, that is not true.

Try this:

$ curl -o /tmp/recipes.html https://www.gatsbyjs.org/docs/recipes/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  148k  100  148k    0     0  42422      0  0:00:03  0:00:03 --:--:-- 42410

Then open file:///tmp/recipes.html and try to reproduce the problem.
This scrolls correctly for me in Firefox, Chrome, Safari, etc. unlike https://www.gatsbyjs.org/docs/recipes/

@gatsbot
Copy link

gatsbot bot commented Jan 26, 2019

Old issues will be closed after 30 days of inactivity. This issue has been quiet for 20 days and is being marked as stale. Reply here or add the label "not stale" to keep this issue open!

@gatsbot gatsbot bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Jan 26, 2019
@wodin
Copy link
Author

wodin commented Jan 26, 2019

image

What? I was the last person to respond! And the scroll behaviour wrt. anchor links is still broken.

@gatsbot
Copy link

gatsbot bot commented Feb 6, 2019

Hey again!

It’s been 30 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it.

Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.

Thanks again for being part of the Gatsby community!

@gatsbot gatsbot bot closed this as completed Feb 6, 2019
@wodin
Copy link
Author

wodin commented Feb 6, 2019

If the bot is going to close issues regardless of replies, perhaps it should not suggest that one "reply here ... to keep this issue open" 😥

@m-allanson
Copy link
Contributor

:( sorry about that. There were some earlier issues with gatsbot being overenthusiastic which should be fixed now.

@m-allanson m-allanson reopened this Feb 7, 2019
@m-allanson m-allanson removed the stale? Issue that may be closed soon due to the original author not responding any more. label Feb 7, 2019
@gatsbot gatsbot bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Feb 28, 2019
@gatsbot
Copy link

gatsbot bot commented Feb 28, 2019

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! 💪💜

@yogeshkotadiya yogeshkotadiya added not stale and removed stale? Issue that may be closed soon due to the original author not responding any more. labels Mar 1, 2019
@wodin
Copy link
Author

wodin commented Mar 5, 2019

This is still a problem. It can be reproduced on https://www.gatsbyjs.org/docs/recipes

@Galzk
Copy link

Galzk commented Jul 9, 2019

Hey, I'm waiting for a solution as well on this one, still reproducible on our project :)

@vulcanr
Copy link

vulcanr commented Apr 14, 2020

Just did the procedure @wodin to reproduce the issue, but I'm unable to reproduce it.

OS: Ubuntu 19.10
Browsers:

  • Brave - Version 1.5.123 Chromium: 80.0.3987.163 (Official Build) (64-bit)
  • Firefox 75.0 (64bit)

@wodin
Copy link
Author

wodin commented Apr 16, 2020

Ah! It's not perfect, but it's vastly improved.

Since the /docs/deploy-gatsby/ link no longer exists, you can test here:
https://www.gatsbyjs.org/docs/gatsby-config/

To see how it's supposed to work, open the following HTML file in a browser:
scroll-test.html.zip
(I had to zip it to appease GitHub)

On the gatsby-config page, click on one of the siteMetadata links. Then scroll part of the way towards the Plugins section. Then click on the Polyfill link and again scroll down a little.
If you click the Back button now it should go back to where you were when you clicked on the Polyfill link. i.e. somewhere in the siteMetadata section. Instead it scrolls to the top of the siteMetadata section.
If you click the Forward button now it should go back to where you were just before clicking on the Back button, i.e. somewhere within the Polyfill section. Instead it scrolls to the top of the Polyfill section.

If you follow a similar procedure in the scroll-test.html file you'll see that the browser correctly takes you to where you were when clicking back/forward.

@stevemarksd
Copy link

Here still happens after updating all plugins.

@blainekasten
Copy link
Contributor

Fixed in latest!

@don-esteban
Copy link

Hi @blainekasten

the issue still persists. Tested with Firefox 80.0, Chrome 85.0.4183.83 and Safari 13.1.2 on macOS 10.15.6.

Reproduction steps (Firefox only):

gatsby new my-blog-starter https://github.com/gatsbyjs/gatsby-starter-blog
npm update

Insert this two lines somewhere in content/blog/new-beginnings/index.md

[Go to (markdown link)](#gohere)
<a href="#gohere">Go to (a href link)</a>

Insert the anchor somewhere below

<h3 id="gohere">Here You are. Now press the backbutton.</h3>

Back Button will not work:

  1. Visit start page http://localhost:8000/
  2. Click "New Beginnings"
  3. Click one of the anchor links
  4. Click back button

Won't return to top of page or previous position.

Back Button still won't work:

  1. Click browser refresh button
  2. Same as above

Back Button will work:

  1. Click in URL address input and hit enter key or visit http://localhost:8000/new-beginnings/ directly
  2. Now back button works as expected

@christina-grannas
Copy link

I have this problem in a project as well

@don-esteban
Copy link

I have this problem in a project as well

I don't remember all details. Our site (soon productive) works fine. Rules we use for <a> and <Link> are:

  1. Use <Link> for internal links
  2. Use<Link> for internal fragment links
  3. Use <a> for all external links
  4. Use <a> for all internal download links (PDF e.g)

Here you can load some manufacturer pages and test fragment links at the sidebars table of content section.

https://sx.surface-world.de/hersteller/

Hope this helps.

@christina-grannas
Copy link

christina-grannas commented Aug 26, 2021

I have this problem in a project as well

I don't remember all details. Our site (soon productive) works fine. Rules we use for <a> and <Link> are:

1. Use `<Link>` for internal links

2. Use`<Link>` for internal fragment links

3. Use `<a>` for all external links

4. Use `<a>` for all internal download links (PDF e.g)

Here you can load some manufacturer pages and test fragment links at the sidebars table of content section.

https://sx.surface-world.de/hersteller/

Hope this helps.

I tested your site as well and it is not working as I would expect as a user. Try this:

  1. Go to https://sx.surface-world.de/hersteller/
  2. Click "Adobe"
  3. Click "Adobe Acrobat 2020" in the right menu
  4. Scroll down a bit
  5. Click a link to some other page
  6. Click back button in browser

Behavior: I end up at "Adobe Acrobat 2020"

Expected behavior: I would expect to end up at the scroll position I was after scrolling in step 4. I believe this is the default behavior of the browser. You can compare with this page for example: https://www.w3.org/Graphics/Color/Workshop/talks.html

@don-esteban
Copy link

Yes, that's right and probably not the expected behavior. I'm very busy with the launch of the site and do not have time to address this issue at the moment. I put it at my "post release to do list". If you have time, I would recommend the following steps:

  1. Make a public GitHub minimal reproduction site (cloning a starter)
  2. Open an issue

Thanks for indicating this issue.

PS: Our site is a Gatsby 2.x site. Could one confirm this issue at Gatsby 3.x?

@christina-grannas
Copy link

christina-grannas commented Aug 30, 2021

Turns out that its a bug in reach/router, there is a comment in the code about it here:

/** There are two pieces of state: the browser url and

@nikolay543
Copy link

nikolay543 commented Dec 22, 2021

So for everybody that is currently experiencing the issue - Replace your internal links with the <Link> component. Since gatsby has overwritten some of the browser behavior, you need to use their component instead of normal <a> links to preserve the scroll position.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issue with a clear description that the community can help with. status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer.
Projects
None yet
Development

No branches or pull requests