-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
41 lines (40 loc) · 1.36 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
35
36
37
38
39
40
41
const cacheName = 'unzipify-cache';
const filestoCache = [
'./',
'./index.html',
'./script.js',
'./style.css',
'./assets/mergedAssets.json',
'./assets/logo.png',
'./manifest.json',
'./OpenSource.md',
'https://fonts.googleapis.com/css2?family=Josefin+Sans:wght@700&family=Work+Sans&display=swap',
'./jszip.js',
'./heic2any.js',
'https://cdn.jsdelivr.net/npm/markdown-it@13.0.1/dist/markdown-it.min.js',
'https://cdn.jsdelivr.net/npm/pdfjs-dist@3.9.179/build/pdf.min.js',
'https://cdn.jsdelivr.net/npm/pdfjs-dist@3.9.179/build/pdf.worker.min.js',
];
self.addEventListener('install', e => {
e.waitUntil(
caches.open(cacheName)
.then(cache => cache.addAll(filestoCache))
);
});
self.addEventListener('activate', e => self.clients.claim());
self.addEventListener('fetch', event => {
const req = event.request;
if (req.url.indexOf("updatecode") !== -1) return fetch(req); else event.respondWith(networkFirst(req));
});
async function networkFirst(req) {
try {
const networkResponse = await fetch(req);
const cache = await caches.open('unzipify-cache');
await cache.delete(req);
await cache.put(req, networkResponse.clone());
return networkResponse;
} catch (error) {
const cachedResponse = await caches.match(req);
return cachedResponse;
}
}