Better cacheability checks

This commit is contained in:
Andreas Svanberg 2024-05-08 20:29:52 +02:00
parent 1aa00dacb5
commit 9d290fa645

@ -45,12 +45,22 @@ self.addEventListener('fetch', /** @param {FetchEvent} event */ async function (
return (await cache.match(event.request)) || (await fetchedResponse);
}
function isCacheable(url) {
const pathname = new URL(url).pathname;
return !pathname.startsWith('login')
function isCacheable(request) {
if (request.method !== 'GET')
return false;
if (request.destination === 'manifest')
return false;
if (request.destination === 'serviceworker')
return false;
if (request.url.match('oauth2'))
return false;
if (request.url.match('login'))
return false;
return true;
}
if (event.request.method === 'GET' && isCacheable(event.request.url)) {
if (isCacheable(event.request)) {
console.log('interceptiing fetch', event.request.url);
return event.respondWith(staleWhileRevalidate());
}