forked from udacity/mws-restaurant-stage-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
52 lines (48 loc) · 1.22 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
//Create static cache file name
const staticCacheName = 'mws-rest-review-static-v1';
//list of all pages to put into cache
const cacheAssets = [
'/',
'index.html',
'restaurant.html',
'css/styles.css',
'js/main.js',
'js/dbhelper.js',
'js/restaurant_info.js',
'data/restaurants.json'
]
//Call install
self.addEventListener('install', (e) => {
console.log('Service Worker: Installed');
e.waitUntil(
caches
.open(staticCacheName)
.then(cache => {
console.log('Service Worker: Caching files');
return cache.addAll(cacheAssets);
})
.then(() => self.skipWaiting())
);
});
//call activate
self.addEventListener('activate', (e) => {
console.log('Service Worker: Activated');
// remove old caches
e.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if(cache !== staticCacheName) {
console.log('Service Working: Clearing old Cache');
return caches.delete(cache);
}
})
);
})
);
});
//call fetch event
self.addEventListener('fetch', e => {
console.log('Service Worker: Fetching');
e.respondWith(fetch(e.request).catch(() => caches.match(e.request,{ignoreSearch:true})));
});