From 337854e58261f64879c7769701c9581387340a89 Mon Sep 17 00:00:00 2001 From: Mamalizz Date: Thu, 6 Mar 2025 18:02:46 +0330 Subject: [PATCH] added new sw --- frontend/public/sw.js | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 frontend/public/sw.js diff --git a/frontend/public/sw.js b/frontend/public/sw.js new file mode 100644 index 0000000..f3c091e --- /dev/null +++ b/frontend/public/sw.js @@ -0,0 +1,53 @@ +import { precacheAndRoute } from "workbox-precaching"; + +// Precaching configuration for PWA assets +precacheAndRoute(self.__WB_MANIFEST); + +const VERSION = "1.0.0"; + +// Service Worker Installation +self.addEventListener("install", (event) => { + self.skipWaiting(); // Force activate new SW immediately + console.log("Service Worker installed"); +}); + +// Service Worker Activation +self.addEventListener("activate", (event) => { + event.waitUntil(self.clients.claim()); + console.log("Service Worker activated"); +}); + +// Push Notification Handler for Django Web Push +self.addEventListener("push", (event) => { + try { + const payload = event.data?.json() || { + title: "New Notification", + body: "You have a new message", + icon: "/logo-192x192.png", + data: { url: "/" }, + }; + + event.waitUntil( + self.registration.showNotification(payload.title, { + body: payload.body, + icon: payload.icon || "/logo-192x192.png", + data: payload.data, + }) + ); + } catch (error) { + console.error("Push handling failed:", error); + } +}); + +// Notification Click Handler +self.addEventListener("notificationclick", (event) => { + event.notification.close(); + event.waitUntil(clients.openWindow(event.notification.data?.url || "/")); +}); + +self.addEventListener("message", (event) => { + if (event.data === "SKIP_WAITING") { + self.skipWaiting(); + self.clients.claim(); + } +});