1const PRECACHE = 'pre-cache' 2const RUNTIME = 'runtime-cache' 3 4// A list of local resources we always want to be cached. 5const PRECACHE_URLS = [ 6 '/offline/' 7] 8 9const OFFLINE_URL = [ 10 '/offline/' 11] 12 13// The install handler takes care of precaching the resources we always need. 14self.addEventListener('install', event => { 15 event.waitUntil( 16 caches.open(PRECACHE) 17 .then(cache => cache.addAll(PRECACHE_URLS)) 18 .then(self.skipWaiting()) 19 ) 20}) 21 22// The activate handler takes care of cleaning up old caches. 23self.addEventListener('activate', event => { 24 const currentCaches = [PRECACHE, RUNTIME] 25 event.waitUntil( 26 caches.keys().then(cacheNames => { 27 return cacheNames.filter(cacheName => !currentCaches.includes(cacheName)) 28 }).then(cachesToDelete => { 29 return Promise.all(cachesToDelete.map(cacheToDelete => { 30 return caches.delete(cacheToDelete) 31 })) 32 }).then(() => self.clients.claim()) 33 ) 34}) 35 36// The fetch handler serves responses for same-origin resources from a cache. 37// If no response is found, it populates the runtime cache with the response 38// from the network before returning it to the page. 39self.addEventListener('fetch', event => { 40 // Skip cross-origin requests, like those for Google Analytics. 41 if (event.request.url.startsWith(self.location.origin)) { 42 // Exclude certain paths from caching 43 if (event.request.url.includes('/ghost') || // Ghost Admin 44 event.request.url.includes('/signin') || // Signin Page 45 event.request.url.includes('/signup') || // Signin Page 46 event.request.url.includes('/account') || // Signin Page 47 event.request.url.includes('/members')) // Members 48 { 49 return false 50 } 51 52 event.respondWith( 53 caches.match(event.request).then(cachedResponse => { 54 return caches.open(RUNTIME).then(cache => { 55 return fetch(event.request).then(response => { 56 // Put a copy of the response in the runtime cache. 57 return cache.put(event.request, response.clone()).then(() => { 58 return response 59 }) 60 }).catch(error => { 61 if (cachedResponse) { 62 return cachedResponse 63 } else { 64 // Check if the user is offline first and is trying to navigate to a web page 65 return caches.match(OFFLINE_URL) 66 } 67 }) 68 }) 69 }) 70 ) 71 } 72})
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.