Spaces:
Running
Running
Update static/service-worker.js
Browse files- static/service-worker.js +32 -20
static/service-worker.js
CHANGED
|
@@ -1,20 +1,32 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// static/service-worker.js
|
| 2 |
+
const CACHE_NAME = "taskbot-v1-cache-v1";
|
| 3 |
+
const toCache = [
|
| 4 |
+
"/", // cache root HTML
|
| 5 |
+
"/static/icons/icon-192.png",
|
| 6 |
+
"/static/icons/icon-512.png",
|
| 7 |
+
// (any CSS/JS files you serve statically, if you want them offline)
|
| 8 |
+
];
|
| 9 |
+
|
| 10 |
+
self.addEventListener("install", (event) => {
|
| 11 |
+
event.waitUntil(
|
| 12 |
+
caches.open(CACHE_NAME).then((cache) => cache.addAll(toCache))
|
| 13 |
+
);
|
| 14 |
+
});
|
| 15 |
+
|
| 16 |
+
self.addEventListener("fetch", (event) => {
|
| 17 |
+
// Try cache first, then network
|
| 18 |
+
event.respondWith(
|
| 19 |
+
caches.match(event.request).then((cachedResp) => {
|
| 20 |
+
return (
|
| 21 |
+
cachedResp ||
|
| 22 |
+
fetch(event.request).then((networkResp) => {
|
| 23 |
+
// (Optionally) update the cache
|
| 24 |
+
caches.open(CACHE_NAME).then((cache) => {
|
| 25 |
+
cache.put(event.request, networkResp.clone());
|
| 26 |
+
});
|
| 27 |
+
return networkResp;
|
| 28 |
+
})
|
| 29 |
+
);
|
| 30 |
+
})
|
| 31 |
+
);
|
| 32 |
+
});
|