forked from udacity/mws-restaurant-stage-1
/
sw.template.js
78 lines (70 loc) · 2.38 KB
/
sw.template.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
67
68
69
70
71
72
73
74
75
76
77
78
const version = '0.1.5';
const staticCacheName = `restaurants-${version}`;
self.addEventListener('install', function(event) {
const timeStamp = Date.now();
event.waitUntil(
caches.open(staticCacheName).then(function(cache) {
return cache.addAll([
'/',
`/index.html?timestamp=${timeStamp}`,
`/about.html?timestamp=${timeStamp}`,
`/restaurant.html?timestamp=${timeStamp}`,
`/css/styles.min.css?timestamp=${timeStamp}`,
`/js/index.min.js?timestamp=${timeStamp}`,
`/js/restaurant.min.js?timestamp=${timeStamp}`,
`/js/about.min.js?timestamp=${timeStamp}`,
// injector:images
// endinjector
])
.then(() => self.skipWaiting());
})
);
});
self.addEventListener('activate', function(event) {
console.log('Activating new service worker...');
var cacheWhitelist = [staticCacheName];
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});
self.addEventListener('fetch', function(event) {
console.log('WORKER: fetch event in progress.');
/* We should only cache GET requests, and deal with the rest of method in the
client-side, by handling failed POST,PUT,PATCH,etc. requests.
*/
if (event.request.method !== 'GET') {
/* If we don't block the event as shown below, then the request will go to
the network as usual.
*/
console.log('WORKER: fetch event ignored.', event.request.method, event.request.url);
return;
}
event.respondWith(caches.match(event.request).then(function(response) {
// caches.match() always resolves
// but in case of success response will have value
if (response !== undefined) {
return response;
} else {
return fetch(event.request).then(function (response) {
// response may be used only once
// we need to save clone to put one copy in cache
// and serve second one
let responseClone = response.clone();
caches.open(staticCacheName).then(function (cache) {
cache.put(event.request, responseClone);
});
return response;
}).catch(function (error) {
console.log('response failed with ' + error);
});
}
}));
});