49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
const CACHE_NAME = 'nexus-timer-cache-v1';
|
|
const urlsToCache = [
|
|
'/',
|
|
'/index.html',
|
|
'/manifest.json',
|
|
'/favicon.ico',
|
|
'/icons/icon-192x192.png',
|
|
'/icons/icon-512x512.png',
|
|
// Add other static assets here, like JS/CSS bundles if not dynamically named
|
|
// Vite typically names bundles with hashes, so caching them directly might be tricky
|
|
// For a PWA, focus on caching the app shell and key static assets
|
|
];
|
|
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => {
|
|
console.log('Opened cache');
|
|
return cache.addAll(urlsToCache);
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', event => {
|
|
event.respondWith(
|
|
caches.match(event.request)
|
|
.then(response => {
|
|
if (response) {
|
|
return response; // Serve from cache
|
|
}
|
|
return fetch(event.request); // Fetch from network
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', event => {
|
|
const cacheWhitelist = [CACHE_NAME];
|
|
event.waitUntil(
|
|
caches.keys().then(cacheNames => {
|
|
return Promise.all(
|
|
cacheNames.map(cacheName => {
|
|
if (cacheWhitelist.indexOf(cacheName) === -1) {
|
|
return caches.delete(cacheName);
|
|
}
|
|
})
|
|
);
|
|
})
|
|
);
|
|
}); |