up up and away
This commit is contained in:
70
zuss/sw.js
70
zuss/sw.js
@@ -1,5 +1,4 @@
|
||||
// Simple offline cache for kiosk usage
|
||||
const CACHE = "wfw-aushang-2025.12.24.2";
|
||||
const CACHE = "wfw-aushang-2025.12.24.6";
|
||||
|
||||
const ASSETS = [
|
||||
"/zuss/",
|
||||
@@ -12,48 +11,51 @@ const ASSETS = [
|
||||
];
|
||||
|
||||
self.addEventListener("install", (event) => {
|
||||
event.waitUntil(
|
||||
caches.open(CACHE).then((c) => c.addAll(ASSETS)).then(() => self.skipWaiting())
|
||||
);
|
||||
event.waitUntil((async () => {
|
||||
const cache = await caches.open(CACHE);
|
||||
await cache.addAll(ASSETS);
|
||||
self.skipWaiting();
|
||||
})());
|
||||
});
|
||||
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(
|
||||
(async () => {
|
||||
const keys = await caches.keys();
|
||||
await Promise.all(keys.map(k => (k === CACHE ? null : caches.delete(k))));
|
||||
await self.clients.claim();
|
||||
})()
|
||||
);
|
||||
event.waitUntil((async () => {
|
||||
const keys = await caches.keys();
|
||||
await Promise.all(keys.map(k => (k === CACHE ? null : caches.delete(k))));
|
||||
self.clients.claim();
|
||||
})());
|
||||
});
|
||||
|
||||
// Network-first for version.json + list, cache-first for static assets
|
||||
self.addEventListener("fetch", (event) => {
|
||||
const url = new URL(event.request.url);
|
||||
const req = event.request;
|
||||
const url = new URL(req.url);
|
||||
|
||||
// Only handle same-origin requests
|
||||
if (url.origin !== self.location.origin) return;
|
||||
|
||||
const isVersion = url.pathname.endsWith("/zuss/version.json");
|
||||
|
||||
if (isVersion) {
|
||||
event.respondWith(
|
||||
fetch(event.request, { cache: "no-store" }).catch(() => caches.match(event.request))
|
||||
);
|
||||
// Network-first for version.json (so updates are found)
|
||||
if (url.pathname.endsWith("/zuss/version.json")) {
|
||||
event.respondWith((async () => {
|
||||
try {
|
||||
const fresh = await fetch(req, { cache: "no-store" });
|
||||
const cache = await caches.open(CACHE);
|
||||
cache.put(req, fresh.clone());
|
||||
return fresh;
|
||||
} catch {
|
||||
const cached = await caches.match(req);
|
||||
return cached || new Response('{"version":"2025.12.24.6"}', { headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
})());
|
||||
return;
|
||||
}
|
||||
|
||||
// Cache-first for our own static assets
|
||||
// Cache-first for app shell
|
||||
if (url.pathname.startsWith("/zuss/")) {
|
||||
event.respondWith(
|
||||
caches.match(event.request).then((cached) => {
|
||||
if (cached) return cached;
|
||||
return fetch(event.request).then((resp) => {
|
||||
const copy = resp.clone();
|
||||
caches.open(CACHE).then((c) => c.put(event.request, copy));
|
||||
return resp;
|
||||
});
|
||||
})
|
||||
);
|
||||
event.respondWith((async () => {
|
||||
const cached = await caches.match(req);
|
||||
if (cached) return cached;
|
||||
const fresh = await fetch(req);
|
||||
const cache = await caches.open(CACHE);
|
||||
cache.put(req, fresh.clone());
|
||||
return fresh;
|
||||
})());
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user