-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
45 lines (40 loc) · 1.21 KB
/
Copy pathsw.js
File metadata and controls
45 lines (40 loc) · 1.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
const CACHE_NAME = 'rt-cakap-v1';
const ASSETS = [
'/',
'/manifest.json',
'/icons/icon-192.svg',
'/icons/icon-512.svg'
];
self.addEventListener('install', event => {
self.skipWaiting();
event.waitUntil(caches.open(CACHE_NAME).then(cache => cache.addAll(ASSETS)));
});
self.addEventListener('activate', event => {
event.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', event => {
const url = new URL(event.request.url);
// Special-case tessdata: try cache first, then network and cache
if (url.pathname.startsWith('/tessdata/')) {
event.respondWith(
caches.open(CACHE_NAME).then(async cache => {
const cached = await cache.match(event.request);
if (cached) return cached;
try {
const resp = await fetch(event.request);
if (resp && resp.ok) {
cache.put(event.request, resp.clone());
}
return resp;
} catch (err) {
return new Response('', { status: 404 });
}
})
);
return;
}
// Default: cache-first for known assets, fallback to network
event.respondWith(
caches.match(event.request).then(resp => resp || fetch(event.request).catch(() => caches.match('/')))
);
});