Spaces:
Running
Running
| const CACHE_NAME = 'zenith-v1'; | |
| const ASSETS_TO_CACHE = [ | |
| '/', | |
| '/index.html', | |
| '/favicon.ico', | |
| '/manifest.json', | |
| '/vite.svg' | |
| ]; | |
| self.addEventListener('install', (event) => { | |
| event.waitUntil( | |
| caches.open(CACHE_NAME).then((cache) => { | |
| return cache.addAll(ASSETS_TO_CACHE); | |
| }) | |
| ); | |
| }); | |
| self.addEventListener('activate', (event) => { | |
| event.waitUntil( | |
| caches.keys().then((cacheNames) => { | |
| return Promise.all( | |
| cacheNames.map((cacheName) => { | |
| if (cacheName !== CACHE_NAME) { | |
| return caches.delete(cacheName); | |
| } | |
| }) | |
| ); | |
| }) | |
| ); | |
| }); | |
| self.addEventListener('fetch', (event) => { | |
| // Only handle GET requests | |
| if (event.request.method !== 'GET') return; | |
| // SPA fallback for navigation requests | |
| if (event.request.mode === 'navigate') { | |
| event.respondWith( | |
| fetch(event.request).catch(() => { | |
| return caches.match('/index.html'); | |
| }) | |
| ); | |
| return; | |
| } | |
| // Cache-first for static assets | |
| event.respondWith( | |
| caches.match(event.request).then((response) => { | |
| return response || fetch(event.request).then((fetchResponse) => { | |
| // Don't cache API calls or external resources unless necessary | |
| const url = new URL(event.request.url); | |
| if (url.origin === self.location.origin && url.pathname.startsWith('/assets/')) { | |
| return caches.open(CACHE_NAME).then((cache) => { | |
| cache.put(event.request, fetchResponse.clone()); | |
| return fetchResponse; | |
| }); | |
| } | |
| return fetchResponse; | |
| }); | |
| }) | |
| ); | |
| }); | |