Fixed issue where service-worker installing might crash the whole site

This commit is contained in:
Kasper Rynning-Tønnesen
2020-02-19 16:50:37 +01:00
parent 4287059361
commit bcf8a3de63
2 changed files with 96 additions and 16 deletions

View File

@@ -6,24 +6,60 @@ var STATIC_CACHE_URLS = ["/"];
console.log("Nåværende versjon:", version);
self.addEventListener("activate", event => {
event.waitUntil(
new Promise(resolve => {
const applicationServerKey = urlB64ToUint8Array(__PUBLICKEY__);
const options = { applicationServerKey, userVisibleOnly: true };
self.registration.pushManager.subscribe(options).then(subscription =>
saveSubscription(subscription)
.then(() => {
resolve();
})
.catch(() => {
resolve();
})
);
})
);
console.log("Aktiverer");
event.waitUntil(removeCache(CACHE_NAME));
event.waitUntil(removeCache(CACHE_NAME_API));
event.waitUntil(addCache(CACHE_NAME, STATIC_CACHE_URLS));
event.waitUntil(
new Promise((resolve, reject) => {
const applicationServerKey = urlB64ToUint8Array(__PUBLICKEY__);
const options = { applicationServerKey, userVisibleOnly: true };
self.registration.pushManager
.subscribe(options)
.then(subscription =>
saveSubscription(subscription)
.then(() => {
resolve();
})
.catch(() => {
resolve();
})
)
.catch(() => {
console.log("Kunne ikke legge til pushnotifications");
resolve();
});
})
);
});
self.addEventListener("message", event => {
if (event.data === "updatePush") {
event.waitUntil(
new Promise((resolve, reject) => {
const applicationServerKey = urlB64ToUint8Array(__PUBLICKEY__);
const options = { applicationServerKey, userVisibleOnly: true };
self.registration.pushManager
.subscribe(options)
.then(subscription =>
saveSubscription(subscription)
.then(() => {
const channel = new BroadcastChannel("updatePush");
channel.postMessage({ success: true });
resolve();
})
.catch(() => {
resolve();
})
)
.catch(() => {
console.log("Kunne ikke legge til pushnotifications");
reject();
});
})
);
}
});
self.addEventListener("push", function(event) {