opaque-script-sw.js (1113B)
1 importScripts('test-helpers.sub.js'); 2 importScripts('/common/get-host-info.sub.js'); 3 4 const NAME = 'foo'; 5 const SAME_ORIGIN_BASE = new URL('./', self.location.href).href; 6 const CROSS_ORIGIN_BASE = new URL('./', 7 get_host_info().HTTPS_REMOTE_ORIGIN + base_path()).href; 8 9 const urls = [ 10 `${SAME_ORIGIN_BASE}opaque-script-small.js`, 11 `${SAME_ORIGIN_BASE}opaque-script-large.js`, 12 `${CROSS_ORIGIN_BASE}opaque-script-small.js`, 13 `${CROSS_ORIGIN_BASE}opaque-script-large.js`, 14 ]; 15 16 self.addEventListener('install', evt => { 17 evt.waitUntil(async function() { 18 const c = await caches.open(NAME); 19 const promises = urls.map(async function(u) { 20 const r = await fetch(u, { mode: 'no-cors' }); 21 await c.put(u, r); 22 }); 23 await Promise.all(promises); 24 }()); 25 }); 26 27 self.addEventListener('fetch', evt => { 28 const url = new URL(evt.request.url); 29 if (!url.pathname.includes('opaque-script-small.js') && 30 !url.pathname.includes('opaque-script-large.js')) { 31 return; 32 } 33 evt.respondWith(async function() { 34 const c = await caches.open(NAME); 35 return c.match(evt.request); 36 }()); 37 });