-
Notifications
You must be signed in to change notification settings - Fork 19
/
sw.js
52 lines (47 loc) · 1.3 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
/* global self, caches, fetch */
'use strict'
var APP_PREFIX = 'sweaterify_'
var VERSION = 'version_17'
var CACHE_NAME = APP_PREFIX + VERSION
var URLS = [
'/sweaterify/',
'/sweaterify/index.html',
'/sweaterify/assets/css/main.css',
'/sweaterify/assets/js/main.min.js',
'/sweaterify/assets/img/patterntop.png',
'/sweaterify/assets/img/patternbottom.png'
]
// Respond with cached resources
self.addEventListener('fetch', function (e) {
e.respondWith(
caches.match(e.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(URLS)
})
)
})
// Delete outdated caches
self.addEventListener('activate', function (e) {
e.waitUntil(
caches.keys().then(function (keyList) {
var cacheWhitelist = keyList.filter(function (key) {
return key.indexOf(APP_PREFIX)
})
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])
}
}))
})
)
})