-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
sw.js
43 lines (37 loc) · 1.19 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
var CACHE = "v1:static"
var filesToCache = [
'./',
'resources/js/jquery.min.js',
'resources/js/highlight.pack.js',
'resources/js/common.js',
'https://mholt.github.io/json-to-go/resources/js/json-to-go.js',
'resources/js/curl-to-go.js',
'resources/js/gofmt.js',
'resources/css/color-brewer.css',
'resources/css/common.css'
]
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(fromCache(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 respond only from service worker.
// This strategy is cache first.
function fromCache(request) {
return caches.open(CACHE).then(function (cache) {
return cache.match(request).then(function (matching) {
return matching || fetch(request);
}).catch(console.error);
});
}