-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
61 lines (50 loc) · 1.57 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
51
52
53
54
55
56
57
58
59
60
61
// inspired from https://gist.github.com/kosamari/7c5d1e8449b2fbc97d372675f16b566e
const APP_PREFIX = 'RandomMusicNote';
const CACHE_NAME = './';
const CACHE_ROOT = '/RandomMusicNote';
const urlsToCache = [
'/',
'/index.html',
'/styles.css',
'/random_note.js',
'/presets.js',
].map(url => CACHE_ROOT + url);
// Respond with cached resources
self.addEventListener('fetch', function (event) {
// only handle GETs and let the browser handle the rest
if (event.request.method != 'GET') return;
event.respondWith(
caches.match(event.request).then(function (request) {
return request || fetch(e.request)
})
)
});
// Cache resources
self.addEventListener('install', function (e) {
e.waitUntil(
caches.open(CACHE_NAME).then(function (cache) {
console.log('installing cache : ' + CACHE_NAME)
return cache.addAll(urlsToCache)
})
)
})
// Delete outdated caches
self.addEventListener('activate', function (e) {
e.waitUntil(
caches.keys().then(function (keyList) {
// `keyList` contains all cache names under your username.github.io
// filter out ones that has this app prefix to create white list
var cacheWhitelist = keyList.filter(function (key) {
return key.indexOf(APP_PREFIX);
})
// add current cache name to white list
cacheWhitelist.push(CACHE_NAME)
return Promise.all(keyList.map(function (key, i) {
if (cacheWhitelist.indexOf(key) === -1) {
console.log('deleting cache : ' + keyList[i] )
return caches.delete(keyList[i])
}
}))
})
)
});