2019-12-30 07:33:01 +00:00
|
|
|
var CACHE_NAME = 'trivial-static-image-gallery-v1';
|
|
|
|
var urlsToCache = [
|
|
|
|
'./',
|
|
|
|
'./index.html',
|
|
|
|
'./nsfw.html',
|
|
|
|
'./index.json',
|
|
|
|
'./nsfw.json',
|
|
|
|
'./main.css',
|
2019-12-30 12:53:05 +00:00
|
|
|
'./main.js',
|
2019-12-30 07:33:01 +00:00
|
|
|
'./font-awesome-4.7.0/css/font-awesome.min.css',
|
2019-12-30 12:53:05 +00:00
|
|
|
'./font-awesome-4.7.0/fonts/fontawesome-webfont.woff2',
|
|
|
|
'./font-awesome-4.7.0/fonts/fontawesome-webfont.woff2?v=4.7.0'
|
2019-12-30 07:33:01 +00:00
|
|
|
];
|
|
|
|
|
2019-12-30 14:38:26 +00:00
|
|
|
self.addEventListener('install', event => {
|
2019-12-30 07:33:01 +00:00
|
|
|
// Perform install steps
|
|
|
|
event.waitUntil(
|
|
|
|
caches.open(CACHE_NAME)
|
2019-12-30 14:38:26 +00:00
|
|
|
.then(cache => {
|
2019-12-30 07:33:01 +00:00
|
|
|
console.log('Opened cache');
|
|
|
|
return cache.addAll(urlsToCache);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-01-01 14:41:57 +00:00
|
|
|
self.addEventListener('fetch', function (evt) {
|
|
|
|
console.log('The service worker is serving the asset.');
|
|
|
|
evt.respondWith(fromNetwork(evt.request, 15000).catch(function () {
|
|
|
|
return fromCache(evt.request);
|
|
|
|
}));
|
2020-01-01 14:40:29 +00:00
|
|
|
});
|
2019-12-30 07:33:01 +00:00
|
|
|
|
2020-01-01 14:40:29 +00:00
|
|
|
function fromNetwork(request, timeout) {
|
2020-01-01 14:41:57 +00:00
|
|
|
return new Promise(function (fulfill, reject) {
|
|
|
|
var timeoutId = setTimeout(reject, timeout);
|
|
|
|
fetch(request).then(function (response) {
|
|
|
|
clearTimeout(timeoutId);
|
2020-01-01 14:57:45 +00:00
|
|
|
var responseToCache = response.clone();
|
2020-01-01 14:41:57 +00:00
|
|
|
fulfill(response);
|
2020-01-01 14:57:45 +00:00
|
|
|
caches.open(CACHE_NAME).then(function (cache) {
|
2020-01-01 16:05:17 +00:00
|
|
|
cache.put(request, responseToCache);
|
2020-01-01 14:57:45 +00:00
|
|
|
});
|
2020-01-01 14:41:57 +00:00
|
|
|
}, reject);
|
|
|
|
});
|
2020-01-01 14:40:29 +00:00
|
|
|
}
|
2019-12-30 14:38:26 +00:00
|
|
|
|
2020-01-01 14:40:29 +00:00
|
|
|
function fromCache(request) {
|
2020-01-01 14:57:45 +00:00
|
|
|
return caches.open(CACHE_NAME).then(function (cache) {
|
2020-01-01 14:41:57 +00:00
|
|
|
return cache.match(request).then(function (matching) {
|
|
|
|
return matching || Promise.reject('no-match');
|
|
|
|
});
|
|
|
|
});
|
2020-01-01 14:40:29 +00:00
|
|
|
}
|