-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
43 lines (39 loc) · 1.15 KB
/
sw.js
File metadata and controls
43 lines (39 loc) · 1.15 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
const CACHE_NAME = 'rq-app-cache-v4';
const PDF_CACHE = 'rq-pdf-cache-v1';
// App shell files to cache for offline
const APP_SHELL = [
'/readqueue/',
'/readqueue/index.html',
'/readqueue/manifest.json',
];
// Install — cache app shell
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => cache.addAll(APP_SHELL))
.then(() => self.skipWaiting())
);
});
// Activate — clean old caches
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(keys =>
Promise.all(
keys.filter(k => k !== CACHE_NAME && k !== PDF_CACHE)
.map(k => caches.delete(k))
)
).then(() => self.clients.claim())
);
});
// Fetch — serve app shell from cache, fall back to network.
// PDF caching is managed manually by the app (stored as 'pdf-{id}' keys),
// not intercepted here, so Drive API requests always go to the network.
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request).then(cached =>
cached || fetch(event.request).catch(() =>
caches.match('/readqueue/index.html')
)
)
);
});