better caching of out-of-band images

This commit is contained in:
or-else
2021-08-09 16:04:31 -07:00
parent 58e319b58e
commit 47e7632399
+5 -2
View File
@@ -135,7 +135,10 @@ self.addEventListener('fetch', event => {
event.respondWith((async () => { event.respondWith((async () => {
// Try to find the response in the cache. // Try to find the response in the cache.
const cache = await caches.open(PACKAGE_VERSION); const cache = await caches.open(PACKAGE_VERSION);
const cachedResponse = await cache.match(event.request);
const reqUrl = new URL(event.request.url);
// Using ignoreSearch=true to read cached images and docs despite different auth signatures.
const cachedResponse = await cache.match(event.request, {ignoreSearch: (self.location.origin == reqUrl.origin)});
if (cachedResponse) { if (cachedResponse) {
return cachedResponse; return cachedResponse;
} }
@@ -144,7 +147,7 @@ self.addEventListener('fetch', event => {
if (!response || response.status != 200 || response.type != 'basic') { if (!response || response.status != 200 || response.type != 'basic') {
return response; return response;
} }
if (event.request.url && (event.request.url.startsWith('http://') || event.request.url.startsWith('https://'))) { if (reqUrl.protocol == 'http:' || reqUrl.protocol == 'https:') {
await cache.put(event.request, response.clone()); await cache.put(event.request, response.clone());
} }
return response; return response;