diff --git a/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js b/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js index 9edd7a816200a..799de9fe8f0ec 100644 --- a/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js +++ b/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js @@ -18,31 +18,43 @@ exports.onRenderBody = ( { setHeadComponents, setPostBodyComponents }, pluginOptions ) => { - if (process.env.NODE_ENV === `production`) { - let excludeGAPaths = [] - if (typeof pluginOptions.exclude !== `undefined`) { - const Minimatch = require(`minimatch`).Minimatch - pluginOptions.exclude.map(exclude => { - const mm = new Minimatch(exclude) - excludeGAPaths.push(mm.makeRe()) - }) - } + if (process.env.NODE_ENV !== `production`) { + return null + } - const gaCreateOptions = {} - for (const option in knownOptions) { - if (typeof pluginOptions[option] === knownOptions[option]) { - gaCreateOptions[option] = pluginOptions[option] - } + // Lighthouse recommends pre-connecting to google analytics + setHeadComponents([ + , + ]) + + let excludeGAPaths = [] + if (typeof pluginOptions.exclude !== `undefined`) { + const Minimatch = require(`minimatch`).Minimatch + pluginOptions.exclude.map(exclude => { + const mm = new Minimatch(exclude) + excludeGAPaths.push(mm.makeRe()) + }) + } + + const gaCreateOptions = {} + for (const option in knownOptions) { + if (typeof pluginOptions[option] === knownOptions[option]) { + gaCreateOptions[option] = pluginOptions[option] } + } - const setComponents = pluginOptions.head - ? setHeadComponents - : setPostBodyComponents - return setComponents([ -