-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
59 lines (55 loc) · 1.93 KB
/
Copy pathsw.js
File metadata and controls
59 lines (55 loc) · 1.93 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
const CACHE_NAME = 'tigercal-v5';
const STATIC_ASSETS = [
'/manifest.json',
'/icons/icon-96x96.png',
'/icons/icon-128x128.png',
'/icons/icon-144x144.png',
'/icons/icon-152x152.png',
'/icons/icon-180x180.png',
'/icons/icon-192x192.png',
'/icons/icon-384x384.png',
'/icons/icon-512x512.png',
'/icons/icon-772x772.png'
];
self.addEventListener('install', e => {
e.waitUntil(caches.open(CACHE_NAME).then(c => c.addAll(STATIC_ASSETS)));
self.skipWaiting();
});
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then(keys => Promise.all(
keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k))
)).then(() => {
// Tell all clients to reload so they get the latest version immediately
return self.clients.matchAll({type:'window'}).then(clients => {
clients.forEach(client => client.postMessage({type:'SW_UPDATED'}));
});
})
);
self.clients.claim();
});
self.addEventListener('fetch', e => {
const url = e.request.url;
// Skip non-HTTP(S) schemes (chrome-extension://, data:, blob:, etc.) — Cache API rejects them
if (!url.startsWith('http://') && !url.startsWith('https://')) return;
// Skip non-GET requests — Cache API only supports caching GET
if (e.request.method !== 'GET') return;
// Network-first for HTML navigation — always serve fresh HTML
if (e.request.mode === 'navigate') {
e.respondWith(fetch(e.request).catch(() => caches.match('/index.html')));
return;
}
// Cache-first with network fallback for other assets
e.respondWith(
caches.match(e.request).then(cached => {
if (cached) return cached;
return fetch(e.request).then(response => {
if (response && response.status === 200 && response.type !== 'opaque') {
const clone = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put(e.request, clone));
}
return response;
}).catch(() => null);
})
);
});