update service worker to hack things

This commit is contained in:
Cadey Ratio 2018-11-13 07:44:45 -08:00
parent e245d89daa
commit 2453be3817
1 changed files with 10 additions and 10 deletions

View File

@ -9,15 +9,15 @@ var preLoad = function(){
console.log('[PWA Builder] Install Event processing');
return caches.open('pwabuilder-offline').then(function(cache) {
console.log('[PWA Builder] Cached index and offline page during Install');
return cache.addAll(['/offline.html', '/index.html']);
return cache.addAll(['/blog/', '/blog', '/', '/contact', '/resume']);
});
}
};
self.addEventListener('fetch', function(event) {
console.log('[PWA Builder] The service worker is serving the asset.');
event.respondWith(checkResponse(event.request).catch(function() {
return returnFromCache(event.request)}
));
return returnFromCache(event.request);
}));
event.waitUntil(addToCache(event.request));
});
@ -25,18 +25,18 @@ var checkResponse = function(request){
return new Promise(function(fulfill, reject) {
fetch(request).then(function(response){
if(response.status !== 404) {
fulfill(response)
fulfill(response);
} else {
reject()
reject();
}
}, reject)
}, reject);
});
};
var addToCache = function(request){
return caches.open('pwabuilder-offline').then(function (cache) {
return fetch(request).then(function (response) {
console.log('[PWA Builder] add page to offline'+response.url)
console.log('[PWA Builder] add page to offline'+response.url);
return cache.put(request, response);
});
});
@ -46,9 +46,9 @@ var returnFromCache = function(request){
return caches.open('pwabuilder-offline').then(function (cache) {
return cache.match(request).then(function (matching) {
if(!matching || matching.status == 404) {
return cache.match('offline.html')
return cache.match('offline.html');
} else {
return matching
return matching;
}
});
});