Files
taskboard/sw.js
T

31 lines
1.0 KiB
JavaScript

const CACHE = "taskboard-v1";
const SHELL = ["./", "index.html", "manifest.webmanifest", "icon-192.png", "icon-512.png", "apple-touch-icon.png"];
self.addEventListener("install", (e) => {
e.waitUntil(caches.open(CACHE).then((c) => c.addAll(SHELL)).then(() => self.skipWaiting()));
});
self.addEventListener("activate", (e) => {
e.waitUntil(
caches.keys()
.then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))
.then(() => self.clients.claim())
);
});
// Network-first for same-origin shell so updates show up; cache fallback offline.
// GitHub API calls pass through untouched.
self.addEventListener("fetch", (e) => {
const url = new URL(e.request.url);
if (url.origin !== location.origin || e.request.method !== "GET") return;
e.respondWith(
fetch(e.request)
.then((res) => {
const copy = res.clone();
caches.open(CACHE).then((c) => c.put(e.request, copy));
return res;
})
.catch(() => caches.match(e.request, { ignoreSearch: true }))
);
});