forked from udacity/mws-restaurant-stage-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
39 lines (36 loc) · 931 Bytes
/
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
const versionName = 'v1';
// Install service worker
self.addEventListener('install', function(e) {
});
// Activate service worker
self.addEventListener('activate', function(e) {
// Remove unwanted caches
e.waitUntil(
caches.keys().then(versionNames => {
return Promise.all(
versionNames.map(cache => {
if (cache !== versionName) {
return caches.delete(cache);
}
})
);
})
);
});
// Fetching files for offline view
self.addEventListener('fetch', function(e) {
e.respondWith(
fetch(e.request)
.then(res => {
// copy/clone of response
const resClone = res.clone();
// Open cahce
caches.open(versionName).then(cache => {
// Add response to cache
cache.put(e.request, resClone);
});
return res;
})
.catch(err => caches.match(e.request).then(res => res))
);
});