-
Notifications
You must be signed in to change notification settings - Fork 4
/
service-worker.js
32 lines (30 loc) · 1006 Bytes
/
service-worker.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
const CACHE = 'fdp-1';
// On activate, clear old caches
self.addEventListener('activate', event => {
const caches = [CACHE];
event.waitUntil(
caches.keys().then(cacheNames => {
return cacheNames.filter(cacheName => !caches.includes(cacheName));
}).then(cachesToDelete => {
return Promise.all(cachesToDelete.map(cacheToDelete => {
return caches.delete(cacheToDelete);
}));
}).then(() => self.clients.claim())
);
});
// Offline-first read-through cache
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.open(CACHE).then(function(cache) {
return cache.match(event.request).then(function(response) {
if (response) return response; // found in cache
return fetch(event.request.clone()).then(function(response) {
if (response.status < 400) cache.put(event.request, response.clone());
return response;
});
}).catch(function(error) {
throw error;
});
})
);
});