From 4ce0a41232deb9d433cfb9dbe3df0831431b881c Mon Sep 17 00:00:00 2001 From: Aaron Boodman Date: Mon, 25 Jan 2021 00:56:11 -1000 Subject: [PATCH] Introduce an `enableOffline` configuration flag that can be used to force offline mode to work. Defaults to false in dev mode, true in production. Addresses #143 --- index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index af3f53e4..659101ed 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,7 @@ module.exports = (nextConfig = {}) => ({ // https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-webpack-plugin.GenerateSW const { disable = false, + enableOffline = !dev, register = true, dest = distDir, sw = 'sw.js', @@ -82,7 +83,7 @@ module.exports = (nextConfig = {}) => ({ }) if (!options.isServer) { - if (dev) { + if (!enableOffline) { console.log( '> [PWA] Build in develop mode, cache and precache are mostly disabled. This means offline support is disabled, but you can continue developing other functions in service worker.' ) @@ -188,13 +189,13 @@ module.exports = (nextConfig = {}) => ({ const prefix = config.output.publicPath ? `${config.output.publicPath}static/` : 'static/' const workboxCommon = { swDest: path.join(_dest, sw), - additionalManifestEntries: dev ? [] : manifestEntries, + additionalManifestEntries: !enableOffline ? [] : manifestEntries, exclude: [ ({ asset, compilation }) => { if (asset.name.match(/^(build-manifest\.json|react-loadable-manifest\.json)$/)) { return true } - if (dev && !asset.name.startsWith('static/runtime/')) { + if (!enableOffline && !asset.name.startsWith('static/runtime/')) { return true } if (experimental.modern /* modern */) { @@ -237,7 +238,7 @@ module.exports = (nextConfig = {}) => ({ }) ) } else { - if (dev) { + if (!enableOffline) { ignoreURLParametersMatching.push(/ts/) } @@ -250,7 +251,7 @@ module.exports = (nextConfig = {}) => ({ ignoreURLParametersMatching, importScripts, ...workbox, - runtimeCaching: dev + runtimeCaching: !enableOffline ? [ { urlPattern: /.*/i,