blob: 666c7d315ba2a19cb028535094800453c522c8b4 (
plain)
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
|
const cachable_api = [
'/api/image/load/',
];
const is_cachable = (url) => {
for (const api of cachable_api) {
console.log(api, url);
if (url.includes(api)) {
return true;
}
}
return false;
}
const put_in_cache = async (request, response) => {
const cache = await caches.open("v1");
await cache.put(request, response);
};
const cache_first = async (request, event) => {
if (request.method !== 'GET') {
return await fetch(request);
}
if (!is_cachable(request.url)) {
return await fetch(request);
}
const cache_response = await caches.match(request);
if (cache_response) {
return cache_response;
}
const network_response = await fetch(request);
event.waitUntil(put_in_cache(request, network_response.clone()));
return network_response;
};
self.addEventListener("fetch", (event) => {
event.respondWith(cache_first(event.request, event));
});
|