Skip to content

Commit

Permalink
fix: fix offline mode
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesrocket committed Oct 20, 2024
1 parent 077bcc2 commit 44b4992
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions static/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ const cacheList = [
"/webfonts/hack-regular.woff2",
];

function fetchedResponse(response, event, cache) {
if (response.status < 400) {
console.log("Caching the response to", event.request.url);
cache.put(event.request, response.clone());
} else {
console.log("Not caching the response to", event.request.url);
}
function fetchedResponse(event, cache) {
return fetch(event.request).then((networkResponse) => {
if (networkResponse.status < 400) {
console.log("Caching the response to", event.request.url);
cache.put(event.request, networkResponse.clone());
} else {
console.log("Service worker not caching the response to", event.request.url);
}

return response;
return networkResponse;
});
}

oninstall = (event) => {
Expand Down Expand Up @@ -57,10 +59,10 @@ onfetch = (event) => {
event.respondWith(caches.open(cacheName).then((cache) => {
return cache.match(event.request)
.then((cachedResponse) => {
const fetchedResponse = fetch(event.request).then((networkResponse) => {
return fetchedResponse(networkResponse, event, cache);
}).catch(() => caches.match("/offline/"));
return cachedResponse || fetchedResponse;
return cachedResponse || fetchedResponse(
event, cache).catch(
() => caches.match("/offline/")
);
});
}));
return;
Expand All @@ -75,9 +77,7 @@ onfetch = (event) => {
return cachedResponse;
}

return fetch(event.request).then((networkResponse) => {
return fetchedResponse(networkResponse, event, cache);
});
return fetchedResponse(event, cache);
});
}));
return;
Expand Down

0 comments on commit 44b4992

Please sign in to comment.