Skip to content

Commit

Permalink
Update service-worker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
j4ii authored Mar 8, 2024
1 parent b5f44b3 commit 203032a
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
})
);
Expand Down

0 comments on commit 203032a

Please sign in to comment.