-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
54 lines (50 loc) · 1.46 KB
/
service-worker.js
File metadata and controls
54 lines (50 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// service-worker.js
const CACHE_NAME = 'checklist-cache-v2'; // Updated cache version
const urlsToCache = [
'/',
'/index.html',
'/styles.css',
'/script.js',
'/offline.html', // Offline fallback page
'/app.js',
'/manifest.json',
'/icon.jpg',
'Chewy-Regular.ttf',
'https://fonts.googleapis.com/css2?family=Chewy&display=swap', // External font
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css', // Font Awesome
];
// Install service worker and cache resources
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME).then(cache => {
return cache.addAll(urlsToCache);
})
);
});
// Activate service worker and remove old caches
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if (cache !== CACHE_NAME) {
console.log('Deleting old cache:', cache);
return caches.delete(cache);
}
})
);
})
);
});
// Fetch resources from cache when offline
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request).then(response => {
// If the resource is cached, return it; otherwise try to fetch it
return response || fetch(event.request).catch(() => {
// If fetching fails (offline), serve the offline page
return caches.match('/offline.html');
});
})
);
});