arnaucube
arnaucube/macrosPublic Self-hosted repository
macros/sw.js
sw.jsRaw
 1const CACHE_NAME = "macros-v12";
 2const ASSETS = [
 3  "/",
 4  "/index.html",
 5  "/styles.css",
 6  "/app.js",
 7  "/foods.json",
 8  "/manifest.json",
 9  "/icons/logo.svg",
10  "/icons/icon-192.svg",
11  "/icons/icon-512.svg"
12];
13
14self.addEventListener("install", (event) => {
15  event.waitUntil(
16    caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS))
17  );
18  self.skipWaiting();
19});
20
21self.addEventListener("activate", (event) => {
22  event.waitUntil(
23    caches.keys().then((keys) =>
24      Promise.all(
25        keys.map((key) => {
26          if (key !== CACHE_NAME) return caches.delete(key);
27          return null;
28        })
29      )
30    )
31  );
32  self.clients.claim();
33});
34
35self.addEventListener("fetch", (event) => {
36  if (event.request.method !== "GET") return;
37
38  event.respondWith(
39    caches.match(event.request).then((cached) => {
40      if (cached) return cached;
41      return fetch(event.request).then((response) => {
42        const copy = response.clone();
43        caches.open(CACHE_NAME).then((cache) => {
44          cache.put(event.request, copy);
45        });
46        return response;
47      });
48    })
49  );
50});