From 1286c0e80a005ae54cdc57415557b6b411cc95d5 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Wed, 11 Dec 2024 00:29:32 +0100 Subject: [PATCH] add detectSWUpdate again --- src/routes/+layout.svelte | 44 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index f7b44374..4e6d6179 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -91,6 +91,42 @@ $numberOfUnreadNotification = output?.numberOfUnreadNotification?.length; //pushNotification() }; + /* + async function pushNotification() { + Notification?.requestPermission()?.then((perm) => { + if (perm === "granted") { + new Notification("Stocknear", { + body: "this is more text", + }); + } + }); + } + */ + + //Check Service Worker (SW) + async function detectSWUpdate() { + try { + const registration = await navigator.serviceWorker.ready; + + registration.addEventListener("updatefound", () => { + const newSW = registration.installing; + + newSW?.addEventListener("statechange", () => { + if (newSW.state === "installed") { + const message = + "🚀 A fresh update is ready! Reload now to enjoy the latest features?"; + + if (confirm(message)) { + newSW.postMessage({ type: "SKIP_WAITING" }); + window.location.reload(); + } + } + }); + }); + } catch (error) { + console.error("Service Worker registration failed:", error); + } + } const loadWorker = async () => { if ("serviceWorker" in navigator) { @@ -129,13 +165,19 @@ onMount(async () => { //await fallbackWorker(); - await Promise.all([checkMarketHour(), loadWorker()]); + await Promise.all([checkMarketHour(), loadWorker(), detectSWUpdate()]); //await pushNotification() if ($showCookieConsent === true) { Cookie = (await import("$lib/components/Cookie.svelte")).default; } + /* + if (window?.innerWidth <= 768) { + detectSWUpdate(); + } + */ + //Clear all the cache every 20 min const interval = setInterval( () => {