Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setTimeout before calling onRouteUpdate to ensure React has written to DOM before plugins run code #3772

Merged
merged 1 commit into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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