// EXIF Scanner — Service Worker // Cache-first for static assets, network-first for API. const CACHE = "exif-cache-v1"; const STATIC = ["/", "/static/"]; self.addEventListener("install", (e) => { self.skipWaiting(); e.waitUntil( caches.open(CACHE).then((c) => c.addAll(["/", "/static/index.html"])) ); }); self.addEventListener("activate", (e) => { e.waitUntil(clients.claim()); }); self.addEventListener("fetch", (e) => { const url = new URL(e.request.url); // API calls: network-first if (url.pathname.startsWith("/api/")) { e.respondWith( fetch(e.request).catch(() => { return new Response( JSON.stringify({ offline: true, error: "No network connection" }), { status: 503, headers: { "Content-Type": "application/json" } } ); }) ); return; } // Static assets: cache-first e.respondWith( caches.match(e.request).then((r) => r || fetch(e.request)) ); }); // Background Sync self.addEventListener("sync", (e) => { if (e.tag === "sync-photos") { e.waitUntil(syncPhotos()); } }); async function syncPhotos() { const clients = await self.clients.matchAll(); for (const client of clients) { client.postMessage({ type: "SYNC_TRIGGERED" }); } }