File size: 994 Bytes
e07524c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const cacheName = "caches-v0.9.8";

self.addEventListener("install", (e) => {
  e.waitUntil(
    caches.open(cacheName)
      .then(cache => cache.addAll(["./", "./index.html", "./icon.png"]))
  )
});

self.addEventListener("fetch", (e) => {
  e.respondWith(
    caches.match(e.request).then(r => {
      if (r) return r;
      return fetch(e.request).then(response => {
        // only cache css & js
        if (/^http.+(\.css|\.js)$/.test(e.request.url) && !/(\/env\.js)$/.test(e.request.url) && response.ok) {
          const cloned = response.clone();
          caches.open(cacheName).then(cache => {
            cache.put(e.request, cloned);
          })
        };
        return response;
      })
    })
  )
});

self.addEventListener("activate", (e) => {
  e.waitUntil(
    caches.keys().then((keyList) => {
      return Promise.all(
        keyList.map((key) => {
          if (key !== cacheName) {
            return caches.delete(key);
          }
        })
      )
    })
  )
});