From 74940b601b919943967f4856654ada4a81fb0024 Mon Sep 17 00:00:00 2001 From: Bill Thornton Date: Thu, 3 Jun 2021 10:14:28 -0400 Subject: [PATCH] Move serviceworker precaching to production config --- src/serviceworker.js | 10 +++++++--- webpack.common.js | 5 ----- webpack.prod.js | 10 +++++++++- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/serviceworker.js b/src/serviceworker.js index 97357a8a5..770f5de5c 100644 --- a/src/serviceworker.js +++ b/src/serviceworker.js @@ -44,6 +44,10 @@ self.addEventListener('notificationclick', function (event) { event.waitUntil(executeAction(action, data, serverId)); }, false); -// this is needed by the webpack Workbox plugin -/* eslint-disable-next-line no-restricted-globals,no-undef */ -precacheAndRoute(self.__WB_MANIFEST); +// Do not precache files in development so live reload works as expected +/* eslint-disable-next-line no-undef -- NODE_ENV is replaced by webpack */ +if (process.env.NODE_ENV === 'production') { + // this is needed by the webpack Workbox plugin + /* eslint-disable-next-line no-restricted-globals,no-undef */ + precacheAndRoute(self.__WB_MANIFEST); +} diff --git a/webpack.common.js b/webpack.common.js index ba723767f..e04d368c9 100644 --- a/webpack.common.js +++ b/webpack.common.js @@ -1,7 +1,6 @@ const path = require('path'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); const CopyPlugin = require('copy-webpack-plugin'); -const WorkboxPlugin = require('workbox-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const Assets = [ @@ -72,10 +71,6 @@ module.exports = { to: path.resolve(__dirname, './dist/libraries/wasm-gen') }; }) - }), - new WorkboxPlugin.InjectManifest({ - swSrc: path.resolve(__dirname, 'src/serviceworker.js'), - swDest: 'serviceworker.js' }) ], output: { diff --git a/webpack.prod.js b/webpack.prod.js index 7d2dbc129..1f2366fae 100644 --- a/webpack.prod.js +++ b/webpack.prod.js @@ -1,7 +1,15 @@ +const path = require('path'); const common = require('./webpack.common'); const merge = require('webpack-merge'); +const WorkboxPlugin = require('workbox-webpack-plugin'); module.exports = merge(common, { mode: 'production', - entry: './scripts/site.js' + entry: './scripts/site.js', + plugins: [ + new WorkboxPlugin.InjectManifest({ + swSrc: path.resolve(__dirname, 'src/serviceworker.js'), + swDest: 'serviceworker.js' + }) + ] });