Files
bscscore/public/sw.js
T
Frank Schwenk 3327f6e19e chore(release): 2.1.1
Refs #30

Made-with: Cursor
2026-04-16 13:20:48 +02:00

69 lines
1.6 KiB
JavaScript

const CACHE_NAME = 'bscscore-v2.1.1';
const APP_SHELL = [
'/',
'/index.html',
'/manifest.webmanifest',
'/favicon.ico',
'/icon-192.png',
'/icon-512.png',
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL)),
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(
keys.map((key) => {
if (key !== CACHE_NAME) {
return caches.delete(key);
}
return Promise.resolve();
}),
),
),
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
const { request } = event;
const requestUrl = new URL(request.url);
if (request.method !== 'GET' || requestUrl.origin !== self.location.origin) {
return;
}
if (request.mode === 'navigate') {
event.respondWith(
fetch(request)
.then((response) => {
const responseClone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, responseClone));
return response;
})
.catch(() => caches.match(request).then((cached) => cached || caches.match('/'))),
);
return;
}
event.respondWith(
caches.match(request).then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse;
}
return fetch(request).then((networkResponse) => {
const networkClone = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, networkClone));
return networkResponse;
});
}),
);
});