OctoSpaccHub/static/ServiceWorker.js

50 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

const cachables = {
"/": "networkFirst",
"//": "networkFirst",
};
2024-04-27 10:31:35 +02:00
const checkUrlCaching = (url) => {
let caching = (cachables[url] || cachables[`/${url.split('://').slice(1).join('://').split('/')[1]}/`]);
if (caching === undefined) {
caching = cachables['//'];
};
return caching;
};
const putResponseInCache = (request, response) => {
if (request.method === 'GET' && response.ok) {
return caches.open('GamingShitposting/v1').then((cache) => {
try {
return cache.put(request, response.clone());
} catch(err) {}
});
}
}
const strategies = {
networkFirst: async (request) => {
try {
const networkResponse = await fetch(request);
putResponseInCache(request, networkResponse);
return networkResponse;
} catch (error) {
return ((await caches.match(request)) || Response.error());
}
},
cacheFirst: async (request) => {
const fetchResponsePromise = fetch(request).then(async (networkResponse) => {
putResponseInCache(request, networkResponse);
return networkResponse;
});
return (await caches.match(request)) || (await fetchResponsePromise);
},
}
self.addEventListener('activate', () => self.clients.claim());
self.addEventListener('fetch', (event) => {
const strategy = strategies[checkUrlCaching(event.request.url)];
if (strategy) {
return event.respondWith(strategy(event.request));
}
});