-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
65 lines (61 loc) · 1.79 KB
/
Copy pathservice-worker.js
File metadata and controls
65 lines (61 loc) · 1.79 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
55
56
57
58
59
60
61
62
63
64
65
const CACHE_NAME = 'breathe-please';
const ASSETS_TO_CACHE = [
'./',
'./index.html',
'./mantra-om.mp3',
'./icon-512.png',
'./icon-192.png',
'./onebell.mp3',
'./icon-192-maskable.png',
'./favicon.ico',
'./apple-touch-icon.png'
];
// Install Event: Cache core assets immediately
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log('[Service Worker] Caching core assets');
return cache.addAll(ASSETS_TO_CACHE);
})
);
self.skipWaiting();
});
// Activate Event: Clean up old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(
keyList.map((key) => {
if (key !== CACHE_NAME) {
console.log('[Service Worker] Removing old cache', key);
return caches.delete(key);
}
})
);
})
);
self.clients.claim();
});
// Fetch Event: Handle requests
self.addEventListener('fetch', (event) => {
// Handle Google Fonts (cache them dynamically)
if (event.request.url.includes('fonts.googleapis.com') || event.request.url.includes('fonts.gstatic.com')) {
event.respondWith(
caches.open(CACHE_NAME).then((cache) => {
return cache.match(event.request).then((response) => {
return response || fetch(event.request).then((networkResponse) => {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
});
})
);
return;
}
// Handle App Assets (Cache First, fall back to Network)
event.respondWith(
caches.match(event.request).then((response) => {
return response || fetch(event.request);
})
);
});