4.1 Beta...ish #12
91
sw.js
91
sw.js
@ -1,36 +1,75 @@
|
||||
self.addEventListener('install', event => {
|
||||
console.log('Worker installed');
|
||||
const CACHE_NAME = 'hummingbird-anime-client';
|
||||
|
||||
event.waitUntil(
|
||||
caches.open('hummingbird-anime-client')
|
||||
.then(cache => cache.addAll([
|
||||
'public/images/icons/favicon.ico',
|
||||
'public/css/app.min.css',
|
||||
'public/js/index.min.js',
|
||||
'public/js/index-authed.min.js',
|
||||
'public/js/tables.min.js',
|
||||
]))
|
||||
);
|
||||
});
|
||||
async function fromCache (request) {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
return await cache.match(request);
|
||||
}
|
||||
|
||||
self.addEventListener('fetch', event => {
|
||||
async function fromNetwork (request) {
|
||||
return await fetch(request);
|
||||
}
|
||||
|
||||
async function update (request) {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
const response = await fetch(request);
|
||||
|
||||
if (request.url.includes('/public/images/')) {
|
||||
console.log('Saving to cache: ', request.url);
|
||||
await cache.put(request, response.clone());
|
||||
}
|
||||
|
||||
event.respondWith(
|
||||
caches.match(event.request).then(response => {
|
||||
if (response !== undefined) {
|
||||
// Return cached version
|
||||
return response;
|
||||
}
|
||||
|
||||
return fetch(event.request).then(response => {
|
||||
const clone = response.clone();
|
||||
function refresh (response) {
|
||||
return self.clients.matchAll().then(clients => {
|
||||
clients.forEach(client => {
|
||||
const message = {
|
||||
type: 'refresh',
|
||||
url: response.url,
|
||||
eTag: response.headers.get('ETag')
|
||||
};
|
||||
|
||||
caches.open('hummingbird-anime-client').then(cache => {
|
||||
cache.put(event.request, clone);
|
||||
});
|
||||
|
||||
return response;
|
||||
});
|
||||
client.postMessage(JSON.stringify(message));
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
self.addEventListener('install', event => {
|
||||
console.log('Public Folder Worker installed');
|
||||
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME)
|
||||
.then(cache => cache.addAll([
|
||||
'public/images/icons/favicon.ico',
|
||||
'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',
|
||||
]))
|
||||
)
|
||||
});
|
||||
|
||||
self.addEventListener('activate', event => {
|
||||
console.log('Public Folder Worker activated');
|
||||
});
|
||||
|
||||
// Pull css, images, and javascript from cache
|
||||
self.addEventListener('fetch', event => {
|
||||
fromCache(event.request).then(cached => {
|
||||
if (cached !== undefined) {
|
||||
event.respondWith(cached);
|
||||
} else {
|
||||
event.respondWith(fromNetwork(event.request));
|
||||
}
|
||||
});
|
||||
|
||||
event.waitUntil(
|
||||
update(event.request).then(refresh)
|
||||
);
|
||||
});
|
Loading…
Reference in New Issue
Block a user