maxage_test.js (1179B)
1 function synthesizeImage(suffix) { 2 // Serve image-20px for the first page, and image-40px for the second page. 3 return clients 4 .matchAll() 5 .then(clients => { 6 var url = "image-20px.png"; 7 clients.forEach(client => { 8 if (client.url.indexOf("?new") > 0) { 9 url = "image-40px.png"; 10 } 11 client.postMessage({ suffix, url }); 12 }); 13 return fetch(url); 14 }) 15 .then(response => { 16 return response.arrayBuffer(); 17 }) 18 .then(ab => { 19 var headers; 20 if (suffix == "") { 21 headers = { 22 "Content-Type": "image/png", 23 Date: "Tue, 1 Jan 1990 01:02:03 GMT", 24 "Cache-Control": "max-age=1", 25 }; 26 } else { 27 headers = { 28 "Content-Type": "image/png", 29 "Cache-Control": "no-cache", 30 }; 31 } 32 return new Response(ab, { 33 status: 200, 34 headers, 35 }); 36 }); 37 } 38 39 self.addEventListener("fetch", function (event) { 40 if (event.request.url.includes("image.png")) { 41 event.respondWith(synthesizeImage("")); 42 } else if (event.request.url.includes("image2.png")) { 43 event.respondWith(synthesizeImage("2")); 44 } 45 });