-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
29 lines (29 loc) · 962 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
self.addEventListener('install', function(event) {
var indexPage = new Request('index.html');
event.waitUntil(
fetch(indexPage).then(function(response) {
return caches.open('offline').then(function(cache) {
return cache.put(indexPage, response);
});
}));
});
self.addEventListener('fetch', function(event) {
var updateCache = function(request){
return caches.open('offline').then(function (cache) {
return fetch(request).then(function (response) {
return cache.put(request, response);
});
});
};
event.waitUntil(updateCache(event.request));
event.respondWith(
fetch(event.request).catch(function(error) {
return caches.open('offline').then(function (cache) {
return cache.match(event.request).then(function (matching) {
var report = !matching || matching.status == 404?Promise.reject('no-match'): matching;
return report
});
});
})
);
})