From 1c44f0eb5fdba3182cf7e7800534c25f6c76fb94 Mon Sep 17 00:00:00 2001 From: Rusta Date: Tue, 7 Feb 2017 17:28:23 +0000 Subject: [PATCH 1/3] enable custom shouldUpdateScroll function in gatsby-browser file --- lib/utils/web-entry.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/utils/web-entry.js b/lib/utils/web-entry.js index 96c749f428e8f..e23c0112b5084 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, customShouldUpdateScroll } from 'gatsby-browser' const loadContext = require('.gatsby-context') @@ -33,9 +33,13 @@ function onUpdate () { } } -function shouldUpdateScroll (prevRouterProps, { location: { pathname } }) { +function shouldUpdateScroll (prevRouterProps, nextRouterProps) { + if (customShouldUpdateScroll) { + return customShouldUpdateScroll(prevRouterProps, nextRouterProps) + } if (prevRouterProps) { const { location: { pathname: oldPathname } } = prevRouterProps + const { location: { pathname } } = nextRouterProps if (oldPathname === pathname) { return false } From 7f853c8a13e80058cc95121d0f810c4e4f0a8f3d Mon Sep 17 00:00:00 2001 From: Rusta Date: Tue, 7 Feb 2017 19:05:03 +0000 Subject: [PATCH 2/3] add sensible name for the shouldUpdateScroll API function; add details of this override function to the README --- README.md | 2 ++ lib/utils/web-entry.js | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) 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 e23c0112b5084..c9fad5e8be424 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, customShouldUpdateScroll } from 'gatsby-browser' +import { onRouteChange, onRouteUpdate, modifyRoutes, shouldUpdateScroll } from 'gatsby-browser' const loadContext = require('.gatsby-context') @@ -33,9 +33,9 @@ function onUpdate () { } } -function shouldUpdateScroll (prevRouterProps, nextRouterProps) { - if (customShouldUpdateScroll) { - return customShouldUpdateScroll(prevRouterProps, nextRouterProps) +function updateScroll (prevRouterProps, nextRouterProps) { + if (shouldUpdateScroll) { + return shouldUpdateScroll(prevRouterProps, nextRouterProps) } if (prevRouterProps) { const { location: { pathname: oldPathname } } = prevRouterProps @@ -66,7 +66,7 @@ loadConfig(() => , typeof window !== 'undefined' ? document.getElementById('react-mount') : undefined) From 3693947f3c359f3891d6d5877ee037d5d3bc7c7a Mon Sep 17 00:00:00 2001 From: Rusta Date: Tue, 7 Feb 2017 19:40:21 +0000 Subject: [PATCH 3/3] rename function to defaultShouldUpdateScroll --- lib/utils/web-entry.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/utils/web-entry.js b/lib/utils/web-entry.js index c9fad5e8be424..46fa2666f4923 100644 --- a/lib/utils/web-entry.js +++ b/lib/utils/web-entry.js @@ -33,7 +33,7 @@ function onUpdate () { } } -function updateScroll (prevRouterProps, nextRouterProps) { +function defaultShouldUpdateScroll (prevRouterProps, nextRouterProps) { if (shouldUpdateScroll) { return shouldUpdateScroll(prevRouterProps, nextRouterProps) } @@ -66,7 +66,7 @@ loadConfig(() => , typeof window !== 'undefined' ? document.getElementById('react-mount') : undefined)