youcount.github.io/sw.js

44 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-08-29 12:34:09 -07:00
/* global noConnection:false, Promise:false */
2018-05-25 17:37:59 -07:00
var CACHE_NAME = 'youcount-cache-1.4';
2017-08-27 15:57:35 -07:00
var urlsToCache = [
'/index.html',
'/images/social.png',
'/js/script.js'
];
2017-08-29 12:22:33 -07:00
self.addEventListener('install', function (event) {
event.waitUntil(
caches.open(CACHE_NAME)
.then(function (cache) {
return cache.addAll(urlsToCache);
})
);
});
2017-08-29 12:34:09 -07:00
self.addEventListener('activate', function (event) {
event.waitUntil(
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.filter(function (cacheName) {
2017-08-29 12:37:19 -07:00
return cacheName != CACHE_NAME;
2017-08-29 12:34:09 -07:00
}).map(function (cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
2017-08-27 15:57:35 -07:00
self.addEventListener('fetch', function (event) {
2017-08-28 13:56:46 -07:00
try {
event.respondWith(
caches.match(event.request)
.then(function (response) {
if (response) {
return response;
}
2018-05-25 19:17:14 -07:00
return fetch(event.request, {cache: "reload"});
2017-08-28 13:56:46 -07:00
})
);
} catch (err) {
noConnection('Service Worker failed to fetch!');
}
2017-08-27 15:57:35 -07:00
});