From 94b57e70ce30abc4fc7bfb11abcd86652831bffd Mon Sep 17 00:00:00 2001 From: Maidul Islam Date: Tue, 10 Nov 2026 19:52:33 +0600 Subject: [PATCH] Players get updates straight away: fresh files and a quick switch After an update, the saved copy of the game started first, so a player's first visit still ran the old version (no insights, and still the iPhone Bit freeze). - When the new version takes over while the player is still on the start menu with no panel open, the page reloads into it. Otherwise a toast says it loads next time, so nothing in progress is lost. - The service worker fetches files with cache: 'reload' when installing and 'no-cache' when refreshing, so it never stores the browser's old copies (our server lets browsers keep files for 4 hours). - sw.js -> v30. Co-Authored-By: Claude Opus 5.5 --- src/main.js | 10 ++++++++++ sw.js | 9 ++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main.js b/src/main.js index d872fc0..6cdc980 100644 --- a/src/main.js +++ b/src/main.js @@ -2412,6 +2412,16 @@ if ('serviceWorker' in navigator && (!isLocal || new URLSearchParams(location.se navigator.serviceWorker.register('./sw.js').then(() => { $('offline-note').textContent = 'It also works offline once it has loaded.'; }).catch(() => { /* offline support is optional */ }); + // The saved copy starts first, so right after an update this page is still the old version. + // When the new version takes over, switch to it if nothing would be lost (still on the start + // menu with no panel open); otherwise it loads next time. + const hadVersion = !!navigator.serviceWorker.controller; + navigator.serviceWorker.addEventListener('controllerchange', () => { + if (!hadVersion) return; // first visit: this page is already the newest version + const busy = hasPlayed || state !== 'menu' || document.querySelector('.overlay:not(.hidden):not(#menu)') || /^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement?.tagName || ''); + if (busy) toast('✨ A new version of MiniCrafter is ready. It loads next time you open the game.'); + else location.reload(); + }); } // Roomy screens show the settings straight away; phones keep them folded. if (matchMedia('(min-width: 720px) and (min-height: 640px)').matches) $('settings-fold').open = true; diff --git a/sw.js b/sw.js index fd980a0..458b9c7 100644 --- a/sw.js +++ b/sw.js @@ -1,6 +1,6 @@ // Service worker: caches the whole game so it works offline once loaded. // Bump VERSION whenever files change so players get the update. -const VERSION = 'minicrafter-v29'; +const VERSION = 'minicrafter-v30'; const CORE = [ './', './index.html', './style.css', './privacy.html', './terms.html', './legal.css', './features.html', './features.css', './manifest.webmanifest', './site-config.json', './icons/icon-192.png', './icons/icon-512.png', './icons/icon-maskable-512.png', './icons/apple-touch-icon.png', './icons/favicon-32.png', @@ -41,7 +41,8 @@ const CORE = [ ]; self.addEventListener('install', (e) => { - e.waitUntil(caches.open(VERSION).then((c) => c.addAll(CORE)).then(() => self.skipWaiting())); + // cache: 'reload' skips the browser's own HTTP cache, so a new version never picks up old files. + e.waitUntil(caches.open(VERSION).then((c) => c.addAll(CORE.map((u) => new Request(u, { cache: 'reload' })))).then(() => self.skipWaiting())); }); self.addEventListener('activate', (e) => { @@ -61,7 +62,9 @@ self.addEventListener('fetch', (e) => { if (url.pathname.includes('/api/') || url.pathname.endsWith('/rooms')) return; // accounts and Play Together are always live, never cached e.respondWith(caches.open(VERSION).then(async (cache) => { const cached = await cache.match(req, { ignoreSearch: req.mode === 'navigate' }); - const network = fetch(req).then((res) => { + // no-cache: always check with the server (a cheap "not changed" answer when nothing changed). + const fresh = req.mode === 'navigate' ? new Request(req.url, { cache: 'no-cache' }) : new Request(req, { cache: 'no-cache' }); + const network = fetch(fresh).then((res) => { if (res.ok) cache.put(req, res.clone()); return res; }).catch(() => null);