-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
45 lines (38 loc) · 1.09 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
var CACHE = 'effigie_v1';
var TOCACHE = [
'/',
'/manifest.json',
'/images/effigie192.png',
'/images/effigie512.png',
'/scripts/seedrandom.js',
'/scripts/effigie.js',
'/styles/main.css'
];
self.addEventListener('install', function(evt) {
evt.waitUntil(precache());
});
self.addEventListener('fetch', function(evt) {
evt.respondWith(fromCache(evt.request));
if (evt.request.method == 'GET')
evt.waitUntil(update(evt.request));
});
function precache() {
return caches.open(CACHE).then(function(cache) {
return cache.addAll(TOCACHE);
});
}
function fromCache(request) {
return caches.open(CACHE).then(function(cache) {
return cache.match(request).then(function(matching) {
return matching || fetch(request);
});
});
}
function update(request) {
if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') return;
return caches.open(CACHE).then(function(cache) {
return fetch(request).then(function(response) {
return cache.put(request, response);
});
});
}