From 5f09cc377d06e51df5b39abc2fb5300853907c6b Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Thu, 30 Aug 2018 13:54:06 -0700 Subject: [PATCH] Temp fix for scroll behavior problems --- packages/gatsby-link/src/index.js | 8 ++++++++ .../src/ScrollBehaviorContext.js | 9 +++++++-- packages/gatsby/cache-dir/navigation.js | 2 ++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/gatsby-link/src/index.js b/packages/gatsby-link/src/index.js index 4d7e5d1e489f9..1aefcfe79fade 100644 --- a/packages/gatsby-link/src/index.js +++ b/packages/gatsby-link/src/index.js @@ -4,6 +4,11 @@ import React from "react" import { Link, Location } from "@reach/router" import { parsePath } from "gatsby" +// Temp hack while awaiting https://github.com/reach/router/issues/119 +if (typeof window !== `undefined`) { + window.__navigatingToLink = false +} + export function withPrefix(path) { return normalizePath(`${__PATH_PREFIX__}/${path}`) } @@ -151,6 +156,9 @@ class GatsbyLink extends React.Component { } } + // Temp hack while awaiting https://github.com/reach/router/issues/119 + window.__navigatingToLink = true + // Make sure the necessary scripts and data are // loaded before continuing. navigate(prefixedTo, { state }) diff --git a/packages/gatsby-react-router-scroll/src/ScrollBehaviorContext.js b/packages/gatsby-react-router-scroll/src/ScrollBehaviorContext.js index ac9e996ca6f9c..1b9d30700c382 100644 --- a/packages/gatsby-react-router-scroll/src/ScrollBehaviorContext.js +++ b/packages/gatsby-react-router-scroll/src/ScrollBehaviorContext.js @@ -49,9 +49,14 @@ class ScrollContext extends React.Component { // The "scroll-behavior" package expects the "action" to be on the location // object so let's copy it over. - if (!location.action) { - location.action = `push` + + // Temp hack while awaiting https://github.com/reach/router/issues/119 + if (window.__navigatingToLink) { + location.action = `PUSH` + } else { + location.action = `POP` } + this.scrollBehavior.updateScroll(prevRouterProps, { history, location }) } diff --git a/packages/gatsby/cache-dir/navigation.js b/packages/gatsby/cache-dir/navigation.js index 9a64e1eeb5729..242be3053f878 100644 --- a/packages/gatsby/cache-dir/navigation.js +++ b/packages/gatsby/cache-dir/navigation.js @@ -42,6 +42,8 @@ const onPreRouteUpdate = location => { const onRouteUpdate = location => { if (!maybeRedirect(location.pathname)) { apiRunner(`onRouteUpdate`, { location }) + // Temp hack while awaiting https://github.com/reach/router/issues/119 + window.__navigatingToLink = false } }