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 43 // Exclude certain paths from caching 44 if ( event.request.url.includes('/ghost') || // Ghost Admin 45 event.request.url.includes('/signin') || // Signin Page 46 event.request.url.includes('/signup') || // Signin Page 47 event.request.url.includes('/account') || // Signin Page 48 event.request.url.includes('/members') ) // Members 49 { 50 return false 51 } 52 53 event.respondWith( 54 caches.match(event.request).then(cachedResponse => { 55 return caches.open(RUNTIME).then(cache => { 56 return fetch(event.request).then(response => { 57 // Put a copy of the response in the runtime cache. 58 return cache.put(event.request, response.clone()).then(() => { 59 return response; 60 }); 61 }).catch(error => { 62 if (cachedResponse) { 63 return cachedResponse; 64 } else { 65 // Check if the user is offline first and is trying to navigate to a web page 66 return caches.match(OFFLINE_URL); 67 } 68 }); 69 }); 70 }) 71 ); 72 } 73});
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.