Skip to content

Commit

Permalink
[serviceworker] cache by url, not the whole request
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Nov 23, 2023
1 parent 196dce6 commit e4e7f86
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions serviceworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ self.addEventListener("fetch", function (e) {
return [4, caches.open(cacheName)];
case 1:
cache = _a.sent();
return [4, cache.match(event.request)];
return [4, cache.match(event.request.url)];
case 2:
response = _a.sent();
if (!(response === undefined)) return [3, 4];
console.log("[Service Worker] Response for " + event.request.url + " is not available in cache. Making an actual request...");
return [4, fetch(event.request)];
return [4, fetch(event.request.url)];
case 3:
response = _a.sent();
cache.put(event.request, response.clone());
cache.put(event.request.url, response.clone());
_a.label = 4;
case 4: return [2, response];
}
Expand Down
6 changes: 3 additions & 3 deletions serviceworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ self.addEventListener("fetch", (e) => {
event.respondWith((async () => {
console.log(`[Service Worker] Fetch ${event.request.url}`);
const cache = await caches.open(cacheName);
let response = await cache.match(event.request);
let response = await cache.match(event.request.url);
if (response === undefined) {
console.log(`[Service Worker] Response for ${event.request.url} is not available in cache. Making an actual request...`);
response = await fetch(event.request);
cache.put(event.request, response.clone());
response = await fetch(event.request.url);
cache.put(event.request.url, response.clone());
}
return response;
})());
Expand Down

0 comments on commit e4e7f86

Please sign in to comment.