-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
30 lines (27 loc) · 737 Bytes
/
sw.js
File metadata and controls
30 lines (27 loc) · 737 Bytes
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
var version = '1';
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([
'index.html'
]);
}));
});
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('index.html'));
}
// The below line is buggy. The fetch header needs to be cleared after a request is done.
//
return fetch(event.request);
}));
});