forked from dr5hn/countries-states-cities-database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
49 lines (46 loc) · 2 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
importScripts('./js/cache-polyfill.js');
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open('countrystatecity').then((cache) => {
// add all the following resources to the cache
return cache.addAll([
'./',
'./css/app.css',
'./js/app.js',
'./index.html',
'./vendor/bootstrap/css/bootstrap.min.css',
'./vendor/bootstrap/js/bootstrap.bundle.min.js',
'./vendor/dynatable/css/jquery.dynatable.css',
'./vendor/dynatable/js/jquery.dynatable.js',
'./vendor/jquery/jquery.min.js',
'https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/master/countries.json',
'https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/master/states.json',
'https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/master/cities.json'
]);
})
);
});
self.addEventListener('activate', function (event) {
event.waitUntil(
// Get all the cache names
caches.keys().then(function (cacheNames) {
return Promise.all(
// Get all the items that are stored under a different cache name than the current one
cacheNames.filter(function (cacheName) {
return cacheName != 'countrystatecity';
}).map(function (cacheName) {
// Delete the items
return caches.delete(cacheName);
})
); // end Promise.all()
}) // end caches.keys()
); // end event.waitUntil()
});
// NOTE: the fetch event is triggered for every request on the page. So for every individual CSS, JS and image file.
self.addEventListener('fetch', function (event) {
event.respondWith(
caches.match(event.request).then(function (response) {
return response || fetch(event.request);
})
);
});