Skip to content

Commit e7e5cb4

Browse files
sapiensfioMitchell Bennett
and
Mitchell Bennett
authored
fix(gatsby-react-router-scroll): fix issues with anchor links (#37498)
Co-authored-by: Mitchell Bennett <[email protected]> Fixes #28555
1 parent fe65c29 commit e7e5cb4

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

packages/gatsby-react-router-scroll/src/scroll-handler.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ export class ScrollHandler extends React.Component<
5959
scrollPosition = this._stateStorage.read(this.props.location, key)
6060
}
6161

62-
if (scrollPosition) {
63-
this.windowScroll(scrollPosition, undefined)
64-
} else if (hash) {
62+
/** If a hash is present in the browser url as the component mounts (i.e. the user is navigating
63+
* from an external website) then scroll to the hash instead of any previously stored scroll
64+
* position. */
65+
if (hash) {
6566
this.scrollToHash(decodeURI(hash), undefined)
67+
} else if (scrollPosition) {
68+
this.windowScroll(scrollPosition, undefined)
6669
}
6770
}
6871

0 commit comments

Comments
 (0)