-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
48 lines (42 loc) · 1.39 KB
/
sw.js
File metadata and controls
48 lines (42 loc) · 1.39 KB
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
var version = '4';
var CacheArray = [
'https://progressive-web-app-components.github.io/serviceWorker/offline.html',
'https://progressive-web-app-components.github.io/serviceWorker/offline.css',
'https://progressive-web-app-components.github.io/serviceWorker/offline.js',
'https://progressive-web-app-components.github.io/serviceWorker/style.css',
];
var offlineHtml = 'https://progressive-web-app-components.github.io/serviceWorker/offline.html';
self.addEventListener('install', function(event) {
console.log('SW installed ', version , ' -> ', new Date().toLocaleString());
self.skipWaiting();
event.waitUntil(
caches.open(version)
.then(function(cache) {
return cache.addAll(CacheArray);
}));
});
self.addEventListener('activate', function(event) {
console.log('SW activated ', version, ' -> ', new Date().toLocaleString());
event.waitUntil(
caches.keys().then(function(keys) {
return Promise.all(
keys.filter(function(key) {
return key !== version;
}).map(function(key) {
return caches.delete(key);
}));
}));
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(function(res) {
if(res) {
return res;
}
if(!navigator.onLine) {
return caches.match(new Request(offlineHtml));
}
return fetch(event.request);
}));
});