-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
76 lines (67 loc) · 2.14 KB
/
sw.js
File metadata and controls
76 lines (67 loc) · 2.14 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
// SW – VeganScanner (V6.8)
// Wichtig: Query-Strings werden NICHT mehr ignoriert, um Cache-Busting zu erlauben.
const VERSION = 'vegan-scanner-v6_8';
const APP_SHELL = [
'index.html',
'app.js?v=V6_8',
'manifest.webmanifest',
'icons/icon-192.png',
'icons/icon-512.png',
// Tesseract-Files ohne Query, aber wir matchen jetzt MIT Query
'vendor/tesseract/tesseract.min.js',
'vendor/tesseract/worker.min.js',
'vendor/tesseract/tesseract-core.wasm.js',
'vendor/tesseract/tesseract-core.wasm'
];
self.addEventListener('install', e => {
e.waitUntil((async () => {
const c = await caches.open(VERSION);
try { await c.addAll(APP_SHELL); } catch(e) {}
self.skipWaiting();
})());
});
self.addEventListener('activate', e => {
e.waitUntil((async () => {
const keys = await caches.keys();
await Promise.all(keys.map(k => k !== VERSION && caches.delete(k)));
self.clients.claim();
})());
});
// Sprachdaten: network-first, Rest: cache-first
self.addEventListener('fetch', e => {
const url = new URL(e.request.url);
if (url.pathname.includes('/vendor/tesseract/lang/')) {
e.respondWith(networkThenCache(e.request));
return;
}
const isShell = APP_SHELL.some(p =>
url.pathname.endsWith('/' + p) || (url.pathname === '/' && p === 'index.html'));
if (isShell) {
e.respondWith(cacheFirst(e.request));
}
});
async function cacheFirst(req) {
const cache = await caches.open(VERSION);
// Query-Strings NICHT ignorieren
const hit = await cache.match(req, { ignoreVary: true, ignoreSearch: false });
if (hit) return hit;
const res = await fetch(req);
if (res && res.ok && res.type !== 'opaque') {
try { await cache.put(req, res.clone()); } catch {}
}
return res;
}
async function networkThenCache(req) {
const cache = await caches.open(VERSION);
try {
const res = await fetch(req);
if (res && res.ok && res.type !== 'opaque') {
try { await cache.put(req, res.clone()); } catch {}
}
return res;
} catch {
const hit = await cache.match(req, { ignoreVary: true, ignoreSearch: false });
if (hit) return hit;
throw new Error('Offline und nicht im Cache: ' + req.url);
}
}