summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/fetch-event-within-sw-worker.js
blob: 5903bab968dc47aec8d5a51da1422d1eccbdb076 (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
46
47
48
skipWaiting();

addEventListener('fetch', event => {
  const url = new URL(event.request.url);

  if (url.origin != location.origin) return;

  if (url.pathname.endsWith('/sample.txt')) {
    event.respondWith(new Response('intercepted'));
    return;
  }

  if (url.pathname.endsWith('/sample.txt-inner-fetch')) {
    event.respondWith(fetch('sample.txt'));
    return;
  }

  if (url.pathname.endsWith('/sample.txt-inner-cache')) {
    event.respondWith(
      caches.open('test-inner-cache').then(cache =>
        cache.add('sample.txt').then(() => cache.match('sample.txt'))
      )
    );
    return;
  }

  if (url.pathname.endsWith('/show-notification')) {
    // Copy the currect search string onto the icon url
    const iconURL = new URL('notification_icon.py', location);
    iconURL.search = url.search;

    event.respondWith(
      registration.showNotification('test', {
        icon: iconURL
      }).then(() => registration.getNotifications()).then(notifications => {
        for (const n of notifications) n.close();
        return new Response('done');
      })
    );
    return;
  }

  if (url.pathname.endsWith('/notification_icon.py')) {
    new BroadcastChannel('icon-request').postMessage('yay');
    event.respondWith(new Response('done'));
    return;
  }
});