-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_worker.js
58 lines (50 loc) · 1.46 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
54
55
56
57
58
(function() {
var version = 'v1::',
cacheIdentifier = 'static',
cacheKey = version + cacheIdentifier;
var installAssets = [
'/',
'/index.html'
];
function cacheInstallAssets() {
return caches.open().then(function(cache) {
return cache.addAll(installAssets);
});
};
function cleanCache() {
caches.keys().then(function(keys) {
return Promise.all(keys.filter(function(key) {
return key.indexOf(version) !== 0;
})).map(function(key) {
return caches.delete(key);
});
});
};
self.addEventListener('install', function(event) {
event.waitUntil(cacheInstallAssets());
});
self.addEventListener('activate', function(event) {
event.waitUntil(cleanCache());
});
self.addEventListener('fetch', function(event) {
var request;
request = event.request;
if (request.headers.get('Accept').indexOf('text/html') !== -1) {
event.respondWith(fetch(request).then(function(response) {
caches.open(cacheKey).then(function(cache) {
return cache.put(request, response.clone());
});
return response;
}).catch(function() {
return caches.match(request).then(function(response) {
return response;
});
}));
return;
}
return event.respondWith(caches.match(request).then(function(response) {
debugger
return response || fetch(request, { mode: 'no-cors' });
}));
});
})();