/* Tally service worker: offline shell + update prompts. Bump CACHE on every deploy that changes the app. */ var CACHE = "tally-v4"; var SHELL = [ "/", "/index.html", "/styles.css", "/app.js", "/manifest.json", "/icon-192.png", "/icon-512.png" ]; self.addEventListener("install", function (e) { // {cache:"reload"} bypasses the browser HTTP cache so a fresh deploy // precaches new bytes instead of the stale responses it may hold. e.waitUntil(caches.open(CACHE).then(function (c) { return c.addAll(SHELL.map(function (u) { return new Request(u, { cache: "reload" }); })); })); }); self.addEventListener("activate", function (e) { e.waitUntil(caches.keys().then(function (keys) { return Promise.all(keys.filter(function (k) { return k !== CACHE; }).map(function (k) { return caches.delete(k); })); }).then(function () { return self.clients.claim(); })); }); self.addEventListener("message", function (e) { if (e.data && e.data.type === "SKIP_WAITING") self.skipWaiting(); }); self.addEventListener("fetch", function (e) { var url = new URL(e.request.url); if (e.request.method !== "GET" || url.origin !== self.location.origin) return; // Shell URLs ignore the query string (?v= cache-buster): one entry per file. var key = SHELL.indexOf(url.pathname) !== -1 ? url.pathname : e.request; e.respondWith(caches.match(key, { cacheName: CACHE }).then(function (hit) { if (hit) return hit; return fetch(e.request).then(function (res) { if (res && res.ok) { var copy = res.clone(); caches.open(CACHE).then(function (c) { c.put(key, copy); }); } return res; }); })); });