From 58b86753eb08682388d5b8578d1e1a38efaf95a5 Mon Sep 17 00:00:00 2001 From: Niko Simonson Date: Wed, 31 Jan 2018 19:30:14 -0800 Subject: [PATCH 1/4] Pass full location object to history --- packages/gatsby/cache-dir/production-app.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/gatsby/cache-dir/production-app.js b/packages/gatsby/cache-dir/production-app.js index 8cb9a4622edeb..3e1a7cb8bbdc7 100644 --- a/packages/gatsby/cache-dir/production-app.js +++ b/packages/gatsby/cache-dir/production-app.js @@ -7,6 +7,7 @@ import ReactDOM from "react-dom" import { Router, Route, withRouter, matchPath } from "react-router-dom" import { ScrollContext } from "gatsby-react-router-scroll" import domReady from "domready" +import { createLocation } from "history" import history from "./history" window.___history = history import emitter from "./emitter" @@ -50,7 +51,9 @@ apiRunnerAsync(`onClientEntry`).then(() => { require(`./register-service-worker`) } - const navigateTo = pathname => { + const navigateTo = to => { + const location = createLocation(to, null, null, history.location) + let { pathname } = location const redirect = redirectMap[pathname] // If we're redirecting, just replace the passed in pathname @@ -70,7 +73,7 @@ apiRunnerAsync(`onClientEntry`).then(() => { if (e.page.path === loader.getPage(pathname).path) { emitter.off(`onPostLoadPageResources`, eventHandler) clearTimeout(timeoutId) - window.___history.push(pathname) + window.___history.push(location) } } @@ -79,13 +82,13 @@ apiRunnerAsync(`onClientEntry`).then(() => { const timeoutId = setTimeout(() => { emitter.off(`onPostLoadPageResources`, eventHandler) emitter.emit(`onDelayedLoadPageResources`, { pathname }) - window.___history.push(pathname) + window.___history.push(location) }, 1000) if (loader.getResourcesForPathname(pathname)) { // The resources are already loaded so off we go. clearTimeout(timeoutId) - window.___history.push(pathname) + window.___history.push(location) } else { // They're not loaded yet so let's add a listener for when // they finish loading. From 8b8c5ae0ed8625c5d3311b78364f7bcb9c20a829 Mon Sep 17 00:00:00 2001 From: Niko Simonson Date: Wed, 31 Jan 2018 19:41:03 -0800 Subject: [PATCH 2/4] Update navigateTo param to be more precise --- packages/gatsby-link/src/index.js | 4 ++-- packages/gatsby/cache-dir/root.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/gatsby-link/src/index.js b/packages/gatsby-link/src/index.js index 209a317ac99d4..4b2d0ba83ad63 100644 --- a/packages/gatsby-link/src/index.js +++ b/packages/gatsby-link/src/index.js @@ -184,6 +184,6 @@ GatsbyLink.contextTypes = { export default GatsbyLink -export const navigateTo = pathname => { - window.___navigateTo(pathname) +export const navigateTo = to => { + window.___navigateTo(to) } diff --git a/packages/gatsby/cache-dir/root.js b/packages/gatsby/cache-dir/root.js index c975b8834270c..633d5cfc650b3 100644 --- a/packages/gatsby/cache-dir/root.js +++ b/packages/gatsby/cache-dir/root.js @@ -132,8 +132,8 @@ const addNotFoundRoute = () => { } } -const navigateTo = pathname => { - window.___history.push(pathname) +const navigateTo = to => { + window.___history.push(to) } window.___navigateTo = navigateTo From 31e9adb19c7d9c9ddd3aac1fe05d6a973f3857d9 Mon Sep 17 00:00:00 2001 From: Niko Simonson Date: Wed, 31 Jan 2018 19:42:10 -0800 Subject: [PATCH 3/4] Pass location to navigateTo in gatsby-link --- packages/gatsby-link/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby-link/src/index.js b/packages/gatsby-link/src/index.js index 4b2d0ba83ad63..a19d7c8214592 100644 --- a/packages/gatsby-link/src/index.js +++ b/packages/gatsby-link/src/index.js @@ -157,7 +157,7 @@ class GatsbyLink extends React.Component { // loaded before continuing. if (process.env.NODE_ENV === `production`) { e.preventDefault() - window.___navigateTo(this.state.path) + window.___navigateTo(this.state.to) } } From 668ad1ac7b59ec89c08789b1fb2fa9f3fc7c7817 Mon Sep 17 00:00:00 2001 From: Niko Simonson Date: Wed, 31 Jan 2018 19:46:33 -0800 Subject: [PATCH 4/4] Update navigateTo types --- packages/gatsby-link/index.d.ts | 3 ++- packages/gatsby-link/package.json | 1 + yarn.lock | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-link/index.d.ts b/packages/gatsby-link/index.d.ts index 70510e4f56f18..cd97a84ec0065 100644 --- a/packages/gatsby-link/index.d.ts +++ b/packages/gatsby-link/index.d.ts @@ -1,5 +1,6 @@ import * as React from "react"; import { NavLinkProps } from "react-router-dom"; +import { LocationDescriptor } from "history"; export interface GatsbyLinkProps extends NavLinkProps { onClick?: (event: any) => void @@ -7,7 +8,7 @@ export interface GatsbyLinkProps extends NavLinkProps { style?:any; } -export const navigateTo: (path: string) => void; +export const navigateTo: (to: LocationDescriptor) => void; export const withPrefix: (path: string) => string; diff --git a/packages/gatsby-link/package.json b/packages/gatsby-link/package.json index a974d1143509e..92dd115d4948b 100644 --- a/packages/gatsby-link/package.json +++ b/packages/gatsby-link/package.json @@ -23,6 +23,7 @@ "gatsby": "^1.0.0" }, "dependencies": { + "@types/history": "^4.6.2", "@types/react-router-dom": "^4.2.2", "babel-runtime": "^6.26.0", "prop-types": "^15.5.8", diff --git a/yarn.lock b/yarn.lock index 6749c6ffd74cc..e36ecd84dd0a3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,6 +2,10 @@ # yarn lockfile v1 +"@types/history@*", "@types/history@^4.6.2": + version "4.6.2" + resolved "https://registry.yarnpkg.com/@types/history/-/history-4.6.2.tgz#12cfaba693ba20f114ed5765467ff25fdf67ddb0" + "@types/inline-style-prefixer@^3.0.0": version "3.0.1" resolved "https://registry.yarnpkg.com/@types/inline-style-prefixer/-/inline-style-prefixer-3.0.1.tgz#8541e636b029124b747952e9a28848286d2b5bf6" @@ -14,6 +18,21 @@ version "6.0.90" resolved "https://registry.yarnpkg.com/@types/node/-/node-6.0.90.tgz#0ed74833fa1b73dcdb9409dcb1c97ec0a8b13b02" +"@types/react-router-dom@^4.2.2": + version "4.2.3" + resolved "https://registry.yarnpkg.com/@types/react-router-dom/-/react-router-dom-4.2.3.tgz#06e0b67ff536adc0681dffdbe592ae91fb85887d" + dependencies: + "@types/history" "*" + "@types/react" "*" + "@types/react-router" "*" + +"@types/react-router@*": + version "4.0.21" + resolved "https://registry.yarnpkg.com/@types/react-router/-/react-router-4.0.21.tgz#d6c7ea3b45dba02eb8f869629f3b7d7b6e9a7938" + dependencies: + "@types/history" "*" + "@types/react" "*" + "@types/react@*": version "16.0.18" resolved "https://registry.yarnpkg.com/@types/react/-/react-16.0.18.tgz#c08eea79ada55bf10b5353e18c21797dd17afa23" @@ -11970,6 +11989,10 @@ shallow-clone@^0.1.2: lazy-cache "^0.2.3" mixin-object "^2.0.1" +shallow-compare@^1.2.2: + version "1.2.2" + resolved "https://registry.yarnpkg.com/shallow-compare/-/shallow-compare-1.2.2.tgz#fa4794627bf455a47c4f56881d8a6132d581ffdb" + shallow-equal@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/shallow-equal/-/shallow-equal-1.0.0.tgz#508d1838b3de590ab8757b011b25e430900945f7"