-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
executable file
·49 lines (43 loc) · 1.31 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
var CACHE = "v1.1"
var filesToCache = [
'./',
'./resources/js/jquery.min.js',
'./resources/js/highlight.min.js',
'./resources/js/json-to-nest.js',
'./resources/js/common.js',
'./resources/css/tomorrow.highlight.css',
'./resources/css/common.css',
'https://d33wubrfki0l68.cloudfront.net/e937e774cbbe23635999615ad5d7732decad182a/26072/logo-small.ede75a6b.svg'
]
self.addEventListener('install', function (evt) {
console.log('Attempting service worker installation.');
// Wait until promise resolves
evt.waitUntil(precache());
});
// On fetch, return from cache
self.addEventListener('fetch', function (evt) {
evt.respondWith(serve(evt.request));
});
// Opens cache and loads filesToCache into cache for using them in future
function precache() {
return caches
.open(CACHE)
.then(function (cache) {
return cache.addAll(filesToCache);
});
}
// When a resource is requested first serve from service worker and if service
// worker hasn't cached that request, then fetch that resource and then serve it
// This strategy is cache first.
function serve(request) {
return caches
.open(CACHE)
.then(function (cache) {
return cache
.match(request)
.then(function (matching) {
return matching || fetch(request);
})
.catch(console.error);
});
}