-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
66 lines (59 loc) · 1.81 KB
/
sw.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
57
58
59
60
61
62
63
64
65
66
const activeCache = 'currency-converter-v1';
self.addEventListener('install', event => {
const toCache = [
'/',
'/assets/css/style.css',
'/assets/css/materialize.min.css',
'/main.js',
'https://fonts.googleapis.com/icon?family=Material+Icons',
'https://fonts.googleapis.com/css?family=Oswald',
'https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/js/materialize.min.js',
'https://cdn.jsdelivr.net/npm/[email protected]/lib/idb.min.js',
'https://free.currencyconverterapi.com/api/v5/countries'
];
event.waitUntil(
// Cache the values
caches
.open(activeCache)
.then(cache => {
return cache.addAll(toCache);
})
.catch(err => {
console.log(err);
})
);
});
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(names => {
return Promise.all(
names
.filter(name => name !== activeCache)
.map(name => caches.delete(name))
);
})
);
});
self.addEventListener('fetch', event => {
// Respond with cached responses if any
event.respondWith(
// If there's a response in the cache else Return response
caches.match(event.request).then(res => {
if (res) return res;
return fetch(event.request).then(response => {
const clone = response.clone();
if (response.ok || response.type === 'opaque') {
// Cache the response if the url begins with country flags
if (event.request.url.startsWith('http://www.countryflags.io/')) {
caches.open(activeCache).then(cache => {
cache.put(event.request, clone);
});
}
}
// Return the response
return response;
});
})
);
});