summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/force_refresh_worker.js
blob: 2e266b54d7faa2f140c27a356ce7a6408fc560af (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);
      })
  );
});