Installable as an app (PWA): manifest, pixel-art icons, offline service worker
- manifest.webmanifest (full screen, any orientation), icons 192/512/maskable/apple-touch - sw.js: network first, cache as offline fallback, the savegame API is never cached - registered only over HTTPS or on localhost; nginx serves the manifest type - README: installing, HTTPS requirement Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b8bf2b8503
commit
44b938f0e5
9 files changed
+85
-1
No files matched your search
@@ -0,0 +1,45 @@
|
||||
// DungeonCraft service worker: makes the game installable and playable offline.
|
||||
// Network first (updates arrive at once), the cache is the fallback when offline.
|
||||
// The savegame service (/api/) is never cached.
|
||||
const CACHE = "dungeoncraft-v1";
|
||||
const CORE = [
|
||||
"./",
|
||||
"index.html",
|
||||
"mq_js_bundle.js",
|
||||
"dc_plugin.js",
|
||||
"dungeoncraft.wasm",
|
||||
"manifest.webmanifest",
|
||||
"icons/icon-192.png",
|
||||
"icons/icon-512.png",
|
||||
];
|
||||
|
||||
self.addEventListener("install", (event) => {
|
||||
event.waitUntil(caches.open(CACHE).then((c) => c.addAll(CORE)).then(() => self.skipWaiting()));
|
||||
});
|
||||
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(
|
||||
caches.keys()
|
||||
.then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))
|
||||
.then(() => self.clients.claim())
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("fetch", (event) => {
|
||||
const req = event.request;
|
||||
const url = new URL(req.url);
|
||||
if (req.method !== "GET" || url.origin !== self.location.origin || url.pathname.includes("/api/")) {
|
||||
return;
|
||||
}
|
||||
event.respondWith(
|
||||
fetch(req)
|
||||
.then((res) => {
|
||||
if (res.ok) {
|
||||
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")))
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user