From ef22eb24670b5b84851e4107cf05b54e22451c6a Mon Sep 17 00:00:00 2001 From: Joe Gregory Date: Mon, 3 Jan 2022 11:55:20 -0800 Subject: [PATCH] fix: Prevent scroll to top after query param update --- gatsby-browser.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/gatsby-browser.js b/gatsby-browser.js index c66e4533d..b1a804848 100644 --- a/gatsby-browser.js +++ b/gatsby-browser.js @@ -55,4 +55,19 @@ const onClientEntry = () => { window.ReactDOM = require('react-dom'); }; -export { wrapPageElement, onInitialClientRender, onClientEntry }; +const shouldUpdateScroll = ({ routerProps: { location } }) => { + const transitionDelay = 600; + + // Scroll window to top position if there is no search (i.e., page load) + if (!location.search) { + window.setTimeout(() => window.scrollTo(0, 0), transitionDelay); + } + return false; +}; + +export { + wrapPageElement, + onInitialClientRender, + onClientEntry, + shouldUpdateScroll, +};