-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
44 lines (39 loc) · 987 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
37
38
39
40
41
42
43
44
var CACHE = 'offline';
var URLS = [
'./',
'./index.html',
'./css/skeleton.min.css',
'./css/normalize.min.css',
'./js/main.js'
];
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE)
.then(function (cache) {
return cache.addAll(URLS);
})
);
console.log('installed');
});
self.addEventListener('activated', event => {
console.log('activated');
});
self.addEventListener('fetch', function(event) {
console.log('got a fetch request' + event);
event.respondWith(fromCache(event.request));
event.waitUntil(update(event.request));
});
function fromCache(request) {
return caches.open(CACHE).then(function (cache) {
return cache.match(request);
console.log('returned from cache');
});
}
function update(request) {
return caches.open(CACHE).then(function (cache) {
return fetch(request).then(function (response) {
return cache.put(request, response.clone());
console.log('update done ' + request);
});
});
}