const CACHE_NAME = "epicnabbo-v1"; const STATIC_ASSETS = [ "/", "/manifest.json", "/assets/pwa/icon-192x192.png", "/assets/pwa/icon-512x512.png", ]; // Install event - cache static assets self.addEventListener("install", (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => { console.log("Caching static assets"); return cache.addAll(STATIC_ASSETS); }), ); self.skipWaiting(); }); // Activate event - clean up old caches self.addEventListener("activate", (event) => { event.waitUntil( caches.keys().then((cacheNames) => { return Promise.all( cacheNames .filter((name) => name !== CACHE_NAME) .map((name) => caches.delete(name)), ); }), ); self.clients.claim(); }); // Fetch event - network first, fallback to cache self.addEventListener("fetch", (event) => { // Skip non-GET requests if (event.request.method !== "GET") return; // Skip API calls and external requests const url = new URL(event.request.url); if (url.pathname.startsWith("/api") || url.origin !== location.origin) { return; } event.respondWith( fetch(event.request) .then((response) => { // Clone the response const responseClone = response.clone(); // Cache successful responses if (response.status === 200) { caches.open(CACHE_NAME).then((cache) => { cache.put(event.request, responseClone); }); } return response; }) .catch(() => { // Fallback to cache return caches.match(event.request).then((cachedResponse) => { if (cachedResponse) { return cachedResponse; } // Return offline page if available return caches.match("/"); }); }), ); }); // Handle push notifications self.addEventListener("push", (event) => { const data = event.data ? event.data.json() : {}; const title = data.title || "Epicnabbo"; const options = { body: data.body || "New notification", icon: "/assets/pwa/icon-192x192.png", badge: "/assets/pwa/icon-72x72.png", vibrate: [100, 50, 100], data: { url: data.url || "/", }, }; event.waitUntil(self.registration.showNotification(title, options)); }); // Handle notification click self.addEventListener("notificationclick", (event) => { event.notification.close(); event.waitUntil(clients.openWindow(event.notification.data.url || "/")); });