From 4436d7418555f4db7c73ed276b5c051d4e207e68 Mon Sep 17 00:00:00 2001 From: undefined Date: Fri, 29 May 2020 14:56:31 +0700 Subject: [PATCH] fix: #1419 service worker issue in production --- scripts/webpack/sw.js | 15 +++++++++++++++ scripts/webpack/webpack.base.prod.js | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 scripts/webpack/sw.js diff --git a/scripts/webpack/sw.js b/scripts/webpack/sw.js new file mode 100644 index 0000000000..5ca2706a05 --- /dev/null +++ b/scripts/webpack/sw.js @@ -0,0 +1,15 @@ +// https://github.com/NekR/self-destroying-sw +self.addEventListener('install', function() { + self.skipWaiting() +}) + +self.addEventListener('activate', function() { + self.registration + .unregister() + .then(function() { + return self.clients.matchAll() + }) + .then(function(clients) { + clients.forEach(client => client.navigate(client.url)) + }) +}) diff --git a/scripts/webpack/webpack.base.prod.js b/scripts/webpack/webpack.base.prod.js index 32c19245ee..33d31ba4ec 100644 --- a/scripts/webpack/webpack.base.prod.js +++ b/scripts/webpack/webpack.base.prod.js @@ -16,6 +16,18 @@ const hashOfCommit = getRef() const APP_VERSION = `${tagName.packageName}_${tagName.version}` const outputFileName = `[name].${hashOfCommit}.js` +/** + * https://medium.com/@nekrtemplar/self-destroying-serviceworker-73d62921d717 + * https://github.com/reapit/foundations/issues/1419 + * using this method: https://github.com/NekR/self-destroying-sw + * To clean up service worker for users who has installed it + * + * TODO: + * remove after users stop experiencing errors which relevant to service worker like this: + * https://sentry.io/organizations/reapit-ltd/issues/1692085300/?project=1885603&referrer=slack + */ +const serviceWorkerPath = path.resolve(__dirname, './sw.js') + const webpackConfig = { mode: 'production', bail: true, @@ -26,6 +38,13 @@ const webpackConfig = { filename: outputFileName, }, plugins: [ + new CopyPlugin([ + { + from: serviceWorkerPath, + to: './sw.js', + force: true, + }, + ]), new ResolveTSPathsToWebpackAlias({ tsconfig: PATHS.tsConfig, }),