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

// Cross origin request to ensure that an opaque response is used
var prefix = "http://example.com/tests/dom/serviceworkers/test/";

var testReady = new Promise(resolve => {
  self.addEventListener(
    "message",
    m => {
      resolve();
    },
    { once: true }
  );
});

self.addEventListener("install", function (event) {
  var request = new Request(prefix + "notify_loaded.js", { mode: "no-cors" });
  event.waitUntil(
    Promise.all([caches.open(name), fetch(request), testReady]).then(function (
      results
    ) {
      var cache = results[0];
      var response = results[1];
      return cache.put("./sw_clients/does_not_exist.js", response);
    })
  );
});

self.addEventListener("fetch", function (event) {
  event.respondWith(
    caches
      .open(name)
      .then(function (cache) {
        return cache.match(event.request);
      })
      .then(function (response) {
        return response || fetch(event.request);
      })
  );
});