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