const CACHE_NAME = 'bsc-student-cache-v1'; // We explicitly cache critical assets to allow the app shell to boot offline. // The data embedding logic from template.html doesn't require separate API calls, // so caching the HTML is sufficient for offline usage of the already-embedded repo. const ASSETS = [ './', './bsc', './assets/logo-bsc.svg', './assets/manifest-bsc.json', 'https://cdn.tailwindcss.com', 'https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@24,400,0,0', 'https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.css', 'https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/theme/dracula.min.css', 'https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.js', 'https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/addon/dialog/dialog.min.css', 'https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/addon/dialog/dialog.min.js', 'https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/addon/search/searchcursor.min.js', 'https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/addon/search/search.min.js', 'https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/addon/search/jump-to-line.min.js' ]; self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS)) ); self.skipWaiting(); }); 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.clients.claim(); }); self.addEventListener('fetch', (event) => { // Strategy: Stale-While-Revalidate for app shell & CDNs event.respondWith( caches.match(event.request).then((cachedResponse) => { const fetchPromise = fetch(event.request).then((networkResponse) => { // Dynamically cache scripts/styles we might have missed if (event.request.method === 'GET' && networkResponse.status === 200) { caches.open(CACHE_NAME).then((cache) => { cache.put(event.request, networkResponse.clone()); }); } return networkResponse; }).catch(() => { // If network fails and we don't have it, fallback silently // (Handled automatically by the catch structure if offline) }); return cachedResponse || fetchPromise; }) ); });