const CACHE = "tym-v1.1.0"; const ASSETS = [ "/", "/index.html", "/styles.css", "/app.js", "/manifest.json", "/version.json", "/icon.png", "/icon-192x192.png", "/icon-512x512.png", "/lib/api.js", "/lib/auth.js", "/lib/badge.js", "/lib/offlineQueue.js", "/lib/pwa.js", "/lib/ui.js", ]; self.addEventListener("install", (event) => { event.waitUntil( caches.open(CACHE).then((c) => c.addAll(ASSETS)).then(() => self.skipWaiting()) ); }); self.addEventListener("activate", (event) => { event.waitUntil( caches.keys().then((names) => Promise.all(names.filter((n) => n !== CACHE).map((n) => caches.delete(n))) ).then(() => self.clients.claim()) ); }); self.addEventListener("fetch", (event) => { const { request } = event; if (request.method !== "GET") return; const url = new URL(request.url); if (url.origin !== self.location.origin) return; if (url.pathname.startsWith("/api/")) { event.respondWith(fetch(request).catch(() => new Response(JSON.stringify({ offline: true }), { status: 503, headers: { "Content-Type": "application/json" }, }))); return; } if (request.mode === "navigate") { event.respondWith( fetch(request).catch(() => caches.match("/index.html")) ); return; } event.respondWith( caches.match(request, { ignoreSearch: true }).then((cached) => cached || fetch(request).then((resp) => { if (resp.ok) { const clone = resp.clone(); caches.open(CACHE).then((c) => c.put(request, clone)); } return resp; }) ) ); }); self.addEventListener("sync", (event) => { if (event.tag === "sync-logs") { event.waitUntil( self.clients.matchAll().then((list) => { list.forEach((c) => c.postMessage({ type: "SYNC_QUEUE" })); }) ); } });