Lettres Magiques — jeu de prénoms HTML5 (mobile+ordi)
This commit is contained in:
36
sw.js
Normal file
36
sw.js
Normal file
@@ -0,0 +1,36 @@
|
||||
/* Service worker — network-first (frais en ligne, cache en secours hors-ligne) */
|
||||
const CACHE = "lettres-magiques-v2";
|
||||
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))
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user