1const CACHE_NAME = 'smartscheduler-v1'; 2const STATIC_ASSETS = [ 3 '/', 4 '/favicon.svg', 5 '/manifest.json', 6]; 7 8// Install: cache static assets 9self.addEventListener('install', (event) => { 10 event.waitUntil( 11 caches.open(CACHE_NAME).then((cache) => { 12 return cache.addAll(STATIC_ASSETS); 13 }) 14 ); 15 self.skipWaiting(); 16}); 17 18// Activate: clean up old caches 19self.addEventListener('activate', (event) => { 20 event.waitUntil( 21 caches.keys().then((cacheNames) => { 22 return Promise.all( 23 cacheNames 24 .filter((name) => name !== CACHE_NAME) 25 .map((name) => caches.delete(name)) 26 ); 27 }) 28 ); 29 self.clients.claim(); 30}); 31 32// Fetch: network-first for API, cache-first for static assets 33self.addEventListener('fetch', (event) => { 34 const { request } = event; 35 const url = new URL(request.url); 36 37 // Skip non-GET requests 38 if (request.method !== 'GET') return; 39 40 // Skip Chrome extensions and non-http(s) schemes 41 if (!url.protocol.startsWith('http')) return; 42 43 // API requests: network-first with fallback 44 if (url.pathname.startsWith('/api/')) { 45 event.respondWith( 46 fetch(request) 47 .then((response) => { 48 // Cache successful GET API responses for offline use 49 if (response.ok) { 50 const clone = response.clone(); 51 caches.open(CACHE_NAME).then((cache) => { 52 cache.put(request, clone); 53 }); 54 } 55 return response; 56 }) 57 .catch(() => { 58 return caches.match(request).then((cached) => { 59 if (cached) return cached; 60 return new Response(JSON.stringify({ message: 'You are offline' }), { 61 status: 503, 62 headers: { 'Content-Type': 'application/json' }, 63 }); 64 }); 65 }) 66 ); 67 return; 68 } 69 70 // Static assets: stale-while-revalidate 71 event.respondWith( 72 caches.match(request).then((cached) => { 73 const fetchPromise = fetch(request) 74 .then((response) => { 75 if (response.ok) { 76 const clone = response.clone(); 77 caches.open(CACHE_NAME).then((cache) => { 78 cache.put(request, clone); 79 }); 80 } 81 return response; 82 }) 83 .catch(() => cached); 84 85 return cached || fetchPromise; 86 }) 87 ); 88});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.