fetch-request-css-base-url-worker.js (1659B)
1 let source; 2 let resolveDone; 3 let done = new Promise(resolve => resolveDone = resolve); 4 5 // The page messages this worker to ask for the result. Keep the worker alive 6 // via waitUntil() until the result is sent. 7 self.addEventListener('message', event => { 8 source = event.data.port; 9 source.postMessage('pong'); 10 event.waitUntil(done); 11 }); 12 13 self.addEventListener('fetch', event => { 14 const url = new URL(event.request.url); 15 16 // For the CSS file, respond in a way that may change the response URL, 17 // depending on |url.search|. 18 const cssPath = 'request-url-path/fetch-request-css-base-url-style.css'; 19 if (url.pathname.indexOf(cssPath) != -1) { 20 // Respond with a different URL, deleting "request-url-path/". 21 if (url.search == '?fetch') { 22 event.respondWith(fetch('fetch-request-css-base-url-style.css?fetch')); 23 } 24 // Respond with new Response(). 25 else if (url.search == '?newResponse') { 26 const styleString = 'body { background-image: url("./sample.png");}'; 27 const headers = {'content-type': 'text/css'}; 28 event.respondWith(new Response(styleString, headers)); 29 } 30 } 31 32 // The image request indicates what the base URL of the CSS was. Message the 33 // result back to the test page. 34 else if (url.pathname.indexOf('sample.png') != -1) { 35 // For some reason |source| is undefined here when running the test manually 36 // in Firefox. The test author experimented with both using Client 37 // (event.source) and MessagePort to try to get the test to pass, but 38 // failed. 39 source.postMessage({ 40 url: event.request.url, 41 referrer: event.request.referrer 42 }); 43 resolveDone(); 44 } 45 });