forked from Maxluli/TinderPokemon
Now the app is filling the PWA minimal lighhouse requirements
This commit is contained in:
@@ -4,6 +4,8 @@
|
|||||||
"start_url": "/es2-2022-2023/rhmr/PokemonTinder/index.html",
|
"start_url": "/es2-2022-2023/rhmr/PokemonTinder/index.html",
|
||||||
"id": "/es2-2022-2023/rhmr/PokemonTinder/index.html",
|
"id": "/es2-2022-2023/rhmr/PokemonTinder/index.html",
|
||||||
"display": "fullscreen",
|
"display": "fullscreen",
|
||||||
|
"theme_color": "red",
|
||||||
|
"background_color": "black",
|
||||||
"icons": [
|
"icons": [
|
||||||
{
|
{
|
||||||
"src": "/es2-2022-2023/rhmr/PokemonTinder/assets/img/Icon2.png",
|
"src": "/es2-2022-2023/rhmr/PokemonTinder/assets/img/Icon2.png",
|
||||||
|
|||||||
@@ -1,7 +1,50 @@
|
|||||||
// This code executes in its own worker or thread
|
// This code executes in its own worker or thread
|
||||||
self.addEventListener("install", event => {
|
self.addEventListener("install", event => {
|
||||||
console.log("Service worker installed");
|
console.log("Service worker installed");
|
||||||
});
|
});
|
||||||
self.addEventListener("activate", event => {
|
self.addEventListener("activate", event => {
|
||||||
console.log("Service worker activated");
|
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);
|
||||||
|
})
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user