-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
53 lines (45 loc) · 1.28 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const version = 1;
const cache_name = `v${version}`;
// list of assets we want to prefetch
const assets = [
// servers may redirect the path to index.html but for the service worker
// these are two distinct urls and must be prefetched separately
'./',
'index.html',
'jingle.js',
'jingle.css',
'jingle.m4a',
'bdsu-logo.png',
];
self.addEventListener('install', event => {
// prefetch all our assets
const promise = caches.open(cache_name).then(
cache => cache.addAll(assets)
);
event.waitUntil(promise);
// become active immediately
self.skipWaiting();
});
self.addEventListener('activate', event => {
const promise = caches.keys().then(keys => {
return Promise.all(keys.map(key => {
// delete all caches of previous versions of this service worker
if (key !== cache_name) {
return caches.delete(key);
}
}));
});
event.waitUntil(promise);
});
self.addEventListener('fetch', event => {
const response = fetch(event.request)
.then(response => caches.open(cache_name).then(cache => {
// add all successful requests to the cache
cache.put(event.request, response.clone());
return response;
});
)
// if fetch from server fails try to respond with a cached version
.catch(error => caches.match(event.request));
event.respondWith(response);
});