feat(webapp): add installable PWA support
This commit is contained in:
@@ -1,15 +1,41 @@
|
||||
<script lang="ts">
|
||||
// @ts-nocheck
|
||||
import '../app.css';
|
||||
import { onMount } from 'svelte';
|
||||
import { registerSW } from 'virtual:pwa-register';
|
||||
import favicon from '$lib/assets/favicon.svg';
|
||||
import { appController } from '$lib/app/controller';
|
||||
import { clearInstallPrompt, setInstallPrompt, setOfflineReady, setUpdateAvailable } from '$lib/pwa';
|
||||
|
||||
let { children } = $props();
|
||||
|
||||
onMount(() => {
|
||||
registerSW({
|
||||
onNeedRefresh() {
|
||||
setUpdateAvailable(true);
|
||||
},
|
||||
onOfflineReady() {
|
||||
setOfflineReady(true);
|
||||
}
|
||||
});
|
||||
|
||||
const handleBeforeInstallPrompt = (event) => {
|
||||
event.preventDefault();
|
||||
setInstallPrompt(event);
|
||||
};
|
||||
|
||||
const handleAppInstalled = () => {
|
||||
clearInstallPrompt();
|
||||
};
|
||||
|
||||
window.addEventListener('beforeinstallprompt', handleBeforeInstallPrompt);
|
||||
window.addEventListener('appinstalled', handleAppInstalled);
|
||||
|
||||
void appController.init();
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('beforeinstallprompt', handleBeforeInstallPrompt);
|
||||
window.removeEventListener('appinstalled', handleAppInstalled);
|
||||
void appController.destroy();
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user