xhr-response-url-worker.js (807B)
1 // Service worker for the xhr-response-url test. 2 3 self.addEventListener('fetch', event => { 4 const url = new URL(event.request.url); 5 const respondWith = url.searchParams.get('respondWith'); 6 if (!respondWith) 7 return; 8 9 if (respondWith == 'fetch') { 10 const target = url.searchParams.get('url'); 11 event.respondWith(fetch(target)); 12 return; 13 } 14 15 if (respondWith == 'string') { 16 const headers = {'content-type': 'text/plain'}; 17 event.respondWith(new Response('hello', {headers})); 18 return; 19 } 20 21 if (respondWith == 'document') { 22 const doc = ` 23 <!DOCTYPE html> 24 <html> 25 <title>hi</title> 26 <body>hello</body> 27 </html>`; 28 const headers = {'content-type': 'text/html'}; 29 event.respondWith(new Response(doc, {headers})); 30 return; 31 } 32 });