Skip to content

Commit a102b27

Browse files
naveedahmed1AndrewKushnir
authored andcommittedSep 13, 2021
fix(service-worker): clear service worker cache in safety worker (#43324)
clear angular service worker cache in safety worker to ensure stale or broken contents are not served in future requests Fixes #43163 PR Close #43324
1 parent baf8145 commit a102b27

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed
 

‎aio/content/guide/service-worker-devops.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,9 @@ essentially self-destructing.
326326

327327
Also included in the `@angular/service-worker` NPM package is a small
328328
script `safety-worker.js`, which when loaded will unregister itself
329-
from the browser. This script can be used as a last resort to get rid
330-
of unwanted service workers already installed on client pages.
329+
from the browser and remove the service worker caches. This script can
330+
be used as a last resort to get rid of unwanted service workers already
331+
installed on client pages.
331332

332333
It's important to note that you cannot register this worker directly,
333334
as old clients with cached state may not see a new `index.html` which
@@ -339,8 +340,8 @@ most sites, this means that you should serve the safety worker at the
339340
old Service Worker URL forever.
340341

341342
This script can be used both to deactivate `@angular/service-worker`
342-
as well as any other Service Workers which might have been served in
343-
the past on your site.
343+
(and remove the corresponding caches) as well as any other Service
344+
Workers which might have been served in the past on your site.
344345

345346
### Changing your app's location
346347

‎packages/service-worker/safety-worker.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ self.addEventListener('install', event => {
1414

1515
self.addEventListener('activate', event => {
1616
event.waitUntil(self.clients.claim());
17-
self.registration.unregister().then(() => {
17+
18+
event.waitUntil(self.registration.unregister().then(() => {
1819
console.log('NGSW Safety Worker - unregistered old service worker');
19-
});
20+
}));
21+
22+
event.waitUntil(caches.keys().then(cacheNames => {
23+
const ngswCacheNames = cacheNames.filter(name => /^ngsw:/.test(name));
24+
return Promise.all(ngswCacheNames.map(name => caches.delete(name)));
25+
}));
2026
});

0 commit comments

Comments
 (0)
Please sign in to comment.