Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #1419 service worker issue in production #1434

Merged
merged 1 commit into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions scripts/webpack/sw.js
Original file line number Diff line number Diff line change
@@ -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))
})
})
19 changes: 19 additions & 0 deletions scripts/webpack/webpack.base.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,6 +38,13 @@ const webpackConfig = {
filename: outputFileName,
},
plugins: [
new CopyPlugin([
{
from: serviceWorkerPath,
to: './sw.js',
force: true,
},
]),
new ResolveTSPathsToWebpackAlias({
tsconfig: PATHS.tsConfig,
}),
Expand Down