/
index.js
66 lines (48 loc) · 1.7 KB
/
index.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
// caches polyfill because it is not added to native yet!
var caches = require('./lib/serviceworker-caches');
if(typeof self.CACHE_NAME !== 'string') {
throw new Error('Cache Name cannot be empty');
}
self.addEventListener('fetch', function(event) {
// Clone the request for fetch and cache
// A request is a stream and can be consumed only once.
var fetchRequest = event.request.clone(),
cacheRequest = event.request.clone();
// Respond with content from fetch or cache
event.respondWith(
// Try fetch
fetch(fetchRequest)
// when fetch is successful, we update the cache
.then(function(response) {
// A response is a stream and can be consumed only once.
// Because we want the browser to consume the response,
// as well as cache to consume the response, we need to
// clone it so we have 2 streams
var responseToCache = response.clone();
// and update the cache
caches
.open(self.CACHE_NAME)
.then(function(cache) {
// Clone the request again to use it
// as the key for our cache
var cacheSaveRequest = event.request.clone();
cache.put(cacheSaveRequest, responseToCache);
});
// Return the response stream to be consumed by browser
return response;
})
// when fetch times out or fails
.catch(function(err) {
// Return the promise which
// resolves on a match in cache for the current request
// ot rejects if no matches are found
return caches.match(cacheRequest);
})
);
});
// Now we need to clean up resources in the previous versions
// of Service Worker scripts
self.addEventListener('activate', function(event) {
// Destroy the cache
event.waitUntil(caches.delete(self.CACHE_NAME));
});