From 17f028d7085e1e4dc7bd361134bf0230b2cb7d95 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Thu, 22 Apr 2021 14:19:33 +0200 Subject: [PATCH] fix(gatsby,gatsby-plugin-offline): register offline plugin when not on preview (#30984) --- packages/gatsby-plugin-offline/src/gatsby-browser.js | 6 +++++- packages/gatsby/cache-dir/production-app.js | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/gatsby-plugin-offline/src/gatsby-browser.js b/packages/gatsby-plugin-offline/src/gatsby-browser.js index 0f0ca2b65a3ba..8285b08d6bcb2 100644 --- a/packages/gatsby-plugin-offline/src/gatsby-browser.js +++ b/packages/gatsby-plugin-offline/src/gatsby-browser.js @@ -1,4 +1,4 @@ -exports.registerServiceWorker = () => true +exports.registerServiceWorker = () => process.env.GATSBY_IS_PREVIEW !== `true` // only cache relevant resources for this page const whiteListLinkRels = /^(stylesheet|preload)$/ @@ -8,6 +8,10 @@ exports.onServiceWorkerActive = ({ getResourceURLsForPathname, serviceWorker, }) => { + if (process.env.GATSBY_IS_PREVIEW === `true`) { + return + } + // if the SW has just updated then clear the path dependencies and don't cache // stuff, since we're on the old revision until we navigate to another page if (window.___swUpdated) { diff --git a/packages/gatsby/cache-dir/production-app.js b/packages/gatsby/cache-dir/production-app.js index baf20edc9746b..71da23e767da9 100644 --- a/packages/gatsby/cache-dir/production-app.js +++ b/packages/gatsby/cache-dir/production-app.js @@ -39,7 +39,7 @@ navigationInit() apiRunnerAsync(`onClientEntry`).then(() => { // Let plugins register a service worker. The plugin just needs // to return true. - if (apiRunner(`registerServiceWorker`).length > 0) { + if (apiRunner(`registerServiceWorker`).filter(Boolean).length > 0) { require(`./register-service-worker`) }