File size: 897 Bytes
a48ecf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// static/service-worker.js
const CACHE_NAME = "taskbot-v1-cache-v1";
const toCache = [
  "/",                        // cache root HTML
  "/static/icons/icon-192.png",
  "/static/icons/icon-512.png",
  // (any CSS/JS files you serve statically, if you want them offline)
];

self.addEventListener("install", (event) => {
  event.waitUntil(
    caches.open(CACHE_NAME).then((cache) => cache.addAll(toCache))
  );
});

self.addEventListener("fetch", (event) => {
  // Try cache first, then network
  event.respondWith(
    caches.match(event.request).then((cachedResp) => {
      return (
        cachedResp ||
        fetch(event.request).then((networkResp) => {
          // (Optionally) update the cache
          caches.open(CACHE_NAME).then((cache) => {
            cache.put(event.request, networkResp.clone());
          });
          return networkResp;
        })
      );
    })
  );
});