1const CACHE_NAME = 'pb-pwa-cache-v5'; 2 3// Core static assets to pre-cache immediately 4const PRECACHE_ASSETS = [ 5 '/', 6 '/assets/css/style.css?v=3.6', 7 '/assets/js/app.js?v=2.4', 8 '/assets/img/parivahan-india.png', 9 '/assets/img/favicon.ico', 10 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Outfit:wght@600;700;800;900&display=swap' 11]; 12 13// Install Event - Pre-cache core shell assets 14self.addEventListener('install', event => { 15 event.waitUntil( 16 caches.open(CACHE_NAME) 17 .then(cache => { 18 console.log('[Service Worker] Pre-caching core application shell'); 19 return cache.addAll(PRECACHE_ASSETS); 20 }) 21 .then(() => self.skipWaiting()) 22 ); 23}); 24 25// Activate Event - Clean up any old caches 26self.addEventListener('activate', event => { 27 event.waitUntil( 28 caches.keys().then(cacheNames => { 29 return Promise.all( 30 cacheNames.map(cache => { 31 if (cache !== CACHE_NAME) { 32 console.log('[Service Worker] Clearing old cache:', cache); 33 return caches.delete(cache); 34 } 35 }) 36 ); 37 }).then(() => self.clients.claim()) 38 ); 39}); 40 41// Fetch Event - Dynamic caching strategies based on request type 42self.addEventListener('fetch', event => { 43 const request = event.request; 44 const url = new URL(request.url); 45 46 // Ignore non-GET requests or external API analytics 47 if (request.method !== 'GET' || url.origin !== self.location.origin) { 48 return; 49 } 50 51 // Strategy 1: Network-First (with offline Cache Fallback) for HTML pages 52 if (request.headers.get('accept').includes('text/html')) { 53 event.respondWith( 54 fetch(request) 55 .then(response => { 56 // Clone the response and cache it 57 const responseClone = response.clone(); 58 caches.open(CACHE_NAME).then(cache => cache.put(request, responseClone)); 59 return response; 60 }) 61 .catch(() => { 62 // If offline, serve the cached version of the request 63 return caches.match(request).then(cachedResponse => { 64 if (cachedResponse) { 65 return cachedResponse; 66 } 67 // Fallback to cached root if page is not cached 68 return caches.match('/'); 69 }); 70 }) 71 ); 72 return; 73 } 74 75 // Strategy 2: Cache-First / Stale-While-Revalidate for CSS, JS, and Static Assets 76 event.respondWith( 77 caches.match(request) 78 .then(cachedResponse => { 79 if (cachedResponse) { 80 // Fetch updated version in the background to update the cache (Stale-While-Revalidate) 81 fetch(request).then(networkResponse => { 82 if (networkResponse.status === 200) { 83 caches.open(CACHE_NAME).then(cache => cache.put(request, networkResponse)); 84 } 85 }).catch(() => {/* Ignore network errors during stale updates */}); 86 87 return cachedResponse; 88 } 89 90 // Fetch from network if not in cache 91 return fetch(request).then(networkResponse => { 92 if (!networkResponse || networkResponse.status !== 200 || networkResponse.type !== 'basic') { 93 return networkResponse; 94 } 95 const responseToCache = networkResponse.clone(); 96 caches.open(CACHE_NAME).then(cache => cache.put(request, responseToCache)); 97 return networkResponse; 98 }); 99 }) 100 ); 101});
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.