Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
68 lines (63 loc) · 2.21 KB
/
Copy pathsw.js
File metadata and controls
68 lines (63 loc) · 2.21 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
66
67
68
/* ══════════════════════════════════════════════════════════════
WarpMiner Service Worker — Offline PWA
Developed by THE SAZ 🏴☠️
══════════════════════════════════════════════════════════════ */
const VERSION = 'warpminer-v0.6.8';
const CORE = [
'./',
'./index.html',
'./manifest.webmanifest',
'./assets/logo.svg',
'./assets/banner.svg',
'./assets/icon-192.png',
'./assets/icon-512.png',
'./assets/icon-maskable.png',
'./assets/apple-touch-icon.png'
];
self.addEventListener('install', e => {
e.waitUntil(
caches.open(VERSION)
.then(c => c.addAll(CORE))
.then(() => self.skipWaiting())
);
});
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys()
.then(keys => Promise.all(keys.filter(k => k !== VERSION).map(k => caches.delete(k))))
.then(() => self.clients.claim())
);
});
self.addEventListener('fetch', e => {
if (e.request.method !== 'GET') return;
const url = new URL(e.request.url);
if (url.origin === self.location.origin) {
// Same-origin: cache-first, then network (and cache the result)
e.respondWith(
caches.match(e.request).then(cached => {
if (cached) return cached;
return fetch(e.request).then(res => {
if (res && res.status === 200) {
const copy = res.clone();
caches.open(VERSION).then(c => c.put(e.request, copy));
}
return res;
}).catch(() => caches.match('./index.html'));
})
);
} else {
// Cross-origin (fonts / CDN): network-first, fallback to cache
e.respondWith(
fetch(e.request).then(res => {
if (res && (res.status === 200 || res.type === 'opaque')) {
const copy = res.clone();
caches.open(VERSION).then(c => c.put(e.request, copy));
}
return res;
}).catch(() => caches.match(e.request))
);
}
});
self.addEventListener('message', e => {
if (e.data && e.data.type === 'SKIP_WAITING') self.skipWaiting();
});