-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
34 lines (32 loc) · 1.18 KB
/
service-worker.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
const cacheName = 'checkduplicate-cache';
const filestoCache = [
'./',
'./index.html',
"./assets/icon.png",
"./manifest.json",
"https://cdn.jsdelivr.net/npm/js-sha256@0.11.0/src/sha256.min.js",
'https://fonts.googleapis.com/css2?family=Work+Sans:wght@400;700&display=swap',
];
self.addEventListener('install', e => {
e.waitUntil(
caches.open(cacheName)
.then(cache => cache.addAll(filestoCache))
);
});
self.addEventListener('activate', e => self.clients.claim());
self.addEventListener('fetch', async (event) => {
const req = event.request;
if (req.url.indexOf("update") !== -1 || req.url.indexOf("gstatic.com") !== -1 && navigator.userAgent.indexOf("Firefox") !== -1) event.respondWith(await fetch(req)); else event.respondWith(networkFirst(req));
});
async function networkFirst(req) {
try {
const networkResponse = await fetch(req);
const cache = await caches.open("checkduplicate-cache");
await cache.delete(req);
await cache.put(req, networkResponse.clone());
return networkResponse;
} catch (error) {
const cachedResponse = await caches.match(req);
return cachedResponse;
}
}