Skip to content

Commit

Permalink
fixup! Cleanup service worker a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
bas080 committed Jun 19, 2024
1 parent 78167db commit 8d74a69
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions serviceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,29 @@ const staticTopList = "toplist-v1";

const actions = {
undefined(event) {
console.error(`No action with name "${event.data.action}".`)
console.error(`No action with name "${event.data.action}".`);
},
clearCache() {
caches.keys().then(function(names) {
for (let name of names)
caches.delete(name);
caches.keys().then(function (names) {
for (let name of names) caches.delete(name);
});
}
}
},
};

self.addEventListener("fetch", async ({request}) => {
self.addEventListener("fetch", async ({ request }) => {
try {
const cachedResponse = await caches.match(request);

if (cachedResponse) return cachedResponse;

return await fetch(request);
} catch (error) {
console.error('Fetch error:', error);
console.error("Fetch error:", error);
throw error;
}
});

// Listen for messages from clients
self.addEventListener("message", (event) => {
actions[event.data.action](event)
actions[event.data.action](event);
});

0 comments on commit 8d74a69

Please sign in to comment.