diff --git a/README.md b/README.md index 9d66e912b7e11..f9b4154647cac 100644 --- a/README.md +++ b/README.md @@ -334,6 +334,8 @@ files which start with an underscore: * (optional) `gatsby-browser.js` - a way to hook into key application events. * Export `onRouteUpdate` of type `function()` to be notified whenever React-Router navigates. * Export `modifyRoutes` of type `function(routes: Object) => Object` to modify the react-router routes. + * Export `shouldUpdateScroll` of type `function(prevRouterProps: Object, nextRouterProps: Object) => boolean` + to determine if a given route change should scroll. * (optional) `gatsby-node.js` - a way to hook into events during build and development. diff --git a/lib/utils/web-entry.js b/lib/utils/web-entry.js index 96c749f428e8f..46fa2666f4923 100644 --- a/lib/utils/web-entry.js +++ b/lib/utils/web-entry.js @@ -4,7 +4,7 @@ import ReactDOM from 'react-dom' import { applyRouterMiddleware, browserHistory, Router } from 'react-router' import useScroll from 'react-router-scroll/lib/useScroll' import createRoutes from 'create-routes' -import { onRouteChange, onRouteUpdate, modifyRoutes } from 'gatsby-browser' +import { onRouteChange, onRouteUpdate, modifyRoutes, shouldUpdateScroll } from 'gatsby-browser' const loadContext = require('.gatsby-context') @@ -33,9 +33,13 @@ function onUpdate () { } } -function shouldUpdateScroll (prevRouterProps, { location: { pathname } }) { +function defaultShouldUpdateScroll (prevRouterProps, nextRouterProps) { + if (shouldUpdateScroll) { + return shouldUpdateScroll(prevRouterProps, nextRouterProps) + } if (prevRouterProps) { const { location: { pathname: oldPathname } } = prevRouterProps + const { location: { pathname } } = nextRouterProps if (oldPathname === pathname) { return false } @@ -62,7 +66,7 @@ loadConfig(() => , typeof window !== 'undefined' ? document.getElementById('react-mount') : undefined)