From b10540a0b5b2af3f5b9d8ba3dae8267eac3dc83f Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Mon, 29 Jan 2018 09:23:31 -0800 Subject: [PATCH] feat(service-worker): add helper script which will uninstall SW (#21863) Service Workers can be tricky to work with in production, and often it becomes necessary to deactivate an existing SW. This is trickier than one might imagine - as long as clients on the old SW may exist it is important to serve some script at the old SW URL. This commit adds safety-worker.js to the published NPM package, which is useful for that purpose. On install the SW unregisters itself which safely and gradually allows older clients to update. PR Close #21863 --- packages/service-worker/build.sh | 2 ++ packages/service-worker/safety-worker.js | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 packages/service-worker/safety-worker.js diff --git a/packages/service-worker/build.sh b/packages/service-worker/build.sh index 1ad458d288..3bdff8974c 100755 --- a/packages/service-worker/build.sh +++ b/packages/service-worker/build.sh @@ -11,6 +11,8 @@ $BIN/rollup -c worker/rollup-worker.config.js $BIN/tsc -p cli/tsconfig.json $BIN/rollup -c cli/rollup-cli.config.js +cp ./safety-worker.js ../../dist/packages-dist/service-worker/safety-worker.js + echo "#!/usr/bin/env node" > ../../dist/packages-dist/service-worker/ngsw-config.js cat ../../dist/packages-dist/service-worker/ngsw-config-tmp.js >> ../../dist/packages-dist/service-worker/ngsw-config.js diff --git a/packages/service-worker/safety-worker.js b/packages/service-worker/safety-worker.js new file mode 100644 index 0000000000..6301f64fcf --- /dev/null +++ b/packages/service-worker/safety-worker.js @@ -0,0 +1,17 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +// tslint:disable:no-console + +self.addEventListener('install', event => { self.skipWaiting(); }); + +self.addEventListener('activate', event => { + event.waitUntil(self.clients.claim()); + self.registration.unregister().then( + () => { console.log('NGSW Safety Worker - unregistered old service worker'); }); +});