-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
48 lines (44 loc) · 1.28 KB
/
sw.js
File metadata and controls
48 lines (44 loc) · 1.28 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
const CACHE_NAME = 'kasirmini-cache-v11';
const CORE_ASSETS = [
'./',
'./index.html',
'./manifest.webmanifest',
'./sw.js',
// PWA icons
'./icons/ios/16.png',
'./icons/ios/32.png',
'./icons/ios/180.png',
'./icons/android/android-launchericon-192-192.png',
'./icons/android/android-launchericon-512-512.png',
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(CORE_ASSETS))
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => Promise.all(keys.map((key) => {
if (key !== CACHE_NAME) return caches.delete(key);
})))
);
self.clients.claim();
});
// Cache-first for all GET requests; fallback to network and cache runtime
self.addEventListener('fetch', (event) => {
const req = event.request;
if (req.method !== 'GET') return; // ignore non-GET
event.respondWith(
caches.match(req).then((cached) => {
if (cached) return cached;
return fetch(req).then((res) => {
try {
const resClone = res.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(req, resClone));
} catch (_) {}
return res;
}).catch(() => caches.match('./index.html'));
})
);
});