Skip to content

Internal anchors with hashes not working #25778

Closed
@ediblecode

Description

@ediblecode

Description

Internal anchors with hashes stop working after the first click. See the minimal test case repo at https://github.com/ediblecode/gatsby-anchors-issue and demo site at https://frosty-brown-de3460.netlify.app/. Click into page 2 and click the various internal anchors on that page.

Note: this is using simple anchor tags as per the docs for gatsby link:

Neither <Link> nor navigate can be used for in-route navigation with a hash or query parameter. If you need this behavior, you should either use an anchor tag...

I thought this was fixed by #25749 and released in 2.24.3 but it still seems to be an issue. I think it's related to, if not the same as, #25554, #25745 and #25522. I wonder if it was caused by #24306 (or at least related in some way).

The first anchor seems to be OK but it's definitely something to do with subsquent clicks. Weirdly though it's non-determinate: sometimes it will work fine and sometimes it won't and I can't seem to work out the pattern. Looking into it, session storage is used under the hood for the scroll position, so I wonder if it's something to do with that - maybe the key used isn't including the anchor hash or something? Anyway, that's as far as I got with my digging, sorry. Hoping @blainekasten you might be able to take a look as you've looked at the other related issues. Thank you very much.

Steps to reproduce

  1. Clone the gatsby starter
  2. Upgrade dependencies to latest via npx npm-check-updates -u
    1. This includes Gatsby 2.24.3
  3. Run npm i to install these dependencies
  4. Create a page with hash links to elements on a page with matching ids (see example repo and site)
  5. Click the internal hash anchors to try to jump up and down the page.

Expected result

The URL is updated with the hash, and the page is scrolled to the element with an id matching the hash in the URL.

Actual result

The first click scrolls to the target element and updates the URL but subsequent clicks only update the URL without moving down the page.

Environment

System:
OS: Windows 10 10.0.19041
CPU: (4) x64 Intel(R) Core(TM) i7-5500U CPU @ 2.40GHz
Binaries:
Node: 12.16.3 - C:\Program Files\nodejs\node.EXE
Yarn: 1.16.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.14.4 - C:\Program Files\nodejs\npm.CMD
Languages:
Python: 2.7.11 - C:\Python27\python.EXE
Browsers:
Edge: 44.19041.1.0
npmPackages:
gatsby: ^2.24.3 => 2.24.3
gatsby-image: ^2.4.13 => 2.4.13
gatsby-plugin-manifest: ^2.4.18 => 2.4.18
gatsby-plugin-offline: ^3.2.18 => 3.2.18
gatsby-plugin-react-helmet: ^3.3.10 => 3.3.10
gatsby-plugin-sharp: ^2.6.19 => 2.6.19
gatsby-source-filesystem: ^2.3.19 => 2.3.19
gatsby-transformer-sharp: ^2.5.11 => 2.5.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: coreRelates to Gatsby's core (e.g. page loading, reporter, state machine)type: bugAn issue or pull request relating to a bug in Gatsby

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions