Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
82 lines (76 loc) · 2.53 KB
/
Copy pathsw.js
File metadata and controls
82 lines (76 loc) · 2.53 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
82
// ═══════════════════════════════════════════
// VERD Service Worker — Offline Caching
// ═══════════════════════════════════════════
const CACHE_NAME = 'verd-v4';
const ASSETS = [
'./',
'index.html',
'src/style.css',
'src/main.js',
'src/router.js',
'src/state.js',
'src/utils/dom.js',
'src/utils/icons.js',
'src/utils/offline.js',
'src/components/Card.js',
'src/components/Layout.js',
'src/components/Modal.js',
'src/components/NeuralNetAnim.js',
'src/components/VoiceScanner.js',
'src/components/ReportCard.js',
'src/components/WeatherWidget.js',
'src/services/auth.js',
'src/services/firestore.js',
'src/services/scanner.js',
'src/services/weather.js',
'src/pages/Login.js',
'src/pages/Register.js',
'src/pages/Dashboard.js',
'src/pages/AdminDashboard.js',
'src/pages/Scan.js',
'src/pages/ScanResults.js',
'src/pages/ScanHistory.js',
'src/pages/Marketplace.js',
'src/pages/NotFound.js',
];
// Install — cache all assets
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open(CACHE_NAME)
.then(cache => cache.addAll(ASSETS))
.then(() => self.skipWaiting())
);
});
// Activate — clean old caches
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k)))
).then(() => self.clients.claim())
);
});
// Fetch — cache-first for app assets, network-first for API
self.addEventListener('fetch', (e) => {
const url = new URL(e.request.url);
// Network-first for API calls and external resources
if (url.origin !== location.origin ||
url.pathname.startsWith('/api') ||
url.hostname.includes('firebase') ||
url.hostname.includes('googleapis') ||
url.hostname.includes('cdn.jsdelivr')) {
e.respondWith(
fetch(e.request)
.catch(() => caches.match(e.request))
);
return;
}
// Cache-first for app shell
e.respondWith(
caches.match(e.request)
.then(cached => cached || fetch(e.request).then(resp => {
const clone = resp.clone();
caches.open(CACHE_NAME).then(cache => cache.put(e.request, clone));
return resp;
}))
);
});