-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
36 lines (31 loc) · 963 Bytes
/
sw.js
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
// Heavily based on https://github.com/mdn/pwa-examples/blob/main/js13kpwa/sw.js
const cacheName = 'pipodoroPWA-v1';
const contentToCache = [
'index.html',
'timer.js',
'ui.js',
'utils.js',
'icons/favicon.svg'
]
// Installing Service Worker
self.addEventListener('install', (e) => {
e.waitUntil((async () => {
const cache = await caches.open(cacheName);
await cache.addAll(contentToCache);
})());
});
// Fetching content using Service Worker
self.addEventListener('fetch', (e) => {
// Cache http and https only, skip unsupported chrome-extension:// and file://...
if (!(e.request.url.startsWith('http:') || e.request.url.startsWith('https:'))) {
return;
}
e.respondWith((async () => {
const r = await caches.match(e.request);
if (r) { return r; }
const response = await fetch(e.request);
const cache = await caches.open(cacheName);
cache.put(e.request, response.clone());
return response;
})());
});