2018-08-21 17:11:03 -04:00
|
|
|
const CACHE_NAME = 'hummingbird-anime-client';
|
|
|
|
|
|
|
|
async function fromCache (request) {
|
|
|
|
const cache = await caches.open(CACHE_NAME);
|
|
|
|
return await cache.match(request);
|
|
|
|
}
|
|
|
|
|
2018-11-07 14:29:21 -05:00
|
|
|
async function updateCache (request) {
|
2018-08-21 17:11:03 -04:00
|
|
|
const cache = await caches.open(CACHE_NAME);
|
|
|
|
const response = await fetch(request);
|
|
|
|
|
|
|
|
if (request.url.includes('/public/images/')) {
|
|
|
|
await cache.put(request, response.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2018-08-20 16:24:33 -04:00
|
|
|
self.addEventListener('install', event => {
|
2018-08-21 17:11:03 -04:00
|
|
|
console.log('Public Folder Worker installed');
|
2018-08-20 16:24:33 -04:00
|
|
|
|
|
|
|
event.waitUntil(
|
2018-08-21 17:11:03 -04:00
|
|
|
caches.open(CACHE_NAME)
|
2018-08-20 16:24:33 -04:00
|
|
|
.then(cache => cache.addAll([
|
|
|
|
'public/images/icons/favicon.ico',
|
2018-08-21 17:11:03 -04:00
|
|
|
'public/images/streaming-logos/amazon.svg',
|
|
|
|
'public/images/streaming-logos/crunchyroll.svg',
|
|
|
|
'public/images/streaming-logos/daisuki.svg',
|
|
|
|
'public/images/streaming-logos/funimation.svg',
|
|
|
|
'public/images/streaming-logos/hidive.svg',
|
|
|
|
'public/images/streaming-logos/hulu.svg',
|
|
|
|
'public/images/streaming-logos/netflix.svg',
|
|
|
|
'public/images/streaming-logos/tubitv.svg',
|
|
|
|
'public/images/streaming-logos/viewster.svg',
|
2018-08-20 16:24:33 -04:00
|
|
|
]))
|
2018-08-21 17:11:03 -04:00
|
|
|
)
|
2018-08-20 16:24:33 -04:00
|
|
|
});
|
|
|
|
|
2018-11-07 14:29:21 -05:00
|
|
|
self.addEventListener('activate', () => {
|
|
|
|
console.info('Public Folder Worker activated');
|
2018-08-21 17:11:03 -04:00
|
|
|
});
|
2018-08-20 16:24:33 -04:00
|
|
|
|
2018-08-21 17:11:03 -04:00
|
|
|
// Pull css, images, and javascript from cache
|
|
|
|
self.addEventListener('fetch', event => {
|
2018-10-08 15:45:46 -04:00
|
|
|
// Only cache things with a file extension,
|
|
|
|
// Ignore other requests
|
|
|
|
if ( ! event.request.url.includes('/public/')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-21 17:11:03 -04:00
|
|
|
fromCache(event.request).then(cached => {
|
|
|
|
if (cached !== undefined) {
|
|
|
|
event.respondWith(cached);
|
|
|
|
} else {
|
2018-11-07 14:29:21 -05:00
|
|
|
event.respondWith(updateCache(event.request));
|
2018-08-21 17:11:03 -04:00
|
|
|
}
|
|
|
|
});
|
2018-08-20 16:24:33 -04:00
|
|
|
});
|