forked from ACManoa/acmanoa.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
39 lines (35 loc) · 1.15 KB
/
sw.js
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
35
36
37
38
39
self.addEventListener('install', event => {
event.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', event => {
event.waitUntil(
unregisterAllServiceWorkers()
.then(() => self.registration.unregister())
.then(() => self.clients.claim())
);
});
function unregisterAllServiceWorkers() {
const currentUrl = new URL(self.registration.scope);
const currentScope = currentUrl.origin + currentUrl.pathname;
return self.registration.unregister().then(() => {
return self.clients.matchAll();
}).then(clients => {
clients.forEach(client => {
const clientUrl = new URL(client.url);
const clientScope = clientUrl.origin + clientUrl.pathname;
if (clientScope === currentScope) {
client.navigate(client.url);
}
});
});
}
// Register itself
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js')
.then(registration => {
console.log('Service Worker registered:', registration);
})
.catch(error => {
console.error('Service Worker registration failed:', error);
});
}