// ThoughtSync service worker — installable shell, deliberately NOT offline-first. // // This exists so the web app satisfies the PWA install criteria ("Add to Home // Screen") and shows a friendly page when a navigation happens with no network. // The real offline client is the future Android app (M5); to stay always-fresh // and avoid deploy staleness, this SW does NOT cache the app shell, hashed build // assets, or any /api response — everything but the offline fallback goes // straight to the network. const CACHE = "thoughtsync-shell-v1"; const OFFLINE_URL = "/offline.html"; self.addEventListener("install", (event) => { event.waitUntil( caches .open(CACHE) .then((cache) => cache.add(OFFLINE_URL)) .then(() => self.skipWaiting()), ); }); self.addEventListener("activate", (event) => { event.waitUntil( caches .keys() .then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))) .then(() => self.clients.claim()), ); }); // Only intercept page navigations: try the network, fall back to the cached // offline page when the server is unreachable. All other requests (hashed // assets, /api/*) are left untouched and hit the network normally. self.addEventListener("fetch", (event) => { if (event.request.mode !== "navigate") return; event.respondWith(fetch(event.request).catch(() => caches.match(OFFLINE_URL))); });