From 2247f004cc6312eb00ebda11403d6b5b384824f4 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Tue, 17 May 2022 01:39:35 +0200 Subject: [PATCH] Actually add caching to service worker --- service-worker.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/service-worker.js b/service-worker.js index 23aadf886..1a84494dc 100644 --- a/service-worker.js +++ b/service-worker.js @@ -46,9 +46,17 @@ self.addEventListener('fetch', (event) => { return } event.respondWith( - caches.match(event.request).then(function (response) { - return response || fetch(event.request); - }), + async () => { + const matched = caches.match(event.request) + if(matched){ + return matched + } + const response = fetch(event.request); + const cache = await caches.open(version); + await cache.put(event.request.url, response); + console.log("Put ",event.request.url,"into the cache") + return response + } ); } catch (e) { console.error("CRASH IN SW:", e)