-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice_worker.js
45 lines (39 loc) · 1.16 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 = 'cache-and-network';
self.addEventListener('install', function(evt) {
console.log('The service worker is being installed.');
evt.waitUntil(precache());
});
self.addEventListener('fetch', function(evt) {
console.log('The service worker is serving the asset.');
evt.respondWith(fromCache(evt.request));
});
function precache() {
return caches.open(CACHE).then(function(cache) {
return cache.addAll([
'/',
'./index.html',
'./css/app.css',
'./js/main.js',
'/todojs/',
'/todojs/index.html',
'/todojs/css/app.css',
'/todojs/js/main.js',
]);
});
}
function fromCache(request) {
return caches.open(CACHE).then(function(cache) {
return cache.match(request).then(function(matching) {
return matching || fromNetwork(request);
});
});
}
function fromNetwork(request) {
return fetch(request).catch(function(error) {
return caches.open(CACHE).then(function(cache) {
return cache.match('/index.html').then(match => {
return match;
});
});
});
}