Skip to content

Commit 94d7f6f

Browse files
committed
Don't call route APIs on the first update in dev
1 parent aee5ab9 commit 94d7f6f

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

e2e-tests/development-runtime/cypress/integration/functionality/hooks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const COUNT_ID = `count`
22

33
describe(`hooks`, () => {
44
beforeEach(() => {
5-
cy.visit(`/hooks`).waitForRouteChange()
5+
cy.visit(`/hooks`, { failOnStatusCode: false }).waitForRouteChange()
66
})
77

88
it(`displays initial state`, () => {

e2e-tests/development-runtime/cypress/integration/navigation/linking.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,11 @@ describe(`navigation`, () => {
8888
cy.get(`h1`).invoke(`text`).should(`eq`, `Gatsby.js development 404 page`)
8989

9090
/*
91-
* Three route updates:
91+
* Two route updates:
9292
* - initial render of /
93-
* - render overlays
9493
* - (default) development 404 page
9594
*/
96-
cy.lifecycleCallCount(`onRouteUpdate`).should(`eq`, 3)
95+
cy.lifecycleCallCount(`onRouteUpdate`).should(`eq`, 2)
9796
})
9897

9998
it(`can display a custom 404 page`, () => {
@@ -102,13 +101,12 @@ describe(`navigation`, () => {
102101
cy.getTestElement(`page-title`).invoke(`text`).should(`eq`, `NOT FOUND`)
103102

104103
/*
105-
* Three route updates:
104+
* Two route updates:
106105
* - initial render
107-
* - render overlays
108106
* - 404 page
109107
* a re-render does not occur because the route remains the same
110108
*/
111-
cy.lifecycleCallCount(`onRouteUpdate`).should(`eq`, 3)
109+
cy.lifecycleCallCount(`onRouteUpdate`).should(`eq`, 2)
112110
})
113111
})
114112

@@ -138,7 +136,7 @@ describe(`navigation`, () => {
138136
})
139137

140138
it(`should show 404 page when url with unicode characters point to a non-existent page route when navigating on client`, () => {
141-
cy.visit(`/`).waitForRouteChange()
139+
cy.visit(`/`, { failOnStatusCode: false }).waitForRouteChange()
142140
cy.window()
143141
.then(win => win.___navigate(`/안녕404/`))
144142
.waitForRouteChange()
@@ -220,21 +218,21 @@ describe(`navigation`, () => {
220218

221219
describe(`Route lifecycle update order`, () => {
222220
it(`calls onPreRouteUpdate, render and onRouteUpdate the correct amount of times on route change`, () => {
223-
cy.lifecycleCallCount(`onPreRouteUpdate`).should(`eq`, 2)
221+
cy.lifecycleCallCount(`onPreRouteUpdate`).should(`eq`, 1)
224222
cy.lifecycleCallCount(`render`).should(`eq`, 2)
225-
cy.lifecycleCallCount(`onRouteUpdate`).should(`eq`, 2)
223+
cy.lifecycleCallCount(`onRouteUpdate`).should(`eq`, 1)
226224
cy.getTestElement(`page-two`).click().waitForRouteChange()
227225
cy.getTestElement(`page-2-message`).should(`exist`)
228-
cy.lifecycleCallCount(`onPreRouteUpdate`).should(`eq`, 3)
226+
cy.lifecycleCallCount(`onPreRouteUpdate`).should(`eq`, 2)
229227
cy.lifecycleCallCount(`render`).should(`eq`, 3)
230-
cy.lifecycleCallCount(`onRouteUpdate`).should(`eq`, 3)
228+
cy.lifecycleCallCount(`onRouteUpdate`).should(`eq`, 2)
231229
})
232230

233231
it(`renders the component after onPreRouteUpdate on route change`, () => {
234232
cy.getTestElement(`page-component`).should(`exist`)
235-
cy.lifecycleCallCount(`onPreRouteUpdate`).should(`eq`, 2)
233+
cy.lifecycleCallCount(`onPreRouteUpdate`).should(`eq`, 1)
236234
cy.lifecycleCallCount(`render`).should(`eq`, 2)
237-
cy.lifecycleCallCount(`onRouteUpdate`).should(`eq`, 2)
235+
cy.lifecycleCallCount(`onRouteUpdate`).should(`eq`, 1)
238236
cy.lifecycleCallOrder([
239237
`onPreRouteUpdate`,
240238
`render`,
@@ -250,9 +248,9 @@ describe(`navigation`, () => {
250248
`render`,
251249
`onRouteUpdate`,
252250
]).should(`eq`, true)
253-
cy.lifecycleCallCount(`onPreRouteUpdate`).should(`eq`, 3)
251+
cy.lifecycleCallCount(`onPreRouteUpdate`).should(`eq`, 2)
254252
cy.lifecycleCallCount(`render`).should(`eq`, 3)
255-
cy.lifecycleCallCount(`onRouteUpdate`).should(`eq`, 3)
253+
cy.lifecycleCallCount(`onRouteUpdate`).should(`eq`, 2)
256254
})
257255
})
258256
})

packages/gatsby/cache-dir/navigation.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,25 @@ const compareLocationProps = (prevLocation, nextLocation) => {
217217
}
218218

219219
// Fire on(Pre)RouteUpdate APIs
220+
// We ignore the first update in development as we have do a double render
221+
// to account for rendering first the body component that hydrates the SSRed HTML
222+
// and then a second render to add our dev overlays. We don't want to fire
223+
// route APIs for the first render.
224+
let isNotFirstUpdate = false
220225
class RouteUpdates extends React.Component {
221226
constructor(props) {
222227
super(props)
223-
onPreRouteUpdate(props.location, null)
228+
if (process.env.NODE_ENV === `development` && isNotFirstUpdate) {
229+
onPreRouteUpdate(props.location, null)
230+
}
224231
}
225232

226233
componentDidMount() {
227-
onRouteUpdate(this.props.location, null)
234+
if (process.env.NODE_ENV === `development` && isNotFirstUpdate) {
235+
onRouteUpdate(this.props.location, null)
236+
} else {
237+
isNotFirstUpdate = true
238+
}
228239
}
229240

230241
shouldComponentUpdate(prevProps) {

0 commit comments

Comments
 (0)