From 3209f8b4e0ffc12434ae9a3ad5e52e84cf60121b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thea=20Sch=C3=B6bl?= Date: Wed, 5 Jul 2023 21:27:41 +0200 Subject: [PATCH] add pwa functionality --- src/lib/components/Navigation.svelte | 3 ++ src/lib/components/PwaStatus.svelte | 36 +++++++++++++++++++ .../style/fonts/material-symbols-rounded.scss | 2 ++ src/routes/+layout.svelte | 22 ++++++++++++ vite.config.js | 3 +- 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/lib/components/PwaStatus.svelte diff --git a/src/lib/components/Navigation.svelte b/src/lib/components/Navigation.svelte index fa735681..2601002c 100644 --- a/src/lib/components/Navigation.svelte +++ b/src/lib/components/Navigation.svelte @@ -17,6 +17,9 @@
+ {#await import("$lib/components/PwaStatus.svelte") then { default: PwaStatus }} + + {/await} {#if browser && !("serial" in navigator)} + import {useRegisterSW} from "virtual:pwa-register/svelte" + + const {needRefresh, updateServiceWorker, offlineReady} = useRegisterSW() + + +{#if !$needRefresh} + +{:else if $offlineReady} +
offline_pin
+{:else} +
offline_bolt
+{/if} + + diff --git a/src/lib/style/fonts/material-symbols-rounded.scss b/src/lib/style/fonts/material-symbols-rounded.scss index 9875484f..2b902e0c 100644 --- a/src/lib/style/fonts/material-symbols-rounded.scss +++ b/src/lib/style/fonts/material-symbols-rounded.scss @@ -7,6 +7,8 @@ } .icon { + user-select: none; + direction: ltr; display: inline-block; diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 1d2bfb61..dff7dd07 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -6,6 +6,7 @@ import Navigation from "$lib/components/Navigation.svelte" import {hasSerialPermission} from "$lib/serial/device.js" import {initSerial} from "$lib/serial/connection.js" + import {pwaInfo} from "virtual:pwa-info" onMount(async () => { const theme = themeFromSourceColor(argbFromHex("#6D81C7"), [ @@ -14,10 +15,31 @@ const dark = true // window.matchMedia("(prefers-color-scheme: dark)").matches applyTheme(theme, {target: document.body, dark}) + if (pwaInfo) { + /** @type {import('vite-plugin-pwa/types').RegisterSWOptions} */ + const swOptions = { + immediate: true, + onRegisteredSW(url, registration) { + console.log("Service Worker Registered", url, registration) + }, + onRegisterError(error) { + console.log("ServiceWorker Registration Error", error) + }, + } + const {registerSW} = await import("virtual:pwa-register") + registerSW(swOptions) + } + if (await hasSerialPermission()) await initSerial() }) + + $: webManifestLink = pwaInfo ? pwaInfo.webManifest.linkTag : "" + + {@html webManifestLink} + +
diff --git a/vite.config.js b/vite.config.js index ee08226a..63f92879 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,6 +1,7 @@ import {sveltekit} from "@sveltejs/kit/vite" import {defineConfig} from "vite" +import {SvelteKitPWA} from "@vite-pwa/sveltekit" export default defineConfig({ - plugins: [sveltekit()], + plugins: [sveltekit(), SvelteKitPWA()], })