-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceWorker.js
57 lines (52 loc) · 1.39 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
44
45
46
47
48
49
50
51
52
53
54
55
56
let cacheName = 'radio-cache-v3.4.2';
let urlsToCache = [
'./',
'./text.json',
'./en/',
'./en/text.json',
'./ro/',
'./ro/text.json',
'./style.css',
'./app.min.js',
'./img/main-logo.svg',
'./img/apple-touch.png',
'./img/android-chrome.png',
'./img/maskable-grey.svg',
'./img/star.png',
'./img/star-filled.png',
'./img/website.png',
'./fonts/MaterialIconsOutlined-Regular.otf'
];
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(cacheName)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', (e) => {
e.respondWith((async () => {
const r = await caches.match(e.request);
if (r) { return r; }
try{
const response = await fetch(e.request);
const cache = await caches.open(cacheName);
if(e.request.url.includes("/img/stations")){
cache.put(e.request, response.clone());
}
return response;
}catch (error){
return null;
}
})());
});
self.addEventListener('activate', (e) => {
e.waitUntil(caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if (key === cacheName) { return; }
return caches.delete(key);
}))
}));
});