Spaces:
Running
Running
| // STEM Copilot — Service Worker (PWA installability + basic cache) | |
| const CACHE_NAME = 'stemcopilot-v2'; | |
| const SHELL_URLS = [ | |
| '/', | |
| '/static/style.css', | |
| '/static/app.js', | |
| '/assets/bot.png', | |
| '/assets/stem_black.png', | |
| '/assets/stembotix.png', | |
| ]; | |
| self.addEventListener('install', (e) => { | |
| e.waitUntil( | |
| caches.open(CACHE_NAME).then((cache) => cache.addAll(SHELL_URLS)) | |
| ); | |
| self.skipWaiting(); | |
| }); | |
| self.addEventListener('activate', (e) => { | |
| e.waitUntil( | |
| caches.keys().then((keys) => | |
| Promise.all(keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k))) | |
| ) | |
| ); | |
| self.clients.claim(); | |
| }); | |
| self.addEventListener('fetch', (e) => { | |
| // Network-first for API calls, cache-first for shell assets | |
| if (e.request.url.includes('/chat') || e.request.url.includes('/auth') || | |
| e.request.url.includes('/threads') || e.request.url.includes('/history') || | |
| e.request.url.includes('/user') || e.request.url.includes('/export') || | |
| e.request.url.includes('/health')) { | |
| return; // Let these go straight to network | |
| } | |
| e.respondWith( | |
| caches.match(e.request).then((cached) => cached || fetch(e.request)) | |
| ); | |
| }); | |