-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
81 lines (73 loc) · 2.56 KB
/
Copy pathsw.js
File metadata and controls
81 lines (73 loc) · 2.56 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
69
70
71
72
73
74
75
76
77
78
79
80
81
const CACHE = 'inventario-v29.2.0';
const APP_SHELL = [
'./',
'./index.html',
'./manifest.json',
'./icon-192.png',
'./icon-512.png',
];
const CDN_ASSETS = [
'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',
'https://cdn.tailwindcss.com',
'https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/jsbarcode/3.11.5/JsBarcode.all.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js',
'https://unpkg.com/@zxing/library@0.19.1/umd/index.min.js',
'https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;500;600&family=IBM+Plex+Sans:wght@400;500;600;700&display=swap',
];
self.addEventListener('install', e => {
e.waitUntil(
caches.open(CACHE).then(cache =>
Promise.allSettled(
[...APP_SHELL, ...CDN_ASSETS].map(url =>
fetch(url, { mode: url.startsWith('http') ? 'cors' : 'same-origin' })
.then(r => { if (r.ok) cache.put(url, r); })
.catch(() => {})
)
)
).then(() => self.skipWaiting())
);
});
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))
).then(() => self.clients.claim())
);
});
self.addEventListener('fetch', e => {
const url = e.request.url;
const isCDN = CDN_ASSETS.some(a => url.startsWith(a.split('?')[0]));
// CDN: cache-first, refresh cache in background on success
if (isCDN) {
e.respondWith(
caches.match(e.request).then(cached => {
if (cached) return cached;
return fetch(e.request).then(r => {
if (r.ok) { const clone = r.clone(); caches.open(CACHE).then(c => c.put(e.request, clone)); }
return r;
}).catch(() => new Response('', { status: 503 }));
})
);
return;
}
// Navegação: network-first, cai para cache/index.html offline
if (e.request.mode === 'navigate') {
e.respondWith(
fetch(e.request).then(r => {
const clone = r.clone();
caches.open(CACHE).then(c => c.put(e.request, clone));
return r;
}).catch(() =>
caches.match(e.request).then(cached => cached || caches.match('./index.html'))
)
);
return;
}
// Restantes pedidos same-origin (manifest, ícones): cache-first com fallback à rede
e.respondWith(
caches.match(e.request).then(cached => cached || fetch(e.request).catch(() => cached))
);
});