From 1a3643d34a7556667e5fc98657c39910f5b62da2 Mon Sep 17 00:00:00 2001 From: Mamalizz Date: Thu, 6 Mar 2025 22:57:07 +0330 Subject: [PATCH] updated pwa sw --- frontend/public/sw.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/frontend/public/sw.js b/frontend/public/sw.js index f3c091e..7fab7af 100644 --- a/frontend/public/sw.js +++ b/frontend/public/sw.js @@ -3,18 +3,41 @@ import { precacheAndRoute } from "workbox-precaching"; // Precaching configuration for PWA assets precacheAndRoute(self.__WB_MANIFEST); -const VERSION = "1.0.0"; +// Version + +const VERSION = "1.0.423"; // Service Worker Installation self.addEventListener("install", (event) => { - self.skipWaiting(); // Force activate new SW immediately - console.log("Service Worker installed"); + 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 self.addEventListener("activate", (event) => { - event.waitUntil(self.clients.claim()); - console.log("Service Worker activated"); + event.waitUntil( + (async () => { + const clients = await self.clients.matchAll({ type: "window" }); + clients.forEach((client) => { + client.postMessage({ + type: "VERSION_CHECK", + version: VERSION, + }); + }); + self.clients.claim(); + })() + ); }); // Push Notification Handler for Django Web Push @@ -48,6 +71,5 @@ self.addEventListener("notificationclick", (event) => { self.addEventListener("message", (event) => { if (event.data === "SKIP_WAITING") { self.skipWaiting(); - self.clients.claim(); } });