diff --git a/frontend/plugins/pwaUpdate.client.ts b/frontend/plugins/pwaUpdate.client.ts index 0fe75c1..845ed4d 100644 --- a/frontend/plugins/pwaUpdate.client.ts +++ b/frontend/plugins/pwaUpdate.client.ts @@ -4,55 +4,38 @@ export default defineNuxtPlugin(() => { const updateAvailable = ref(false); let wb: Workbox | null = null; - // Enhanced PWA detection - const isStandalone = window.matchMedia( - "(display-mode: standalone)" - ).matches; - const isIOSPWA = (window.navigator as any).standalone; - const isInstalledAsPWA = isStandalone || isIOSPWA; - - if ("serviceWorker" in navigator && isInstalledAsPWA) { + if ("serviceWorker" in navigator) { wb = new Workbox("/sw.js"); - // Listen for version checks from SW - wb.addEventListener("message", (event) => { - if (event.data.type === "VERSION_CHECK") { + // Listen for messages from the service worker + navigator.serviceWorker.addEventListener("message", (event) => { + if (event.data && event.data.type === "VERSION_CHECK") { checkForUpdate(event.data.version); } }); - // Register service worker + // Register the service worker and check if there's already a waiting one wb.register().then((registration: any) => { if (registration.waiting) { checkForUpdate(); } }); - - // Detect controller changes for updates - wb.addEventListener("controlling", () => { - window.location.reload(); - }); } + // 🔹 Function to compare versions and show update modal if needed const checkForUpdate = (newVersion?: string) => { const currentVersion = localStorage.getItem("pwa_version"); - if (!newVersion) { - // If no version provided, just trigger update - updateAvailable.value = true; - return; - } - - if (currentVersion !== newVersion) { + if (newVersion && currentVersion !== newVersion) { updateAvailable.value = true; localStorage.setItem("pwa_version", newVersion); } }; + // 🔹 Function to apply the update const handleUpdate = () => { - if (!!wb) { - wb.messageSW({ type: "SKIP_WAITING" }); - } + wb?.messageSW({ type: "SKIP_WAITING" }); + window.location.reload(); }; return { diff --git a/frontend/public/sw.js b/frontend/public/sw.js index 8569a3e..89509b4 100644 --- a/frontend/public/sw.js +++ b/frontend/public/sw.js @@ -5,23 +5,11 @@ precacheAndRoute(self.__WB_MANIFEST); // Version -const VERSION = "1.0.6"; +const VERSION = "1.0.7"; // Service Worker Installation self.addEventListener("install", (event) => { self.skipWaiting(); - // Clear old cache during install - event.waitUntil( - caches.keys().then((cacheNames) => { - return Promise.all( - cacheNames.map((cache) => { - if (cache !== self.registration.scope + "::" + VERSION) { - return caches.delete(cache); - } - }) - ); - }) - ); }); // Service Worker Activation @@ -29,13 +17,14 @@ self.addEventListener("activate", (event) => { event.waitUntil( (async () => { const clients = await self.clients.matchAll({ type: "window" }); - clients.forEach((client) => { - client.postMessage({ - type: "VERSION_CHECK", - version: VERSION, - }); - }); + + // Notify all open clients about the version + clients.forEach((client) => + client.postMessage({ type: "VERSION_CHECK", version: VERSION }) + ); + self.clients.claim(); + console.log("Service Worker Activated (Version: " + VERSION + ")"); })() ); });