summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/force_refresh_worker.js
blob: 8c8382493a17a54919f584d917a729fab9c2ea5f (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
var name = "refresherCache";

self.addEventListener("install", function (event) {
  event.waitUntil(
    Promise.all([
      caches.open(name),
      fetch("./sw_clients/refresher_cached.html"),
      fetch("./sw_clients/refresher_cached_compressed.html"),
    ]).then(function (results) {
      var cache = results[0];
      var response = results[1];
      var compressed = results[2];
      return Promise.all([
        cache.put("./sw_clients/refresher.html", response),
        cache.put("./sw_clients/refresher_compressed.html", compressed),
      ]);
    })
  );
});

self.addEventListener("fetch", function (event) {
  event.respondWith(
    caches
      .open(name)
      .then(function (cache) {
        return cache.match(event.request);
      })
      .then(function (response) {
        // If this is one of our primary cached responses, then the window
        // must have generated the request via a normal window reload.  That
        // should be detectable in the event.request.cache attribute.
        if (response && event.request.cache !== "no-cache") {
          dump(
            '### ### FetchEvent.request.cache is "' +
              event.request.cache +
              '" instead of expected "no-cache"\n'
          );
          return Response.error();
        }
        return response || fetch(event.request);
      })
  );
});