-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
65 lines (58 loc) · 1.88 KB
/
service-worker.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
const APP_PREFIX = 'FoodFest-';
const VERSION = 'version_01';
const CACHE_NAME = APP_PREFIX + VERSION;
const FILES_TO_CACHE = [
"./index.html",
"./events.html",
"./tickets.html",
"./schedule.html",
"./assets/css/style.css",
"./assets/css/bootstrap.css",
"./assets/css/tickets.css",
"./dist/app.bundle.js",
"./dist/events.bundle.js",
"./dist/tickets.bundle.js",
"./dist/schedule.bundle.js"
];
self.addEventListener('install', function (e) {
e.waitUntil(
caches.open(CACHE_NAME).then(function (cache) {
console.log('installing cache : ' + CACHE_NAME)
return cache.addAll(FILES_TO_CACHE)
})
)
})
self.addEventListener('activate', function(e) {
e.waitUntil(
caches.keys().then(function(keyList) {
let cacheKeeplist = keyList.filter(function(key) {
return key.indexOf(APP_PREFIX);
});
cacheKeeplist.push(CACHE_NAME);
return Promise.all(
keyList.map(function(key, i) {
if (cacheKeeplist.indexOf(key) === -1) {
console.log('deleting cache : ' + keyList[i]);
return caches.delete(keyList[i]);
}
})
);
})
);
});
self.addEventListener('fetch', function (e) {
console.log('fetch request : ' + e.request.url)
e.respondWith(
caches.match(e.request).then(function (request) {
if (request) { // if cache is available, respond with cache
console.log('responding with cache : ' + e.request.url)
return request
} else { // if there are no cache, try fetching request
console.log('file is not cached, fetching : ' + e.request.url)
return fetch(e.request)
}
// You can omit if/else for console.log & put one line below like this too.
// return request || fetch(e.request)
})
)
})