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

self.addEventListener("fetch", function (event) {
  if (event.request.url.includes("image.png")) {
    event.respondWith(synthesizeImage(""));
  } else if (event.request.url.includes("image2.png")) {
    event.respondWith(synthesizeImage("2"));
  }
});