Update service-worker.js

This commit is contained in:
streetturtle 2018-09-25 14:23:11 -04:00 committed by GitHub
parent 4b06063102
commit 7b63b4f05a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 38 additions and 18 deletions

View File

@ -9,18 +9,29 @@ const PRE_CACHED_ASSETS = [
'/index.html'
];
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
let cachePromises = PRE_CACHED_ASSETS.map(function(asset) {
var url = new URL(asset, location.href);
var request = new Request(url);
return fetch(request).then(function(response) {
return cache.put(asset, response);
});
});
// self.addEventListener('install', function(event) {
// event.waitUntil(
// caches.open(CACHE_NAME).then(function(cache) {
// let cachePromises = PRE_CACHED_ASSETS.map(function(asset) {
// var url = new URL(asset, location.href);
// var request = new Request(url);
// return fetch(request).then(function(response) {
// return cache.put(asset, response);
// });
// });
return Promise.all(cachePromises);
// return Promise.all(cachePromises);
// })
// );
// });
importScripts('/cache-polyfill.js');
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll(PRE_CACHED_ASSETS);
})
);
});
@ -40,12 +51,21 @@ self.addEventListener('activate', function(event) {
);
});
// self.addEventListener('fetch', function(event) {
// if (event.request.headers.get('accept').startsWith('text/html')) {
// event.respondWith(
// fetch(event.request).catch(error => {
// return caches.match('index.html');
// })
// );
// }
// });
self.addEventListener('fetch', function(event) {
if (event.request.headers.get('accept').startsWith('text/html')) {
console.log(event.request.url);
event.respondWith(
fetch(event.request).catch(error => {
return caches.match('index.html');
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
}
});