fix(webapp): use direct service worker registration
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
// @ts-nocheck
|
// @ts-nocheck
|
||||||
import '../app.css';
|
import '../app.css';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { registerSW } from 'virtual:pwa-register';
|
|
||||||
import favicon from '$lib/assets/favicon.svg';
|
import favicon from '$lib/assets/favicon.svg';
|
||||||
import { appController } from '$lib/app/controller';
|
import { appController } from '$lib/app/controller';
|
||||||
import {
|
import {
|
||||||
@@ -17,14 +16,47 @@
|
|||||||
let { children } = $props();
|
let { children } = $props();
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
registerSW({
|
const registerServiceWorker = async () => {
|
||||||
onNeedRefresh() {
|
if (!import.meta.env.PROD || !('serviceWorker' in navigator)) {
|
||||||
setUpdateAvailable(true);
|
return;
|
||||||
},
|
|
||||||
onOfflineReady() {
|
|
||||||
setOfflineReady(true);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
try {
|
||||||
|
const registration = await navigator.serviceWorker.register('/sw.js');
|
||||||
|
|
||||||
|
const handleInstallingWorker = (worker) => {
|
||||||
|
if (!worker) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
worker.addEventListener('statechange', () => {
|
||||||
|
if (worker.state !== 'installed') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (navigator.serviceWorker.controller) {
|
||||||
|
setUpdateAvailable(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setOfflineReady(true);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
if (registration.waiting) {
|
||||||
|
setUpdateAvailable(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
handleInstallingWorker(registration.installing);
|
||||||
|
registration.addEventListener('updatefound', () => {
|
||||||
|
handleInstallingWorker(registration.installing);
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Service worker registration failed', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void registerServiceWorker();
|
||||||
|
|
||||||
const handleBeforeInstallPrompt = (event) => {
|
const handleBeforeInstallPrompt = (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ export default defineConfig({
|
|||||||
tailwindcss(),
|
tailwindcss(),
|
||||||
sveltekit(),
|
sveltekit(),
|
||||||
VitePWA({
|
VitePWA({
|
||||||
|
injectRegister: false,
|
||||||
registerType: 'autoUpdate',
|
registerType: 'autoUpdate',
|
||||||
includeAssets: ['icons/apple-touch-icon.png', 'icons/pwa-192x192.png', 'icons/pwa-512x512.png'],
|
includeAssets: ['icons/apple-touch-icon.png', 'icons/pwa-192x192.png', 'icons/pwa-512x512.png'],
|
||||||
manifest: {
|
manifest: {
|
||||||
@@ -73,7 +74,7 @@ export default defineConfig({
|
|||||||
navigateFallbackDenylist: [/^\/api\//, /^\/devices/, /^\/device-links/, /^\/streams/, /^\/events/, /^\/recordings/, /^\/videos/, /^\/push-notifications/, /^\/socket\.io\//]
|
navigateFallbackDenylist: [/^\/api\//, /^\/devices/, /^\/device-links/, /^\/streams/, /^\/events/, /^\/recordings/, /^\/videos/, /^\/push-notifications/, /^\/socket\.io\//]
|
||||||
},
|
},
|
||||||
devOptions: {
|
devOptions: {
|
||||||
enabled: true
|
enabled: false
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user