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' '/index.html'
]; ];
self.addEventListener('install', function(event) { // self.addEventListener('install', function(event) {
event.waitUntil( // event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) { // caches.open(CACHE_NAME).then(function(cache) {
let cachePromises = PRE_CACHED_ASSETS.map(function(asset) { // let cachePromises = PRE_CACHED_ASSETS.map(function(asset) {
var url = new URL(asset, location.href); // var url = new URL(asset, location.href);
var request = new Request(url); // var request = new Request(url);
return fetch(request).then(function(response) { // return fetch(request).then(function(response) {
return cache.put(asset, 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) { self.addEventListener('fetch', function(event) {
if (event.request.headers.get('accept').startsWith('text/html')) { console.log(event.request.url);
event.respondWith( event.respondWith(
fetch(event.request).catch(error => { caches.match(event.request).then(function(response) {
return caches.match('index.html'); return response || fetch(event.request);
}) })
); );
}
}); });