-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
32 lines (29 loc) · 895 Bytes
/
main.js
File metadata and controls
32 lines (29 loc) · 895 Bytes
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
const registerServiceWorker = async () => {
if ("serviceWorker" in navigator) {
try {
const registration = await navigator.serviceWorker.register("/sw.js", {
scope: "/",
});
if (registration.installing) {
console.info("Service worker installing.");
} else if (registration.waiting) {
console.log("Service worker installed.");
} else if (registration.active) {
console.info("Service worker active.");
}
} catch (error) {
console.error(`Registration failed with ${error}.`);
}
}
};
const unregisterServiceWorkers = async () => {
workers = await navigator.serviceWorker.getRegistrations();
workers.forEach( async worker => {
await worker.unregister();
})
}
const updateServiceWorker = async () => {
await unregisterServiceWorkers();
await registerServiceWorker();
}
registerServiceWorker();