Spaces:
Sleeping
Sleeping
| const CACHE_NAME = 'ri-v4'; | |
| const STATIC_ASSETS = [ | |
| '/static/style.css', | |
| '/static/htmx.min.js', | |
| '/static/favicon.svg', | |
| '/static/manifest.json', | |
| ]; | |
| // Install: pre-cache static assets | |
| self.addEventListener('install', (event) => { | |
| event.waitUntil( | |
| caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS)) | |
| ); | |
| self.skipWaiting(); | |
| }); | |
| // Activate: clean up old caches | |
| self.addEventListener('activate', (event) => { | |
| event.waitUntil( | |
| caches.keys().then((keys) => | |
| Promise.all(keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k))) | |
| ) | |
| ); | |
| self.clients.claim(); | |
| }); | |
| // Fetch: network-first for pages, cache-first for static assets | |
| self.addEventListener('fetch', (event) => { | |
| const url = new URL(event.request.url); | |
| // Static assets: cache-first | |
| if (url.pathname.startsWith('/static/')) { | |
| event.respondWith( | |
| caches.match(event.request).then((cached) => { | |
| if (cached) return cached; | |
| return fetch(event.request).then((response) => { | |
| const clone = response.clone(); | |
| caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone)); | |
| return response; | |
| }); | |
| }) | |
| ); | |
| return; | |
| } | |
| // Google Fonts: cache-first | |
| if (url.hostname.includes('fonts.googleapis.com') || url.hostname.includes('fonts.gstatic.com')) { | |
| event.respondWith( | |
| caches.match(event.request).then((cached) => { | |
| if (cached) return cached; | |
| return fetch(event.request).then((response) => { | |
| const clone = response.clone(); | |
| caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone)); | |
| return response; | |
| }); | |
| }) | |
| ); | |
| return; | |
| } | |
| // HTML pages: network-first with cache fallback | |
| if (event.request.mode === 'navigate' || event.request.headers.get('accept')?.includes('text/html')) { | |
| event.respondWith( | |
| fetch(event.request) | |
| .then((response) => { | |
| const clone = response.clone(); | |
| caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone)); | |
| return response; | |
| }) | |
| .catch(() => caches.match(event.request).then((cached) => cached || caches.match('/'))) | |
| ); | |
| return; | |
| } | |
| // Everything else: network with cache fallback | |
| event.respondWith( | |
| fetch(event.request).catch(() => caches.match(event.request)) | |
| ); | |
| }); | |