From cc515d57c6c70a32dee9f01ff9de082e90381ad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 23 Jul 2023 18:04:57 +0200 Subject: [PATCH] Changing the caching strategy --- pwa/sw.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/pwa/sw.js b/pwa/sw.js index a093044..6620dee 100644 --- a/pwa/sw.js +++ b/pwa/sw.js @@ -28,17 +28,15 @@ self.addEventListener('fetch', (event) => { return; } - event.respondWith((async () => { + event.respondWith(caches.open(cache_name).then((cache) => { log_message(`Fetching resource: ${event.request.url}`); - const cache_promise = await caches.match(event.request); - if (cache_promise) { - log_message(`Resource retrieved from cache: ${event.request.url}`); - return cache_promise; - } - const fetch_promise = await fetch(event.request); - const cache = await caches.open(cache_name); - log_message(`Caching new resource: ${event.request.url}`); - cache.put(event.request, fetch_promise.clone()); - return fetch_promise; - })()); + return cache.match(event.request).then((cached_response) => { + const fetched_response = fetch(event.request).then((network_response) => { + log_message(`Caching resource: ${event.request.url}`); + cache.put(event.request, network_response.clone()); + return network_response; + }); + return cached_response || fetched_response; + }); + })); });