1const cacheName = 'v2'; 2 3caches.delete('prefetch'); 4caches.delete('v1'); 5 6self.addEventListener('install', (ev) => { 7 caches.open(cacheName).then((cache) => { 8 cache.add('/assets/img/icons/symbol-defs.svg'); 9 }); 10 self.skipWaiting(); 11}); 12 13self.addEventListener('fetch', (ev) => { 14 const url = ev.request.url; 15 const destination = ev.request.destination; 16 17 const isAdmin = [ 18 url.indexOf('admin') < 0, 19 url.indexOf('console') < 0, 20 url.indexOf('libraries') < 0, 21 url.indexOf('aurora') < 0, 22 url.indexOf(self.location.origin) === 0 23 ]; 24 25 if (isAdmin.indexOf(false) >= 0) { 26 return; 27 } 28 29 const searchParams = new URL(url).search; 30 31 const isPrefetch = ev.request.mode === 'cors' && ev.request.method === 'GET' && ev.request.headers.get('x-sec-purpose') === 'prefetch'; 32 33 const conditions = [ 34 ev.request.method === 'GET', 35 searchParams === '', 36 url.indexOf(self.location.origin) === 0, 37 destination === 'script' || destination === 'style' || destination === 'image' || destination === 'font' || url.endsWith('.json') 38 ]; 39 40 if (isPrefetch) { 41 ev.respondWith( 42 caches.open('prefetch').then((cache) => { 43 return cache.match(ev.request).then((cacheResponse) => { 44 if (cacheResponse) { 45 return cacheResponse; 46 } 47 48 return fetch(ev.request).then((networkResponse) => { 49 cache.put(ev.request, networkResponse.clone()); 50 51 setTimeout(() => { 52 cache.delete(ev.request); 53 }, 60000); 54 55 return networkResponse; 56 }); 57 }); 58 }) 59 ); 60 } else if (conditions.indexOf(false) < 0) { 61 ev.respondWith( 62 caches.open(cacheName).then((cache) => { 63 return cache.match(ev.request).then((cacheResponse) => { 64 if (cacheResponse) { 65 return cacheResponse; 66 } 67 68 return fetch(ev.request) 69 .then((networkResponse) => { 70 cache.put(ev.request, networkResponse.clone()); 71 72 return networkResponse; 73 }) 74 .catch(() => 75 caches.match(ev.request).then((cacheResponse) => { 76 if (cacheResponse) { 77 return cacheResponse; 78 } 79 80 if (navigator.onLine !== true) { 81 return new Response('No internet connection', { 82 status: 408, 83 headers: { 84 'Content-Type': 'text/html', 85 'Accept-Charset': 'utf-8' 86 } 87 }); 88 } 89 }) 90 ); 91 }); 92 }) 93 ); 94 } else { 95 return; 96 } 97}); 98 99self.addEventListener('push', (ev) => { 100 const notificationData = ev.data.json(); 101 102 const promiseChain = registration 103 .getNotifications() 104 .then((notifications) => { 105 let currentNotification; 106 107 for (let i = 0; i < notifications.length; i++) { 108 if (parseInt(notifications[i].tag, 10) === notificationData.type) { 109 currentNotification = notifications[i]; 110 } 111 } 112 113 return currentNotification; 114 }) 115 .then((currentNotification) => { 116 let notificationTitle; 117 const options = { 118 badge: '/libraries/images/webpush/badge/shoper_sygnet.png' 119 }; 120 121 if (currentNotification && currentNotification.data) { 122 const notificationData = currentNotification.data; 123 const count = notificationData.count + 1; 124 125 options.body = notificationData.content.multi.body || ''; 126 options.tag = notificationData.type; 127 options.data = { 128 ...notificationData, 129 count 130 }; 131 notificationTitle = notificationData.content.multi.message.replace('%s', count); 132 133 currentNotification.close(); 134 } else { 135 notificationTitle = notificationData.content.single.message; 136 137 options.body = notificationData.content.single.body || ''; 138 options.tag = notificationData.type; 139 options.data = { 140 ...notificationData, 141 count: 1 142 }; 143 } 144 145 return self.registration.showNotification(notificationTitle, options); 146 }); 147 148 ev.waitUntil(promiseChain); 149}); 150 151self.addEventListener('notificationclick', (ev) => { 152 const notificationData = ev.notification.data; 153 let url; 154 155 if (notificationData.count > 1) { 156 url = `${self.location.origin}/admin/`; 157 158 if (notificationData.type === 1) { 159 url = `${self.location.origin}/admin/orders/`; 160 } 161 162 if (notificationData.type === 2) { 163 url = `${self.location.origin}/admin/orders/list/#/filter_payment_status/1/sort/paid_date/order/desc/page/1`; 164 } 165 } else { 166 url = `${self.location.origin}/admin/orders/view/id/${notificationData.content.id}`; 167 } 168 169 ev.notification.close(); 170 171 ev.waitUntil(self.clients.openWindow(url)); 172});
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.