fetch-event-within-sw-worker.js (1287B)
1 skipWaiting(); 2 3 addEventListener('fetch', event => { 4 const url = new URL(event.request.url); 5 6 if (url.origin != location.origin) return; 7 8 if (url.pathname.endsWith('/sample.txt')) { 9 event.respondWith(new Response('intercepted')); 10 return; 11 } 12 13 if (url.pathname.endsWith('/sample.txt-inner-fetch')) { 14 event.respondWith(fetch('sample.txt')); 15 return; 16 } 17 18 if (url.pathname.endsWith('/sample.txt-inner-cache')) { 19 event.respondWith( 20 caches.open('test-inner-cache').then(cache => 21 cache.add('sample.txt').then(() => cache.match('sample.txt')) 22 ) 23 ); 24 return; 25 } 26 27 if (url.pathname.endsWith('/show-notification')) { 28 // Copy the currect search string onto the icon url 29 const iconURL = new URL('notification_icon.py', location); 30 iconURL.search = url.search; 31 32 event.respondWith( 33 registration.showNotification('test', { 34 icon: iconURL 35 }).then(() => registration.getNotifications()).then(notifications => { 36 for (const n of notifications) n.close(); 37 return new Response('done'); 38 }) 39 ); 40 return; 41 } 42 43 if (url.pathname.endsWith('/notification_icon.py')) { 44 new BroadcastChannel('icon-request').postMessage('yay'); 45 event.respondWith(new Response('done')); 46 return; 47 } 48 });