-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsw.js
50 lines (44 loc) · 1.37 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
var CACHE = "v1.1"
var filesToCache = [
'./',
'./resources/js/jquery.min.js',
'./resources/js/highlight.min.js',
'./resources/js/json-to-go.js',
'./resources/js/common.js',
'./resources/js/gofmt.js',
'./resources/css/tomorrow.highlight.css',
'./resources/css/common.css',
'./resources/images/toml-to-go.png'
]
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);
});
}