-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
78 lines (72 loc) · 2.1 KB
/
service-worker.js
File metadata and controls
78 lines (72 loc) · 2.1 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
const CACHE_NAME = 'binmanager-v48';
const FONT_CACHE = 'binmanager-fonts-v1';
const ASSETS = [
'./',
'./index.html',
'./style.css',
'./src/app.js',
'./src/db.js',
'./src/scanner.js',
'./src/ui/dom.js',
'./src/ui/toast.js',
'./src/ui/modal.js',
'./src/lib/routes.js',
'./src/lib/tags.js',
'./src/lib/sort.js',
'./src/lib/import-validation.js',
'./src/lib/migrations.js',
'./src/lib/ids.js',
'./src/lib/sync-meta.js',
'./src/lib/cloud-sync.js',
'./src/views/search.js',
'./src/views/item-form.js',
'./src/views/bins.js',
'./manifest.json',
'https://unpkg.com/html5-qrcode@2.3.8/html5-qrcode.min.js',
'https://cdn.jsdelivr.net/npm/fuse.js@7.0.0/dist/fuse.min.js',
'https://cdn.jsdelivr.net/npm/qrcode@1.5.1/build/qrcode.min.js',
];
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open(CACHE_NAME).then((c) => c.addAll(ASSETS)).then(() => self.skipWaiting())
);
});
self.addEventListener('activate', (e) => {
const keep = new Set([CACHE_NAME, FONT_CACHE]);
e.waitUntil(
caches.keys().then((names) =>
Promise.all(names.filter((n) => !keep.has(n)).map((n) => caches.delete(n)))
)
);
self.clients.claim();
});
function isGoogleFont(url) {
return url.origin === 'https://fonts.googleapis.com' ||
url.origin === 'https://fonts.gstatic.com';
}
self.addEventListener('fetch', (e) => {
const url = new URL(e.request.url);
// Google Fonts: stale-while-revalidate
if (isGoogleFont(url)) {
e.respondWith(
caches.open(FONT_CACHE).then((cache) =>
cache.match(e.request).then((cached) => {
const fetched = fetch(e.request)
.then((response) => {
cache.put(e.request, response.clone());
return response;
})
.catch(() => cached); // fallback to cache on network failure
return cached || fetched;
})
)
);
return;
}
// Everything else: cache-first, fallback to network
e.respondWith(
caches.match(e.request).then((cached) =>
cached || fetch(e.request).catch(() => new Response('Offline', { status: 503 }))
)
);
});