From 203032a99d75ecf294a35d1f08d4b0c239f816c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BD=8A=EF=BC=A1=EF=BD=89?= <143520569+ittsjai@users.noreply.github.com> Date: Fri, 8 Mar 2024 21:48:15 +0530 Subject: [PATCH] Update service-worker.js --- service-worker.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/service-worker.js b/service-worker.js index 02609cf..a6f90c9 100644 --- a/service-worker.js +++ b/service-worker.js @@ -5,29 +5,41 @@ const urlsToCache = [ "/script.js", "/logo/icon.png", ]; + self.addEventListener("install", function (event) { event.waitUntil( caches.open(CACHE_NAME).then(function (cache) { - return cache.addAll(urlsToCache); + return cache.addAll(urlsToCache).catch(function(error) { + console.error('Failed to cache some or all resources:', error); + }); }) ); }); + self.addEventListener("fetch", function (event) { event.respondWith( caches.match(event.request).then(function (response) { if (response) { return response; } + var fetchRequest = event.request.clone(); + return fetch(fetchRequest).then(function (response) { if (!response || response.status !== 200 || response.type !== "basic") { return response; } + var responseToCache = response.clone(); + caches.open(CACHE_NAME).then(function (cache) { cache.put(event.request, responseToCache); }); + return response; + }).catch(function(error) { + console.error('Failed to fetch resource:', error); + throw error; }); }) );