-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
89 lines (84 loc) · 3.15 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const APP_PREFIX = 'Netwalk_'; // Identifier for this app (this needs to be consistent across every cache update)
const VERSION = 'version_04'; // Version of the off-line cache (change this value everytime you want to update cache)
const CACHE_NAME = APP_PREFIX + VERSION;
const URLS = [
'/netwalk/',
'/netwalk/index.html',
'/netwalk/index.js',
'/netwalk/item.class.js',
'/netwalk/field.class.js',
'/netwalk/img/background.png',
'/netwalk/img/cable0001.png',
'/netwalk/img/cable0010.png',
'/netwalk/img/cable0011.png',
'/netwalk/img/cable0100.png',
'/netwalk/img/cable0101.png',
'/netwalk/img/cable0110.png',
'/netwalk/img/cable0111.png',
'/netwalk/img/cable1000.png',
'/netwalk/img/cable1001.png',
'/netwalk/img/cable1010.png',
'/netwalk/img/cable1011.png',
'/netwalk/img/cable1100.png',
'/netwalk/img/cable1101.png',
'/netwalk/img/cable1110.png',
'/netwalk/img/cable1111.png',
'/netwalk/img/computer1.png',
'/netwalk/img/computer2.png',
'/netwalk/img/focus.png',
'/netwalk/img/highscores.png',
'/netwalk/img/homepage.png',
'/netwalk/img/locked.png',
'/netwalk/img/newgame.png',
'/netwalk/img/qnetwalk.png',
'/netwalk/img/icon.png',
'/netwalk/img/quit.png',
'/netwalk/img/server.png',
'/netwalk/img/shadow.png',
];
// Respond with cached resources
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)
})
)
})
// Cache resources
self.addEventListener('install', function (e) {
e.waitUntil(
caches.open(CACHE_NAME).then(function (cache) {
console.log('installing cache : ' + CACHE_NAME, cache);
return cache ? cache.addAll(URLS) : Promise.resolve();
})
)
})
// 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])
}
}))
})
)
});