Skip to content

Commit

Permalink
[gatsby] Pass location state to history (#3802)
Browse files Browse the repository at this point in the history
* Pass full location object to history

* Update navigateTo param to be more precise

* Pass location to navigateTo in gatsby-link

* Update navigateTo types
  • Loading branch information
nsimonson authored and KyleAMathews committed Feb 1, 2018
1 parent 1106bbb commit 869bb2c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/gatsby-link/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as React from "react";
import { NavLinkProps } from "react-router-dom";
import { LocationDescriptor } from "history";

export interface GatsbyLinkProps extends NavLinkProps {
onClick?: (event: any) => void
className?: string
style?:any;
}

export const navigateTo: (path: string) => void;
export const navigateTo: (to: LocationDescriptor) => void;

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

Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-link/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby-link/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -184,6 +184,6 @@ GatsbyLink.contextTypes = {

export default GatsbyLink

export const navigateTo = pathname => {
window.___navigateTo(pathname)
export const navigateTo = to => {
window.___navigateTo(to)
}
11 changes: 7 additions & 4 deletions packages/gatsby/cache-dir/production-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}

Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/cache-dir/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ const addNotFoundRoute = () => {
}
}

const navigateTo = pathname => {
window.___history.push(pathname)
const navigateTo = to => {
window.___history.push(to)
}

window.___navigateTo = navigateTo
Expand Down
23 changes: 23 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 869bb2c

Please sign in to comment.