-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
58 lines (54 loc) · 1.61 KB
/
Copy pathsw.js
File metadata and controls
58 lines (54 loc) · 1.61 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
/* Simple Basic Service Worker (sw.js) */
const CACHE_NAME = 'globalstream-v1';
const ASSETS_TO_CACHE = [
'/',
'./index.html',
'./manifest.json',
'https://cdn.tailwindcss.com',
'https://cdn.jsdelivr.net/npm/hls.js@latest',
'https://unpkg.com/lucide@latest',
'https://unpkg.com/react@18/umd/react.production.min.js',
'https://unpkg.com/react-dom@18/umd/react-dom.production.min.js',
'https://unpkg.com/@babel/standalone/babel.min.js',
'./assets/icon-192.png',
'./assets/icon-512.png'
];
// Installation event: Cache static assets
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('ServiceWorker: Caching essential assets');
return cache.addAll(ASSETS_TO_CACHE);
})
);
});
// Activation event: Clean up old caches
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if (cache !== CACHE_NAME) {
console.log('ServiceWorker: Clearing old cache:', cache);
return caches.delete(cache);
}
})
);
})
);
});
// Fetch event: Serve cached assets, otherwise fetch from network
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(response => {
// Cache hit: Return response
if (response) {
return response;
}
// Cache miss: Fetch and return from network
return fetch(event.request);
})
);
});