1 const version = 'v.444' 2const staticCacheName = version + 'staticfiles'; 3const imageCacheName = 'images'; 4const pagesCacheName = 'pages'; 5 6const cacheList = [ 7 staticCacheName, 8 imageCacheName, 9 pagesCacheName 10]; 11 12addEventListener('install', installEvent => { 13 skipWaiting(); 14 installEvent.waitUntil( 15 caches.open(staticCacheName) 16 .then( staticCache => { 17 // Nice to have 18 staticCache.addAll([ 19 '/images/tt-tulip.444.svg' 20 ]); // end addAll 21 // Must have 22 return staticCache.addAll([ 23 '/application.444.css', 24 '/application.444.js', 25 '/images/tt-logo-2027.444.svg', 26 '/images/tt-logo-vertical.svg' 27 ]); // end return addAll 28 }) // end open then 29 ); // end waitUntil 30}); // end addEventListener 31 32addEventListener('fetch', fetchEvent => { 33 const request = fetchEvent.request; 34 if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') { return } 35 36 // Don't save google resources 37 if (/^(http)(s)?(:\/\/)([a-z]+.)?(google|google-analytics)(\.com\/)/.test(request.url)) { 38 return 39 } 40 // Don't save the mapbox api 41 if (/(http)(s)?(:\/\/)(api\.mapbox\.com\/v\d)/.test(request.url)) { 42 return 43 } 44 // Don't save admin stuff 45 if (/\?p=(admin|actions\/debug)/.test(request.url)) { 46 return 47 } 48 // Don't save facebook, google, or twitter resources 49 if (/^(http)(s)?(:\/\/)([a-z]+.)?(facebook|google|google-analytics|googletagmanager|twitter)(\.com|\.net)(\/)/.test(request.url)) { 50 return 51 } 52 // Don't save weird tracker pixel stuff 53 if (/^(http)(s)?(:\/\/)(action\.)?(dstillery\.com)/.test(request.url)) { 54 return 55 } 56 57 // Don't save PDFs 58 var pdf = new RegExp('\\.pdf($|\\?)', 'i'); 59 var asset = new RegExp('/assets/'); 60 61 // Fixes a Chrome devtools issue 62 // See: https://github.com/paulirish/caltrainschedule.io/pull/51/commits/82d03d9c4468681421321db571d978d6adea45a7 63 if (request.destination == "document" && asset.test(request.url) && pdf.test(request.url)) { 64 return 65 } 66 // When the user requests an HTML file 67 if (request.headers.get('Accept').includes('text/html')) { 68 fetchEvent.respondWith( 69 // Fetch that page from the network 70 fetch(request) 71 .then( responseFromFetch => { 72 if (responseFromFetch.ok || responseFromFetch.redirected) { 73 // Put a copy in the cache 74 const copy = responseFromFetch.clone(); 75 fetchEvent.waitUntil( 76 caches.open(pagesCacheName) 77 .then( pagesCache => { 78 return pagesCache.put(request, copy); 79 }) // end open then 80 ); // end waitUntil 81 } 82 return responseFromFetch; 83 }) // end fetch then 84 .catch( error => { 85 // Otherwise look for a cached version of the page 86 return caches.match(request) 87 .then( responseFromCache => { 88 if (responseFromCache && (responseFromCache.ok || responseFromCache.redirected)) { 89 return responseFromCache; 90 } // end if 91 // Otherwise show the fallback page 92 return caches.match('/offline'); 93 }); // end match then and return 94 }) // end fetch catch 95 ); // end respondWith 96 return; // Go no further 97 } // end if 98 99 // When the user requests an image 100 if (request.headers.get('Accept').includes('image')) { 101 fetchEvent.respondWith( 102 // Look for a cached version of the image 103 caches.match(request) 104 .then( responseFromCache => { 105 if (responseFromCache && (responseFromCache.ok || responseFromCache.redirected)) { 106 return responseFromCache; 107 } // end if 108 // Otherwise fetch the image from the network 109 return fetch(request) 110 .then( responseFromFetch => { 111 if (responseFromFetch.ok || responseFromFetch.redirected) { 112 // Put a copy in the cache 113 const copy = responseFromFetch.clone(); 114 fetchEvent.waitUntil( 115 caches.open(imageCacheName) 116 .then( imageCache => { 117 return imageCache.put(request, copy); 118 }) // end open then 119 ); // end waitUntil 120 } 121 return responseFromFetch; 122 }) // end fetch then 123 .catch( error => { 124 // Otherwise show a fallback image 125 return caches.match('/fallback.svg'); 126 }); // end fetch catch and return 127 }) // end match then 128 ); // end respondWith 129 return; // Go no further 130 } // end if 131 132 // For everything else... 133 fetchEvent.respondWith( 134 // Look for a cached version of the file 135 caches.match(request) 136 .then( responseFromCache => { 137 if (responseFromCache) { 138 return responseFromCache; 139 } // end if 140 // Otherwise fetch the file from the network 141 return fetch(request); 142 }) // end match then 143 ); // end respondWith 144}); // end addEventListener 145 146addEventListener('activate', activateEvent => { 147 activateEvent.waitUntil( 148 caches.keys() 149 .then( cacheNames => { 150 return Promise.all( 151 cacheNames.map( cacheName => { 152 if (!cacheList.includes(cacheName)) { 153 return caches.delete(cacheName); 154 } // end if 155 }) // end map 156 ); // end return Promise.all 157 }) // end keys then 158 .then( () => { 159 return clients.claim(); 160 }) // end then 161 ); // end waitUntil 162}); // end addEventListener 163
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.