1const CACHE_NAME = 'toolsaid-v2'; 2const ASSETS_TO_CACHE = [ 3 '/', 4 '/static/css/style.css', 5 '/static/js/script.js', 6 '/static/img/quickdev.png', 7 '/static/img/favicon.ico', 8 '/offline' 9]; 10 11// Install Event - Cache Core Assets 12self.addEventListener('install', (event) => { 13 event.waitUntil( 14 caches.open(CACHE_NAME) 15 .then((cache) => { 16 return cache.addAll(ASSETS_TO_CACHE); 17 }) 18 .then(() => self.skipWaiting()) 19 ); 20}); 21 22// Activate Event - Clean up old caches 23self.addEventListener('activate', (event) => { 24 event.waitUntil( 25 caches.keys().then((cacheNames) => { 26 return Promise.all( 27 cacheNames.map((cache) => { 28 if (cache !== CACHE_NAME) { 29 return caches.delete(cache); 30 } 31 }) 32 ); 33 }).then(() => self.clients.claim()) 34 ); 35}); 36 37// Fetch Event - Stale-while-revalidate for static, Network-first for pages 38self.addEventListener('fetch', (event) => { 39 // Skip cross-origin and non-GET requests 40 if (!event.request.url.startsWith(self.location.origin)) return; 41 if (event.request.method !== 'GET') return; 42 // Skip API routes â never cache these 43 if (event.request.url.includes('/api/')) return; 44 45 const isStaticAsset = event.request.url.includes('/static/'); 46 47 if (isStaticAsset) { 48 // Stale-while-revalidate: serve cache immediately, update in background 49 event.respondWith( 50 caches.open(CACHE_NAME).then((cache) => { 51 return cache.match(event.request).then((cachedResponse) => { 52 const fetchPromise = fetch(event.request).then((networkResponse) => { 53 if (networkResponse && networkResponse.status === 200) { 54 cache.put(event.request, networkResponse.clone()); 55 } 56 return networkResponse; 57 }).catch(() => cachedResponse); 58 59 return cachedResponse || fetchPromise; 60 }); 61 }) 62 ); 63 } else { 64 // Network-first for HTML pages 65 event.respondWith( 66 fetch(event.request) 67 .then((response) => { 68 if (!response || response.status !== 200 || response.type !== 'basic') { 69 return response; 70 } 71 const responseToCache = response.clone(); 72 caches.open(CACHE_NAME).then((cache) => { 73 cache.put(event.request, responseToCache); 74 }); 75 return response; 76 }) 77 .catch(() => { 78 return caches.match(event.request).then((response) => { 79 if (response) return response; 80 if (event.request.mode === 'navigate') { 81 return caches.match('/offline'); 82 } 83 }); 84 }) 85 ); 86 } 87});
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.