Skip to content

Commit f5b2c56

Browse files
trembym-allanson
authored andcommitted
gatsby-link: add push (same as navigateTo) and replace methods (#5934)
* gatsby-link: add push (same as navigateTo) and replace This addresses #5910 * Update README.md * Update example
1 parent 44c2d34 commit f5b2c56

File tree

6 files changed

+56
-21
lines changed

6 files changed

+56
-21
lines changed

packages/gatsby-link/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,20 @@ render () {
5353

5454
## Programmatic navigation
5555

56-
For cases when you can only use event handlers for navigation, you can use
57-
`navigateTo`:
56+
For cases when you can only use event handlers for navigation, you can use `push` or `replace`. `push` is a wrapper for `history.push` and `replace` wraps `history.replace`.
5857

5958
```jsx
60-
import { navigateTo } from "gatsby-link"
59+
import { push } from "gatsby-link"
6160

6261
render () {
63-
<div onClick={ () => navigateTo('/example')}>
62+
<div onClick={ () => push('/example')}>
6463
<p>Example</p>
6564
</div>
6665
}
6766
```
6867

68+
Note that `push` was previously named `navigateTo`. `navigateTo` will be deprecated in Gatsby v2.
69+
6970
## Prefixed paths helper
7071

7172
Gatsby allows you to [automatically prefix links](/docs/path-prefix/) for sites

packages/gatsby-link/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export interface GatsbyLinkProps extends NavLinkProps {
88
style?:any;
99
}
1010

11+
export const push: (to: LocationDescriptor) => void;
12+
export const replace: (to: LocationDescriptor) => void;
1113
export const navigateTo: (to: LocationDescriptor) => void;
1214

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

packages/gatsby-link/src/__tests__/index.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ const getInstance = (props, pathPrefix = ``) => {
1414
return new Link(props, context)
1515
}
1616

17-
const getNavigateTo = () => {
17+
const getPush = () => {
1818
Object.assign(global.window, {
19-
___navigateTo: jest.fn(),
19+
___push: jest.fn(),
2020
})
2121

22-
return require(`../`).navigateTo
22+
return require(`../`).push
23+
}
24+
25+
const getReplace = () => {
26+
Object.assign(global.window, {
27+
___replace: jest.fn(),
28+
})
29+
30+
return require(`../`).replace
2331
}
2432

2533
const getWithPrefix = (pathPrefix = ``) => {
@@ -135,10 +143,16 @@ describe(`<Link />`, () => {
135143
})
136144
})
137145

138-
it(`navigateTo is called with correct args`, () => {
139-
getNavigateTo()(`/some-path`)
146+
it(`push is called with correct args`, () => {
147+
getPush()(`/some-path`)
148+
149+
expect(global.window.___push).toHaveBeenCalledWith(`/some-path`)
150+
})
151+
152+
it(`replace is called with correct args`, () => {
153+
getReplace()(`/some-path`)
140154

141-
expect(global.window.___navigateTo).toHaveBeenCalledWith(`/some-path`)
155+
expect(global.window.___replace).toHaveBeenCalledWith(`/some-path`)
142156
})
143157
})
144158

@@ -151,7 +165,7 @@ describe(`withRouter`, () => {
151165
})
152166

153167
/*
154-
* Same as above, settings a path perfix does not work because the
168+
* Same as above, settings a path perfix does not work because the
155169
* link module sets variables on first import
156170
*/
157171
it.skip(`respects path prefix`, () => {

packages/gatsby-link/src/index.js

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

@@ -186,6 +186,12 @@ GatsbyLink.contextTypes = {
186186

187187
export default GatsbyLink
188188

189-
export const navigateTo = to => {
190-
window.___navigateTo(to)
189+
export const push = to => {
190+
window.___push(to)
191191
}
192+
193+
export const replace = to => {
194+
window.___replace(to)
195+
}
196+
197+
export const navigateTo = push

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ apiRunnerAsync(`onClientEntry`).then(() => {
5151
require(`./register-service-worker`)
5252
}
5353

54-
const navigateTo = to => {
54+
const navigate = (to, replace) => {
5555
const location = createLocation(to, null, null, history.location)
5656
let { pathname } = location
5757
const redirect = redirectMap[pathname]
@@ -72,13 +72,17 @@ apiRunnerAsync(`onClientEntry`).then(() => {
7272
return
7373
}
7474

75+
const historyNavigateFunc = replace
76+
? window.___history.replace
77+
: window.___history.push
78+
7579
// Listen to loading events. If page resources load before
7680
// a second, navigate immediately.
7781
function eventHandler(e) {
7882
if (e.page.path === loader.getPage(pathname).path) {
7983
emitter.off(`onPostLoadPageResources`, eventHandler)
8084
clearTimeout(timeoutId)
81-
window.___history.push(location)
85+
historyNavigateFunc(location)
8286
}
8387
}
8488

@@ -87,13 +91,13 @@ apiRunnerAsync(`onClientEntry`).then(() => {
8791
const timeoutId = setTimeout(() => {
8892
emitter.off(`onPostLoadPageResources`, eventHandler)
8993
emitter.emit(`onDelayedLoadPageResources`, { pathname })
90-
window.___history.push(location)
94+
historyNavigateFunc(location)
9195
}, 1000)
9296

9397
if (loader.getResourcesForPathname(pathname)) {
9498
// The resources are already loaded so off we go.
9599
clearTimeout(timeoutId)
96-
window.___history.push(location)
100+
historyNavigateFunc(location)
97101
} else {
98102
// They're not loaded yet so let's add a listener for when
99103
// they finish loading.
@@ -102,7 +106,9 @@ apiRunnerAsync(`onClientEntry`).then(() => {
102106
}
103107

104108
// window.___loadScriptsForPath = loadScriptsForPath
105-
window.___navigateTo = navigateTo
109+
window.___push = (to) => navigate(to, false)
110+
window.___replace = (to) => navigate(to, true)
111+
window.___navigateTo = window.___push
106112

107113
// Call onRouteUpdate on the initial page load.
108114
apiRunner(`onRouteUpdate`, {

packages/gatsby/cache-dir/root.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,17 @@ const addNotFoundRoute = () => {
134134
}
135135
}
136136

137-
const navigateTo = to => {
137+
const push = (to) => {
138138
window.___history.push(to)
139139
}
140140

141-
window.___navigateTo = navigateTo
141+
const replace = (to) => {
142+
window.___history.replace(to)
143+
}
144+
145+
window.___push = push
146+
window.___replace = replace
147+
window.___navigateTo = push
142148

143149
const AltRouter = apiRunner(`replaceRouterComponent`, { history })[0]
144150
const DefaultRouter = ({ children }) => (

0 commit comments

Comments
 (0)