From d025ae117b9b323ca1f12891aa713b27b724bd1b Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Mon, 29 Jan 2018 16:52:25 -0800 Subject: [PATCH] Add setTimeout before calling onRouteUpdate to ensure React has written to DOM before plugins run code (#3772) --- .../src/gatsby-browser.js | 7 ++----- .../gatsby-plugin-twitter/src/gatsby-browser.js | 17 +++++++---------- packages/gatsby/cache-dir/production-app.js | 5 ++++- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/packages/gatsby-plugin-google-analytics/src/gatsby-browser.js b/packages/gatsby-plugin-google-analytics/src/gatsby-browser.js index 42e22f0e35c51..f7e5891cd0daa 100644 --- a/packages/gatsby-plugin-google-analytics/src/gatsby-browser.js +++ b/packages/gatsby-plugin-google-analytics/src/gatsby-browser.js @@ -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`) } } diff --git a/packages/gatsby-plugin-twitter/src/gatsby-browser.js b/packages/gatsby-plugin-twitter/src/gatsby-browser.js index 2a7e4e86e149f..88ac2acca4ff2 100644 --- a/packages/gatsby-plugin-twitter/src/gatsby-browser.js +++ b/packages/gatsby-plugin-twitter/src/gatsby-browser.js @@ -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() + } } diff --git a/packages/gatsby/cache-dir/production-app.js b/packages/gatsby/cache-dir/production-app.js index 613d52eeb4875..8cb9a4622edeb 100644 --- a/packages/gatsby/cache-dir/production-app.js +++ b/packages/gatsby/cache-dir/production-app.js @@ -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) } }) }