const CACHE_NAME = 'bjorn-wiki-v2'; const STATIC_ASSETS = [ './', './index.html', './config.js', './manifest.json', './assets/bjorn.png', 'https://cdn.tailwindcss.com', 'https://unpkg.com/lucide@latest', 'https://cdn.jsdelivr.net/npm/marked/marked.min.js', 'https://cdnjs.cloudflare.com/ajax/libs/dompurify/3.0.6/purify.min.js', 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css', 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js', 'https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;700&family=Inter:wght@400;500;600&display=swap' ]; // Install Event - Cache Static Assets self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => { console.log('[SW] Caching static assets'); return cache.addAll(STATIC_ASSETS); }) ); self.skipWaiting(); }); // Activate Event - Clean up old caches self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((cacheNames) => { return Promise.all( cacheNames.map((cacheName) => { if (cacheName !== CACHE_NAME) { console.log('[SW] Deleting old cache:', cacheName); return caches.delete(cacheName); } }) ); }) ); self.clients.claim(); }); // Fetch Event - Stale-While-Revalidate Strategy self.addEventListener('fetch', (event) => { // Skip non-GET requests if (event.request.method !== 'GET') return; // Strategy: Stale-While-Revalidate for most assets event.respondWith( caches.open(CACHE_NAME).then((cache) => { return cache.match(event.request).then((response) => { const fetchPromise = fetch(event.request).then((networkResponse) => { // Cache the new response if (networkResponse.ok) { cache.put(event.request, networkResponse.clone()); } return networkResponse; }).catch(() => { // If network fails, we already returned the cached response if it exists }); return response || fetchPromise; }); }) ); });