diff --git a/sw.js b/sw.js index ec4a7b3..15707dc 100644 --- a/sw.js +++ b/sw.js @@ -1,7 +1,10 @@ /* 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'; + + The app shell (index.html) is fetched NETWORK-FIRST so a new deploy + reaches the player immediately; if the network is down it falls back to + the cached copy. The icons and manifest are cache-first because they + only change with a version bump. */ +const CACHE = 'mineflappy-v2'; const ASSETS = [ './', './index.html', @@ -28,10 +31,33 @@ self.addEventListener('activate', e => { ); }); +function isAppShell(url) { + return url.pathname.endsWith('/') || + url.pathname.endsWith('/index.html') || + url.pathname.endsWith('/sw.js'); +} + self.addEventListener('fetch', e => { const req = e.request; - if (req.method !== 'GET' || new URL(req.url).origin !== location.origin) return; + if (req.method !== 'GET') return; + const url = new URL(req.url); + if (url.origin !== location.origin) return; + if (isAppShell(url)) { + /* network first - a redeploy shows up straight away */ + e.respondWith( + fetch(req).then(res => { + if (res && res.status === 200) { + const copy = res.clone(); + caches.open(CACHE).then(c => c.put(req, copy)); + } + return res; + }).catch(() => caches.match(req).then(hit => hit || caches.match('./index.html'))) + ); + return; + } + + /* everything else: cache first, refresh in the background */ e.respondWith( caches.match(req).then(hit => { const network = fetch(req).then(res => { @@ -40,7 +66,7 @@ self.addEventListener('fetch', e => { caches.open(CACHE).then(c => c.put(req, copy)); } return res; - }).catch(() => hit || caches.match('./index.html')); + }).catch(() => hit); return hit || network; }) );