// Kill switch — the app no longer uses a service worker.
//
// It was added for nice-to-have PWA/offline caching, but it sat in the boot
// path and put first loads at risk (post-deploy chunk stalls), which a young
// site with few visitors can't afford. Browser HTTP cache + the Cloudflare edge
// are enough for a news site.
//
// Existing visitors still have the OLD worker registered and controlling their
// pages. This replacement (served at /service-worker.js, no-cache) takes over,
// wipes the old caches, and unregisters itself. There is deliberately NO fetch
// handler, so every request goes straight to the network / browser HTTP cache.
// The app also unregisters any worker on load (see app.html) as a backstop.
self.addEventListener('install', () => self.skipWaiting());

self.addEventListener('activate', (event) => {
  event.waitUntil(
    (async () => {
      try {
        const keys = await caches.keys();
        await Promise.all(keys.map((k) => caches.delete(k)));
      } catch (e) {
        /* best effort */
      }
      try {
        await self.registration.unregister();
      } catch (e) {
        /* best effort */
      }
    })()
  );
});
