Skip to content

Commit 869bb2c

Browse files
nsimonsonKyleAMathews
authored andcommitted
[gatsby] Pass location state to history (#3802)
* Pass full location object to history * Update navigateTo param to be more precise * Pass location to navigateTo in gatsby-link * Update navigateTo types
1 parent 1106bbb commit 869bb2c

File tree

6 files changed

+38
-10
lines changed

6 files changed

+38
-10
lines changed

packages/gatsby-link/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import * as React from "react";
22
import { NavLinkProps } from "react-router-dom";
3+
import { LocationDescriptor } from "history";
34

45
export interface GatsbyLinkProps extends NavLinkProps {
56
onClick?: (event: any) => void
67
className?: string
78
style?:any;
89
}
910

10-
export const navigateTo: (path: string) => void;
11+
export const navigateTo: (to: LocationDescriptor) => void;
1112

1213
export const withPrefix: (path: string) => string;
1314

packages/gatsby-link/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"gatsby": "^1.0.0"
2424
},
2525
"dependencies": {
26+
"@types/history": "^4.6.2",
2627
"@types/react-router-dom": "^4.2.2",
2728
"babel-runtime": "^6.26.0",
2829
"prop-types": "^15.5.8",

packages/gatsby-link/src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class GatsbyLink extends React.Component {
157157
// loaded before continuing.
158158
if (process.env.NODE_ENV === `production`) {
159159
e.preventDefault()
160-
window.___navigateTo(this.state.path)
160+
window.___navigateTo(this.state.to)
161161
}
162162
}
163163

@@ -184,6 +184,6 @@ GatsbyLink.contextTypes = {
184184

185185
export default GatsbyLink
186186

187-
export const navigateTo = pathname => {
188-
window.___navigateTo(pathname)
187+
export const navigateTo = to => {
188+
window.___navigateTo(to)
189189
}

packages/gatsby/cache-dir/production-app.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ReactDOM from "react-dom"
77
import { Router, Route, withRouter, matchPath } from "react-router-dom"
88
import { ScrollContext } from "gatsby-react-router-scroll"
99
import domReady from "domready"
10+
import { createLocation } from "history"
1011
import history from "./history"
1112
window.___history = history
1213
import emitter from "./emitter"
@@ -50,7 +51,9 @@ apiRunnerAsync(`onClientEntry`).then(() => {
5051
require(`./register-service-worker`)
5152
}
5253

53-
const navigateTo = pathname => {
54+
const navigateTo = to => {
55+
const location = createLocation(to, null, null, history.location)
56+
let { pathname } = location
5457
const redirect = redirectMap[pathname]
5558

5659
// If we're redirecting, just replace the passed in pathname
@@ -70,7 +73,7 @@ apiRunnerAsync(`onClientEntry`).then(() => {
7073
if (e.page.path === loader.getPage(pathname).path) {
7174
emitter.off(`onPostLoadPageResources`, eventHandler)
7275
clearTimeout(timeoutId)
73-
window.___history.push(pathname)
76+
window.___history.push(location)
7477
}
7578
}
7679

@@ -79,13 +82,13 @@ apiRunnerAsync(`onClientEntry`).then(() => {
7982
const timeoutId = setTimeout(() => {
8083
emitter.off(`onPostLoadPageResources`, eventHandler)
8184
emitter.emit(`onDelayedLoadPageResources`, { pathname })
82-
window.___history.push(pathname)
85+
window.___history.push(location)
8386
}, 1000)
8487

8588
if (loader.getResourcesForPathname(pathname)) {
8689
// The resources are already loaded so off we go.
8790
clearTimeout(timeoutId)
88-
window.___history.push(pathname)
91+
window.___history.push(location)
8992
} else {
9093
// They're not loaded yet so let's add a listener for when
9194
// they finish loading.

packages/gatsby/cache-dir/root.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ const addNotFoundRoute = () => {
132132
}
133133
}
134134

135-
const navigateTo = pathname => {
136-
window.___history.push(pathname)
135+
const navigateTo = to => {
136+
window.___history.push(to)
137137
}
138138

139139
window.___navigateTo = navigateTo

yarn.lock

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
# yarn lockfile v1
33

44

5+
"@types/history@*", "@types/history@^4.6.2":
6+
version "4.6.2"
7+
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.6.2.tgz#12cfaba693ba20f114ed5765467ff25fdf67ddb0"
8+
59
"@types/inline-style-prefixer@^3.0.0":
610
version "3.0.1"
711
resolved "https://registry.yarnpkg.com/@types/inline-style-prefixer/-/inline-style-prefixer-3.0.1.tgz#8541e636b029124b747952e9a28848286d2b5bf6"
@@ -14,6 +18,21 @@
1418
version "6.0.90"
1519
resolved "https://registry.yarnpkg.com/@types/node/-/node-6.0.90.tgz#0ed74833fa1b73dcdb9409dcb1c97ec0a8b13b02"
1620

21+
"@types/react-router-dom@^4.2.2":
22+
version "4.2.3"
23+
resolved "https://registry.yarnpkg.com/@types/react-router-dom/-/react-router-dom-4.2.3.tgz#06e0b67ff536adc0681dffdbe592ae91fb85887d"
24+
dependencies:
25+
"@types/history" "*"
26+
"@types/react" "*"
27+
"@types/react-router" "*"
28+
29+
"@types/react-router@*":
30+
version "4.0.21"
31+
resolved "https://registry.yarnpkg.com/@types/react-router/-/react-router-4.0.21.tgz#d6c7ea3b45dba02eb8f869629f3b7d7b6e9a7938"
32+
dependencies:
33+
"@types/history" "*"
34+
"@types/react" "*"
35+
1736
"@types/react@*":
1837
version "16.0.18"
1938
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.0.18.tgz#c08eea79ada55bf10b5353e18c21797dd17afa23"
@@ -11970,6 +11989,10 @@ shallow-clone@^0.1.2:
1197011989
lazy-cache "^0.2.3"
1197111990
mixin-object "^2.0.1"
1197211991

11992+
shallow-compare@^1.2.2:
11993+
version "1.2.2"
11994+
resolved "https://registry.yarnpkg.com/shallow-compare/-/shallow-compare-1.2.2.tgz#fa4794627bf455a47c4f56881d8a6132d581ffdb"
11995+
1197311996
shallow-equal@^1.0.0:
1197411997
version "1.0.0"
1197511998
resolved "https://registry.yarnpkg.com/shallow-equal/-/shallow-equal-1.0.0.tgz#508d1838b3de590ab8757b011b25e430900945f7"

0 commit comments

Comments
 (0)