-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
58 lines (49 loc) · 1.54 KB
/
sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const CACHE_NAME = 'hummingbird-anime-client';
async function fromCache (request) {
const cache = await caches.open(CACHE_NAME);
return await cache.match(request);
}
async function updateCache (request) {
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;
}
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', () => {
console.info('Public Folder Worker activated');
});
// Pull css, images, and javascript from cache
self.addEventListener('fetch', event => {
// Only cache things with a file extension,
// Ignore other requests
if ( ! event.request.url.includes('/public/')) {
return;
}
fromCache(event.request).then(cached => {
if (cached !== undefined) {
event.respondWith(cached);
} else {
event.respondWith(updateCache(event.request));
}
});
});