-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceWorker.js
67 lines (64 loc) · 1.45 KB
/
serviceWorker.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
// Define variables.
var version = 'v7::',
urlsToCache = [
'./',
'index.html',
'css/font-awesome.min.css',
'css/style.css',
'fonts/FontAwesome.otf',
'fonts/fontawesome-webfont.eot',
'fonts/fontawesome-webfont.svg',
'fonts/fontawesome-webfont.ttf',
'fonts/fontawesome-webfont.woff',
'fonts/fontawesome-webfont.woff2',
'js/libs/p5.dom.min.js',
'js/libs/p5.min.js',
'js/board.js',
'js/index.js'
];
// Install event listener.
self.addEventListener('install',function(e) {
e.waitUntil(
caches.open(version+'whiteboard')
.then(function(cache) {
// Cache all files needed to run application.
return cache.addAll(urlsToCache);
})
.then(function() {
// Install completed.
})
);
});
// Fetch event listener.
self.addEventListener('fetch',function(e) {
e.respondWith(
caches.match(e.request)
.then(function(response) {
// Return cache or network response.
return response || fetch(e.request);
})
.catch(function() {
// Both cache and network failed.
})
);
});
// Activate event listener.
self.addEventListener('activate',function(e) {
e.waitUntil(
caches.keys()
.then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
return !cacheName.startsWith(version);
})
.map(function(cacheName) {
// Delete outdated cache.
return caches.delete(cacheName);
})
);
})
.then(function() {
// Activation completed.
})
);
});