Add toast dinamically
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
Alessio Davoli 2023-07-22 19:58:19 +02:00
parent 7d8f59dec7
commit 10fa4522ba

View file

@ -1,25 +1,30 @@
<script> <script>
import BottomAppBar from '$lib/components/BottomAppBar.svelte'; import BottomAppBar from '$lib/components/BottomAppBar.svelte';
import Toast from '$lib/components/Toast.svelte';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
let node;
onMount(() => { onMount(() => {
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
addEventListener('load', function () { addEventListener('load', function () {
navigator.serviceWorker.register('/service-worker.js'); navigator.serviceWorker.register('/service-worker.js');
}); });
navigator.serviceWorker.addEventListener('message', (event) => { const channel = new BroadcastChannel('sw-messages');
console.log(`The service worker sent me a message: ${event.data}`); channel.addEventListener('message', (event) => {
alert('You can use the app offline'); new Toast({ target: node });
}); });
navigator.serviceWorker.ready.then((registration) => { navigator.serviceWorker.ready.then((registration) => {
console.log(registration);
console.log('registered service worker'); console.log('registered service worker');
}); });
} }
}); });
</script> </script>
<div bind:this={node}></div>
<slot /> <slot />
<footer> <footer>
<BottomAppBar /> <BottomAppBar />