-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
81 lines (74 loc) · 2.65 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Service Worker Script (sw.js)
// Define the URLs to cache
const cacheName = 'anime-offline-database-cache-v1';
const urlsToCache = [
'/',
'index.html',
'script.js',
'search_anime.js',
'search_anime_bg.wasm',
'search_anime.d.ts',
'package.json',
'search_anime_bg.wasm.d.ts',
'style.css',
'imgs/default.jpg',
'imgs/default-anime-fall.png',
'imgs/default-anime-spring.png',
'imgs/default-anime-summer.png',
'imgs/default-anime-winter.png',
'https://raw.githubusercontent.com/manami-project/anime-offline-database/master/anime-offline-database-minified.json',
'https://api.github.com/repos/manami-project/anime-offline-database/commits/master'
];
// Event listener for installing the service worker
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(cacheName).then(async (cache) => {
try {
ok = await cache.addAll(urlsToCache);
} catch (err) {
for (let i of urlsToCache) {
try {
ok = await cache.add(i);
} catch (err) {
console.warn('sw: cache.add', i);
}
}
}
return ok;
})
);
});
// Event listener for fetching and updating data
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then(async (response) => getData(event, response))
);
});
async function getData(event, response) {
const checkUpdate = "https://api.github.com/repos/manami-project/anime-offline-database/commits/master";
if (event.request.url === 'https://raw.githubusercontent.com/manami-project/anime-offline-database/master/anime-offline-database-minified.json') {
try {
const resp = await fetch(checkUpdate);
const data = await resp.json();
const cachedResponse = await caches.match(event.request);
if (cachedResponse) throw new Error("No old data");
const cachedData = await cachedResponse.json();
if (data.sha !== cachedData.sha) {
const cache = await caches.open(cacheName);
let re = await fetch(event.request);
await cache.put(event.request, re.clone());
await cache.put(checkUpdate, resp.clone());
return re
}
} catch (error) {
console.warn("failed to check for update: " + error);
}
return check2(event, response);
}
}
function check2(event, response) {
if (response) {
return response
}
return fetch(event.request);
}