5bdea62a9f
Wire up a production service worker and web app manifest with install metadata and icon assets. Bump project version to 2.1.0 and register the worker from the main page for offline-ready behavior. Made-with: Cursor
69 lines
1.6 KiB
JavaScript
69 lines
1.6 KiB
JavaScript
const CACHE_NAME = 'bscscore-v2.1.0';
|
|
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;
|
|
});
|
|
}),
|
|
);
|
|
});
|