Advay-Singh commited on
Commit
a48ecf0
·
verified ·
1 Parent(s): d480542

Update static/service-worker.js

Browse files
Files changed (1) hide show
  1. static/service-worker.js +32 -20
static/service-worker.js CHANGED
@@ -1,20 +1,32 @@
1
- self.addEventListener('install', function(event) {
2
- event.waitUntil(
3
- caches.open('taskbot-cache').then(function(cache) {
4
- return cache.addAll([
5
- '/',
6
- '/static/style.css',
7
- '/static/script.js',
8
- '/static/icon-192.png'
9
- ]);
10
- })
11
- );
12
- });
13
-
14
- self.addEventListener('fetch', function(event) {
15
- event.respondWith(
16
- caches.match(event.request).then(function(response) {
17
- return response || fetch(event.request);
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
+ });