summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/opaque-script-sw.js
blob: 4d882c617d8dc6b790a51b82f3edeb65e7f18d22 (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
importScripts('test-helpers.sub.js');
importScripts('/common/get-host-info.sub.js');

const NAME = 'foo';
const SAME_ORIGIN_BASE = new URL('./', self.location.href).href;
const CROSS_ORIGIN_BASE = new URL('./',
    get_host_info().HTTPS_REMOTE_ORIGIN + base_path()).href;

const urls = [
  `${SAME_ORIGIN_BASE}opaque-script-small.js`,
  `${SAME_ORIGIN_BASE}opaque-script-large.js`,
  `${CROSS_ORIGIN_BASE}opaque-script-small.js`,
  `${CROSS_ORIGIN_BASE}opaque-script-large.js`,
];

self.addEventListener('install', evt => {
  evt.waitUntil(async function() {
    const c = await caches.open(NAME);
    const promises = urls.map(async function(u) {
      const r = await fetch(u, { mode: 'no-cors' });
      await c.put(u, r);
    });
    await Promise.all(promises);
  }());
});

self.addEventListener('fetch', evt => {
  const url = new URL(evt.request.url);
  if (!url.pathname.includes('opaque-script-small.js') &&
      !url.pathname.includes('opaque-script-large.js')) {
    return;
  }
  evt.respondWith(async function() {
    const c = await caches.open(NAME);
    return c.match(evt.request);
  }());
});