Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
40 lines (40 loc) · 1.72 KB
/
Copy pathsw.js
File metadata and controls
40 lines (40 loc) · 1.72 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
// Cache the small app shell for offline revisits. Same-origin assets are
// refreshed in the background when connected, preventing stale lessons.
const CACHE = 'agriempower-static-v2';
const ASSETS = [
'./', './index.html', './styles.css', './favicon.svg', './manifest.webmanifest',
'./assets/hero-farm.svg', './assets/icon-192.png', './assets/icon-512.png',
'./src/app.js', './src/data.js', './src/i18n.js', './src/icons.js',
'./src/storage.js', './src/utils.js'
];
self.addEventListener('install', event => {
event.waitUntil(caches.open(CACHE).then(cache => cache.addAll(ASSETS)).then(() => self.skipWaiting()));
});
self.addEventListener('activate', event => {
event.waitUntil(caches.keys().then(keys => Promise.all(keys.filter(key => key !== CACHE).map(key => caches.delete(key)))).then(() => self.clients.claim()));
});
self.addEventListener('fetch', event => {
if (event.request.method !== 'GET') return;
const requestURL = new URL(event.request.url);
if (requestURL.origin !== self.location.origin) return;
if (event.request.mode === 'navigate') {
event.respondWith(fetch(event.request).then(response => {
if (response.ok) {
const copy = response.clone();
caches.open(CACHE).then(cache => cache.put(event.request, copy));
}
return response;
}).catch(() => caches.match('./index.html')));
return;
}
event.respondWith(caches.match(event.request).then(cached => {
const update = fetch(event.request).then(response => {
if (response.ok && response.type === 'basic') {
const copy = response.clone();
caches.open(CACHE).then(cache => cache.put(event.request, copy));
}
return response;
}).catch(() => cached);
return cached || update;
}));
});