-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
34 lines (33 loc) · 1.05 KB
/
Copy pathsw.js
File metadata and controls
34 lines (33 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* SERVICE WORKER KILL-SWITCH
* Unregisters any hijacked/malicious SW, clears all caches, and reloads clients.
* Deploy this at /sw.js to evict any previously registered bad service worker.
*/
self.addEventListener('install', function(e) {
// Skip waiting so this SW activates immediately, beating any existing one
self.skipWaiting();
});
self.addEventListener('activate', function(e) {
e.waitUntil(
// 1. Wipe every cache this origin owns
caches.keys()
.then(function(cacheNames) {
return Promise.all(cacheNames.map(function(name) {
return caches.delete(name);
}));
})
.then(function() {
// 2. Unregister this SW itself so no SW runs at all
return self.registration.unregister();
})
.then(function() {
// 3. Force all open tabs to reload with a clean slate
return self.clients.matchAll({ type: 'window' });
})
.then(function(clients) {
clients.forEach(function(client) {
client.navigate(client.url);
});
})
);
});