37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
/* Service worker — network-first (frais en ligne, cache en secours hors-ligne) */
|
|
const CACHE = "lettres-magiques-v5";
|
|
const ASSETS = [
|
|
".",
|
|
"index.html",
|
|
"style.css",
|
|
"game.js",
|
|
"manifest.webmanifest",
|
|
"icons/icon-192.png",
|
|
"icons/icon-512.png",
|
|
];
|
|
|
|
self.addEventListener("install", (e) => {
|
|
e.waitUntil(caches.open(CACHE).then((c) => c.addAll(ASSETS)).then(() => self.skipWaiting()));
|
|
});
|
|
|
|
self.addEventListener("activate", (e) => {
|
|
e.waitUntil(
|
|
caches.keys().then((keys) =>
|
|
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
|
|
).then(() => self.clients.claim())
|
|
);
|
|
});
|
|
|
|
self.addEventListener("fetch", (e) => {
|
|
if (e.request.method !== "GET") return;
|
|
// Network-first : on tente le réseau, on met à jour le cache, et on retombe
|
|
// sur le cache si on est hors-ligne. Garantit des mises à jour immédiates.
|
|
e.respondWith(
|
|
fetch(e.request).then((resp) => {
|
|
const copy = resp.clone();
|
|
caches.open(CACHE).then((c) => c.put(e.request, copy)).catch(() => {});
|
|
return resp;
|
|
}).catch(() => caches.match(e.request))
|
|
);
|
|
});
|