forked from coderplex-org/coderplex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffline-installer.js
32 lines (31 loc) · 1002 Bytes
/
offline-installer.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
if (typeof window !== 'undefined' && 'serviceWorker' in navigator) {
navigator.serviceWorker
.register('/sw.js', {
scope: './',
})
.then(reg => {
console.log('sw rules here');
reg.onupdatefound = function() {
const installingWorker = reg.installing;
installingWorker.onstatechange = function() {
switch (installingWorker.state) {
case 'installed':
if (navigator.serviceWorker.controller) {
console.log('New or updated content is available.');
} else {
console.log('Content is now available offline!');
}
break;
case 'redundant':
console.log('The installing serviceWorker became redundant.');
break;
default:
console.log('default case');
}
};
};
})
.catch(err => {
console.error('Error during service worker registration:', err);
});
}