Broadcast channel

This commit is contained in:
Alessio Davoli 2023-07-24 01:46:50 +02:00
parent bec8fcdce1
commit e4829976e4

View file

@ -4,21 +4,28 @@
import { onMount } from 'svelte';
let node;
onMount(() => {
if ('serviceWorker' in navigator) {
addEventListener('load', function () {
navigator.serviceWorker.register('/service-worker.js');
});
const channel = new BroadcastChannel('sw-messages');
channel.addEventListener('message', (event) => {
new Toast({ target: node });
});
if (BroadcastChannel) {
const channel = new BroadcastChannel('sw-messages');
channel.addEventListener('message', (event) => {
console.log(event.data);
new Toast({ target: node });
});
} else {
navigator.serviceWorker.addEventListener('message', (evt) => {
console.log(evt.data);
new Toast({ target: node });
});
}
navigator.serviceWorker.ready.then((registration) => {
console.log(registration);
console.log('registered service worker');
registration.active?.postMessage('Save client');
});
}
});