summaryrefslogtreecommitdiff
path: root/public/sw.js
diff options
context:
space:
mode:
Diffstat (limited to 'public/sw.js')
-rw-r--r--public/sw.js26
1 files changed, 26 insertions, 0 deletions
diff --git a/public/sw.js b/public/sw.js
new file mode 100644
index 0000000..f4e6631
--- /dev/null
+++ b/public/sw.js
@@ -0,0 +1,26 @@
+const CACHE = "todo-v1";
+const PRECACHE = ["/manifest.json", "/icons/icon-192.png", "/icons/icon-512.png"];
+
+self.addEventListener("install", (e) => {
+ e.waitUntil(caches.open(CACHE).then((c) => c.addAll(PRECACHE)));
+ self.skipWaiting();
+});
+
+self.addEventListener("activate", (e) => {
+ e.waitUntil(
+ caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))
+ );
+ self.clients.claim();
+});
+
+self.addEventListener("fetch", (e) => {
+ const url = new URL(e.request.url);
+ if (url.pathname.startsWith("/icons/") || url.pathname === "/manifest.json") {
+ e.respondWith(caches.match(e.request).then((r) => r || fetch(e.request)));
+ return;
+ }
+ // Network-first for everything else (SSR pages, API)
+ e.respondWith(
+ fetch(e.request).catch(() => caches.match(e.request))
+ );
+});