Add support for postMessage
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
Alessio Davoli 2023-07-24 01:47:46 +02:00
parent e4829976e4
commit b2347b0466
2 changed files with 15 additions and 3 deletions

View file

@ -25,7 +25,7 @@
} }
navigator.serviceWorker.ready.then((registration) => { navigator.serviceWorker.ready.then((registration) => {
registration.active?.postMessage('Save client'); registration.active.postMessage('Save client');
}); });
} }
}); });

View file

@ -14,6 +14,12 @@ const ASSETS = [
...prerendered // dynamic routes ...prerendered // dynamic routes
]; ];
let client;
addEventListener('message', event => {
client = event.source;
});
self.addEventListener('install', (event) => { self.addEventListener('install', (event) => {
// Create a new cache and add all files to it // Create a new cache and add all files to it
async function addFilesToCache() { async function addFilesToCache() {
@ -24,7 +30,7 @@ self.addEventListener('install', (event) => {
event.waitUntil(addFilesToCache()); event.waitUntil(addFilesToCache());
}); });
const channel = new BroadcastChannel('sw-messages');
self.addEventListener('activate', (event) => { self.addEventListener('activate', (event) => {
@ -36,7 +42,13 @@ self.addEventListener('activate', (event) => {
} }
event.waitUntil(deleteOldCaches()); event.waitUntil(deleteOldCaches());
channel.postMessage({title: 'Cache downloaded!'}); let channel;
if(BroadcastChannel) {
channel = new BroadcastChannel('sw-messages');
channel.postMessage({title: 'Cache Downloaded'});
} else {
client.postMessage("Cache Downloaded")
}
}); });
self.addEventListener('fetch', (event) => { self.addEventListener('fetch', (event) => {