1'use strict'; 2 3 4/////////////////// 5// VERSIONS 6/////////////////// 7 8const CACHE_VERSION = 'v1.0.16'; 9const CACHE_VERSION_PAGES = 'v1.0.15'; 10const CACHE_THEME = 'agglo-cache-'; 11const CACHE_PAGES = CACHE_THEME + CACHE_VERSION_PAGES + '-pages'; 12const CACHE_ASSETS = CACHE_THEME + CACHE_VERSION + '-assets'; 13 14const maxPages = 50; 15const maxAssets = 1000; 16const timeout = 5000; 17var cachesList = [ 18 CACHE_PAGES, 19 CACHE_ASSETS 20]; 21 22 23/////////////////// 24// INSTALL SERVICE WORKER - PRECACHING 25/////////////////// 26 27function updateCache() { 28 return caches.open(CACHE_ASSETS).then(cache => { 29 return cache.addAll([ 30 '/offline', 31 'https://cdn.agglo-larochelle.fr/fonts/DroidSans-webfont.woff2', 32 'https://cdn.agglo-larochelle.fr/fonts/DroidSans-Bold-webfont.woff2' 33 ]); 34 }) 35} 36 37function cacheClients() { 38 const pages = []; 39 return clients.matchAll({ 40 includeUncontrolled: true 41 }) 42 .then( allClients => { 43 for (const client of allClients) { 44 pages.push(client.url); 45 } 46 }) 47 .then (() => { 48 caches.open(CACHE_PAGES) 49 .then(pagesCache => { 50 return pagesCache.addAll(pages); 51 }); 52 }) 53} 54 55self.addEventListener('install', event => { 56 event.waitUntil( 57 updateCache() 58 .then(() => { 59 cacheClients() 60 }) 61 .then(() => { 62 return skipWaiting(); 63 }) 64 ); 65}); 66 67 68/////////////////// 69// ACTIVATE SERVICE WORKER - DELETING OLD CACHES 70/////////////////// 71 72function clearOldCaches() { 73 return caches.keys() 74 .then( keys => { 75 return Promise.all(keys 76 .filter(key => !cachesList.includes(key)) 77 .map(key => caches.delete(key)) 78 ); 79 }); 80} 81 82self.addEventListener('activate', event => { 83 event.waitUntil( 84 clearOldCaches() 85 .then( () => { 86 return self.clients.claim(); 87 }) 88 ); 89}); 90 91 92/////////////////// 93// CACHE CLEANING - TRIM CACHE 94/////////////////// 95 96function trimCache(cacheName, maxItems) { 97 caches.open(cacheName) 98 .then( cache => { 99 cache.keys() 100 .then(keys => { 101 if (keys.length > maxItems) { 102 cache.delete(keys[0]) 103 .then( () => { 104 trimCache(cacheName, maxItems) 105 }); 106 } 107 }); 108 }); 109} 110 111self.addEventListener('message', event => { 112 if (event.data.command == 'trimCaches') { 113 trimCache(CACHE_PAGES, maxPages); 114 trimCache(CACHE_PAGES, maxAssets); 115 } 116}); 117 118 119/////////////////// 120// FETCH A RESOURCE - REQUEST FROM CACHE OR NETWORK, DEPENDING ON THE SCENARIO 121/////////////////// 122 123self.addEventListener('fetch', event => { 124 125 const request = event.request; 126 if (request.method !== 'GET' || request.url.indexOf('wxs.ign.fr') != -1) { 127 return; 128 } 129 130 // Network first network then cache then custom offline page (for freshness rather than speed) 131 event.respondWith( 132 new Promise(resolveWithResponse => { 133 // Response from network if ok 134 fetch(request).then(responseFromFetch => { 135 // Update cache content 136 const responseCopy = responseFromFetch.clone(); 137 try { 138 if (request.headers.get('Accept') && request.headers.get('Accept').includes('text/html')) { 139 event.waitUntil( 140 caches.open(CACHE_PAGES).then(cache => { 141 return cache.put(request, responseCopy); 142 }) 143 ); 144 } else { 145 var cacheRequest = request; 146 if (request.url.indexOf('.pagespeed.')!=-1) { 147 var cacheUrl = request.url.substring(0,request.url.indexOf('.pagespeed.')); 148 if (cacheUrl.indexOf(',')!=-1) { 149 cacheUrl = cacheUrl.substring(0,cacheUrl.
149indexOf(',')); 150 } 151 cacheRequest = new Request(cacheUrl, { 152 method: request.method, 153 headers: request.headers, 154 mode: 'same-origin', 155 credentials: request.credentials, 156 redirect: 'manual' 157 }); 158 } 159 event.waitUntil( 160 caches.open(CACHE_ASSETS).then(cache => { 161 return cache.put(cacheRequest, responseCopy); 162 }) 163 ); 164 } 165 } catch (error) { 166 console.log(error); 167 } 168 // Display network content 169 resolveWithResponse(responseFromFetch); 170 }) 171 // Response from cache/fallback if no network 172 .catch(fetchError => { 173 var cacheRequest = request; 174 if ((!request.headers.get('Accept') || !request.headers.get('Accept').includes('text/html')) && request.url.indexOf('.pagespeed.')!=-1) { 175 var cacheUrl = request.url.substring(0,request.url.indexOf('.pagespeed.')); 176 if (cacheUrl.indexOf(',')!=-1) { 177 cacheUrl = cacheUrl.substring(0,cacheUrl.indexOf(',')); 178 } 179 cacheRequest = new Request(cacheUrl, { 180 method: request.method, 181 headers: request.headers, 182 mode: 'same-origin', 183 credentials: request.credentials, 184 redirect: 'manual' 185 }); 186 } 187 caches.match(cacheRequest) 188 // Display cache content or offline fallback 189 .then(responseFromCache => { 190 if (cacheRequest.headers.get('Accept') && cacheRequest.headers.get('Accept').includes('text/html')) { 191 resolveWithResponse( responseFromCache || caches.match('/offline') ); 192 } else { 193 resolveWithResponse(responseFromCache); 194 } 195 }) 196 }) 197 }) 198 ) 199 200}); 201 202 203/////////////////// 204// PUSH NOTIFICATIONS 205/////////////////// 206 207self.addEventListener('push', function(event) { 208 209 event.waitUntil( 210 self.registration.pushManager.getSubscription() 211 .then(function(subscription) { 212 var jsonData = event.data.json(); // obtenir de data les informations de la notification pour l'afficher 213 var notificationOptions = { 214 body: jsonData.notification.body, 215 icon: jsonData.notification.icon ? jsonData.notification.icon : 'https://www.agglo-larochelle.fr/cdalr-new-theme/images/social-card.jpg', 216 tag : jsonData.notification.tag, 217 data:{ 218 url : jsonData.data.url 219 } 220 }; 221 title = jsonData.notification.title; 222 return self.registration.showNotification(title, notificationOptions); 223 224 }) 225 .catch(function(err) { 226 //gestion de l'erreur de récupération de l'inscription 227 }) 228 ); 229}); 230 231self.addEventListener('notificationclick', function(event) { 232 var url = event.notification.data.url; 233 event.notification.close(); 234 if (url){ 235 event.waitUntil(clients.openWindow(url)); 236 } 237});
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.