Now the app is filling the PWA minimal lighhouse requirements

This commit is contained in:
maxluli
2022-10-11 11:34:06 +02:00
parent d23eb50fb3
commit 6e1512b4b8
2 changed files with 50 additions and 5 deletions

View File

@@ -4,6 +4,8 @@
"start_url": "/es2-2022-2023/rhmr/PokemonTinder/index.html",
"id": "/es2-2022-2023/rhmr/PokemonTinder/index.html",
"display": "fullscreen",
"theme_color": "red",
"background_color": "black",
"icons": [
{
"src": "/es2-2022-2023/rhmr/PokemonTinder/assets/img/Icon2.png",

53
sw.js
View File

@@ -1,7 +1,50 @@
// This code executes in its own worker or thread
self.addEventListener("install", event => {
console.log("Service worker installed");
});
self.addEventListener("activate", event => {
console.log("Service worker activated");
});
console.log("Service worker installed");
});
self.addEventListener("activate", event => {
console.log("Service worker activated");
});
self.addEventListener("fetch", event => {
// Stratégie Cache-First
event.respondWith(
caches
.match(event.request) // On vérifie si la requête a déjà été mise en cache
.then(cached => cached || fetch(event.request)) // sinon on requête le réseau
.then(
response =>
cache(event.request, response) // on met à jour le cache
.then(() => response) // et on résout la promesse avec l'objet Response
)
);
});
function cache(request, response) {
if (response.type === "error" || response.type === "opaque") {
return Promise.resolve(); // do not put in cache network errors
}
return caches
.open(CACHE_NAME)
.then(cache => cache.put(request, response.clone()));
}
const CACHE_NAME = "V2";
self.addEventListener("activate", event => {
// delete any unexpected caches
event.waitUntil(
caches
.keys()
.then(keys => keys.filter(key => key !== CACHE_NAME))
.then(keys =>
Promise.all(
keys.map(key => {
console.log(`Deleting cache ${key}`);
return caches.delete(key);
})
)
)
);
});