59 lines
1.4 KiB
JavaScript
59 lines
1.4 KiB
JavaScript
// Service Worker for caching EVE Online type icons only
|
|
const CACHE_NAME = 'eve-icons-v1';
|
|
const ICON_PROXY_URL = 'https://proxy.site.quack-lab.dev';
|
|
|
|
// Check if a request is for an icon
|
|
function isIconRequest(url) {
|
|
try {
|
|
const urlObj = new URL(url);
|
|
return urlObj.origin === ICON_PROXY_URL && urlObj.searchParams.has('url');
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Install event - prepare cache
|
|
self.addEventListener('install', (event) => {
|
|
self.skipWaiting();
|
|
});
|
|
|
|
// Activate event - clean up old caches
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((cacheNames) => {
|
|
return Promise.all(
|
|
cacheNames
|
|
.filter((name) => name !== CACHE_NAME)
|
|
.map((name) => caches.delete(name))
|
|
);
|
|
})
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
// Fetch event - intercept icon requests and cache them
|
|
self.addEventListener('fetch', (event) => {
|
|
const url = event.request.url;
|
|
|
|
if (!isIconRequest(url)) {
|
|
return;
|
|
}
|
|
|
|
event.respondWith(
|
|
caches.open(CACHE_NAME).then((cache) => {
|
|
return cache.match(event.request).then((cachedResponse) => {
|
|
if (cachedResponse) {
|
|
return cachedResponse;
|
|
}
|
|
|
|
return fetch(event.request).then((response) => {
|
|
if (response.ok) {
|
|
cache.put(event.request, response.clone());
|
|
}
|
|
return response;
|
|
});
|
|
});
|
|
})
|
|
);
|
|
});
|