Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions sw.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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) => {
Expand All @@ -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);
Expand Down
Loading