/* Mineflappy offline cache. Cache-first for the app shell, with a background refresh so a new deploy is picked up on the next visit. */ const CACHE = 'mineflappy-v1'; const ASSETS = [ './', './index.html', './manifest.webmanifest', './icon-192.png', './icon-512.png', './apple-touch-icon.png', './favicon-32.png' ]; self.addEventListener('install', e => { e.waitUntil( caches.open(CACHE) .then(c => c.addAll(ASSETS)) .then(() => self.skipWaiting()) ); }); self.addEventListener('activate', e => { e.waitUntil( caches.keys() .then(keys => Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))) .then(() => self.clients.claim()) ); }); self.addEventListener('fetch', e => { const req = e.request; if (req.method !== 'GET' || new URL(req.url).origin !== location.origin) return; e.respondWith( caches.match(req).then(hit => { const network = fetch(req).then(res => { if (res && res.status === 200 && res.type === 'basic') { const copy = res.clone(); caches.open(CACHE).then(c => c.put(req, copy)); } return res; }).catch(() => hit || caches.match('./index.html')); return hit || network; }) ); });