From e7e5cb4ec3864109c8252bd9f91f1cf3d092fbbc Mon Sep 17 00:00:00 2001 From: sapiensfio Date: Fri, 20 Jan 2023 01:15:20 +1300 Subject: [PATCH] fix(gatsby-react-router-scroll): fix issues with anchor links (#37498) Co-authored-by: Mitchell Bennett Fixes https://github.com/gatsbyjs/gatsby/pull/28555 --- .../gatsby-react-router-scroll/src/scroll-handler.tsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/gatsby-react-router-scroll/src/scroll-handler.tsx b/packages/gatsby-react-router-scroll/src/scroll-handler.tsx index a0a40d691daea..f6868bc00ac71 100644 --- a/packages/gatsby-react-router-scroll/src/scroll-handler.tsx +++ b/packages/gatsby-react-router-scroll/src/scroll-handler.tsx @@ -59,10 +59,13 @@ export class ScrollHandler extends React.Component< scrollPosition = this._stateStorage.read(this.props.location, key) } - if (scrollPosition) { - this.windowScroll(scrollPosition, undefined) - } else if (hash) { + /** If a hash is present in the browser url as the component mounts (i.e. the user is navigating + * from an external website) then scroll to the hash instead of any previously stored scroll + * position. */ + if (hash) { this.scrollToHash(decodeURI(hash), undefined) + } else if (scrollPosition) { + this.windowScroll(scrollPosition, undefined) } }