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-react-router-scroll): fix issues with anchor links #37498

Merged
merged 1 commit into from
Jan 19, 2023

Conversation

sapiensfio
Copy link
Contributor

@sapiensfio sapiensfio commented Jan 19, 2023

Description

Fixes the following issue:

When a user visits an anchor link (i.e. from an external website or by entering it in their browser) and then scrolls, if the user revisits the same link (again, from an external website or by entering it in their browser) they are taken to where they last scrolled to instead of the actual anchor point.

Note that this PR makes fixes that were missed from issue #28555.

Please note the following comment that was made some time ago in the scroll-handler.js:


    /**  There are two pieces of state: the browser url and
     * history state which keeps track of scroll position
     * Native behaviour prescribes that we ought to restore scroll position
     * when a user navigates back in their browser (this is the `POP` action)
     * Currently, reach router has a bug that prevents this at https://github.com/reach/router/issues/228
     * So we _always_ stick to the url as a source of truth — if the url
     * contains a hash, we scroll to it
     */

Notably, the author writes about the URL being treated as the source of truth. The change in this PR enforces that behaviour.

Documentation

The feature is used simply by navigation via hyperlinks with anchors across different pages.

Related Issues

Fixes #28555.

…t if a hash is present in the URL otherwise scroll to the previously recorded position.
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jan 19, 2023
@LekoArts LekoArts added topic: frontend Relates to frontend issues (e.g. reach/router, gatsby-link, page-loading, asset-loading, navigation) and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jan 19, 2023
Copy link
Contributor

@LekoArts LekoArts left a comment

Choose a reason for hiding this comment

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

Thanks!

@LekoArts LekoArts merged commit e7e5cb4 into gatsbyjs:master Jan 19, 2023
@ZeldOcarina
Copy link

Hi! I am on Gatsby 5.6.0 but I am still experiencing these issues with anchor points being completely random.. let me know if I should open a separate issue..

@mordonez
Copy link

mordonez commented Nov 7, 2023

Still on Gatsby 5.12.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: frontend Relates to frontend issues (e.g. reach/router, gatsby-link, page-loading, asset-loading, navigation)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants