1// https://pwa-workshop.js.org/fr/3-precaching/#preambule-utilisation-des-promesses-et-async-wait 2const preLoad = function () { 3 return caches.open('offline').then(function (cache) { 4 // caching index and important routes 5 return cache.addAll(filesToCache); 6 }); 7}; 8 9self.addEventListener('install', function (event) { 10 event.waitUntil(preLoad()); 11}); 12 13const filesToCache = [ 14 '/', 15 '/offline.html' 16]; 17 18const checkResponse = function (request) { 19 return fetch(request); 20}; 21 22const addToCache = function (request) { 23 return caches.open('offline').then(function (cache) { 24 return fetch(request).then(function (response) { 25 return cache.put(request, response); 26 }); 27 }); 28}; 29 30const returnFromCache = function (request) { 31 return caches.open('offline').then(function (cache) { 32 return cache.match(request).then(function (matching) { 33 if (!matching || matching.status === 404) { 34 return cache.match('offline.html'); 35 } else { 36 return matching; 37 } 38 }); 39 }); 40}; 41 42self.addEventListener('fetch', function (event) { 43 event.respondWith(checkResponse(event.request).catch(function () { 44 return returnFromCache(event.request); 45 })); 46 if (!event.request.url.startsWith('http')) { 47 event.waitUntil(addToCache(event.request)); 48 } 49}); 50 51self.addEventListener('push', function (e) { 52 if (!(self.Notification && self.Notification.permission === 'granted')) { 53 //notifications aren't supported or permission not granted! 54 return; 55 } 56 57 if (e.data) { 58 var msg = e.data.json(); 59 e.waitUntil(self.registration.showNotification(msg.title, { 60 body: msg.body, 61 icon: msg.icon, 62 actions: msg.actions 63 })); 64 } 65}); 66 67self.addEventListener('notificationclick', function (e) { 68 if (!e.action) { 69 return; 70 } 71 e.notification.close(); 72 e.waitUntil(clients.openWindow(e.action)); 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.