Skip to content

Commit

Permalink
Add setTimeout before calling onRouteUpdate to ensure React has writt…
Browse files Browse the repository at this point in the history
…en to DOM before plugins run code (#3772)
  • Loading branch information
KyleAMathews committed Jan 30, 2018
1 parent a89057f commit d025ae1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
7 changes: 2 additions & 5 deletions packages/gatsby-plugin-google-analytics/src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
exports.onRouteUpdate = function({ location }) {
// Don't track while developing.
if (process.env.NODE_ENV === `production` && typeof ga === `function`) {
// Wait for the title update (see #2478)
setTimeout(() => {
window.ga(`set`, `page`, (location || {}).pathname)
window.ga(`send`, `pageview`)
}, 0)
window.ga(`set`, `page`, (location || {}).pathname)
window.ga(`send`, `pageview`)
}
}
17 changes: 7 additions & 10 deletions packages/gatsby-plugin-twitter/src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
exports.onRouteUpdate = function({ location }) {
// Wait to ensure page is rendered first.
setTimeout(() => {
if (
typeof twttr !== `undefined` &&
window.twttr.widgets &&
typeof window.twttr.widgets.load === `function`
) {
window.twttr.widgets.load()
}
}, 0)
if (
typeof twttr !== `undefined` &&
window.twttr.widgets &&
typeof window.twttr.widgets.load === `function`
) {
window.twttr.widgets.load()
}
}
5 changes: 4 additions & 1 deletion packages/gatsby/cache-dir/production-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ apiRunnerAsync(`onClientEntry`).then(() => {

history.listen((location, action) => {
if (!maybeRedirect(location.pathname)) {
apiRunner(`onRouteUpdate`, { location, action })
// Make sure React has had a chance to flush to DOM first.
setTimeout(() => {
apiRunner(`onRouteUpdate`, { location, action })
}, 0)
}
})
}
Expand Down

0 comments on commit d025ae1

Please sign in to comment.