-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
41 lines (38 loc) · 1.1 KB
/
Copy pathservice-worker.js
File metadata and controls
41 lines (38 loc) · 1.1 KB
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
// service-worker.js
const CACHE_NAME = 'ec-app-v1';
const ASSETS = [
'/',
'/index.html',
'/css/style.css',
'/js/app.js',
'/js/ui.js',
'/js/storage.js',
'/js/i18n.js',
'/locales/en.json',
'/locales/ur.json',
'/assets/icons.svg'
];
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open(CACHE_NAME).then(cache => cache.addAll(ASSETS))
);
self.skipWaiting();
});
self.addEventListener('activate', (e) => {
e.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', (e) => {
const url = new URL(e.request.url);
if(ASSETS.includes(url.pathname) || e.request.destination === 'document' || e.request.destination === 'style' || e.request.destination === 'script'){
e.respondWith(
caches.match(e.request).then(resp => resp || fetch(e.request).then(fetchResp => {
return caches.open(CACHE_NAME).then(cache => {
cache.put(e.request, fetchResp.clone());
return fetchResp;
});
})).catch(() => caches.match('/index.html'))
);
} else {
e.respondWith(fetch(e.request).catch(() => caches.match(e.request)));
}
});