-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceWorker.js
43 lines (39 loc) · 1.26 KB
/
ServiceWorker.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
const cachables = {
"/SalaMuseoGames/": "networkFirst",
"/ext-bin-1/": "cacheFirst",
};
const checkUrlCaching = (url) => (cachables[url] || cachables[`/${url.split('://').slice(1).join('://').split('/')[1]}/`]);
const putResponseInCache = (request, response) => {
if (request.method === 'GET' && response.ok) {
return caches.open('GamingShitposting/v1').then((cache) => {
try {
return cache.put(request, response.clone());
} catch(err) {}
});
}
}
const strategies = {
networkFirst: async (request) => {
try {
const networkResponse = await fetch(request);
await putResponseInCache(request, networkResponse);
return networkResponse;
} catch (error) {
return ((await caches.match(request)) || Response.error());
}
},
cacheFirst: async (request) => {
const fetchResponsePromise = fetch(request).then(async (networkResponse) => {
await putResponseInCache(request, networkResponse);
return networkResponse;
});
return ((await caches.match(request)) || (await fetchResponsePromise));
},
}
self.addEventListener('activate', () => self.clients.claim());
self.addEventListener('fetch', (event) => {
const strategy = strategies[checkUrlCaching(event.request.url)];
if (strategy) {
return event.respondWith(strategy(event.request));
}
});