1const VERSION = '1.0.1'; 2 3const CACHE_FILES = [ 4 '/pwa/index.html', 5 '/pwa/logo-vgn-frost-hat.svg', 6 '/pwa/rubik-v11-latin_cyrillic-regular.woff', 7 '/pwa/rubik-v11-latin_cyrillic-regular.woff2', 8]; 9 10const HOST_WHITELIST = [ 11 'www.vgoroden.ru' 12]; 13 14self.addEventListener('install', installSW); 15 16self.addEventListener('activate', activateSW); 17 18self.addEventListener('fetch', fetchSW); 19 20function installSW(event) { 21 self.skipWaiting(); 22 23 if (checkHost(self.location.host)) { 24 event.waitUntil(caches.open(VERSION).then(function(cache) { 25 return cache.addAll(CACHE_FILES); 26 })); 27 } 28} 29 30function activateSW(event) { 31 const cacheWhitelist = [VERSION]; 32 33 if (checkHost(self.location.host)) { 34 event.waitUntil( 35 caches.keys().then(function(keyList) { 36 return Promise.all(keyList.map(function(key) { 37 if (cacheWhitelist.indexOf(key) === -1) { 38 return caches.delete(key); 39 } 40 })); 41 }) 42 ); 43 } 44} 45 46function fetchSW(event) { 47 const request = event.request; 48 const reqUrl = new URL(request.url); 49 50 if (isSWRequest(reqUrl)) { 51 event.respondWith(finalResponce(request, reqUrl)); 52 } 53} 54 55/** 56 * ФÑнкÑÐ¸Ñ ÑилÑÑÑаÑии запÑоÑов 57 * ÐÑжна Ð´Ð»Ñ Ñого, ÑÑÐ¾Ð±Ñ SW не обÑабаÑÑвал лиÑнее 58 * 59 * @param url 60 * @returns {boolean} 61 */ 62function isSWRequest(url) { 63 const checkIncludeFile = CACHE_FILES.indexOf(url.pathname) > -1; 64 const checkWhiteHost = checkHost(url.host); 65 66 return checkIncludeFile || checkWhiteHost; 67} 68 69/** 70 * ÐÑовеÑка пÑинадлежноÑÑи url'а наÑÐµÐ¼Ñ Ð¿ÑÐ¸Ð»Ð¾Ð¶ÐµÐ½Ð¸Ñ 71 * @param host 72 * @returns {boolean} 73 */ 74function checkHost(host) { 75 return !!HOST_WHITELIST.find(function(hostWhite) { 76 return host.indexOf(hostWhite) > -1; 77 }); 78} 79 80/** 81 * ФÑнкÑÐ¸Ñ Ð¿Ð¾Ð¸Ñка запÑоÑов в кÑÑе клиенÑа и Ð¸Ñ Ð¾ÑдаÑа 82 * @param request 83 * @param url 84 */ 85function finalResponce(request, url) { 86 const positionFileInCache = CACHE_FILES.indexOf(url.pathname); 87 88 if (positionFileInCache > -1) return caches.match(CACHE_FILES[positionFileInCache]); 89 90 return fetch(request) 91 .catch(function() { 92 return caches.match(CACHE_FILES[0]); 93 }); 94}
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.