-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
67 lines (63 loc) · 2.08 KB
/
sw.js
File metadata and controls
67 lines (63 loc) · 2.08 KB
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
const APP_NAME = 'logo-code-editor';
const CACHE_NAME = `${APP_NAME}-v1`;
const urlsToCache = [
`/${APP_NAME}/`,
`/${APP_NAME}/index.html`,
`/${APP_NAME}/js/libs/p5/p5.min.js`,
`/${APP_NAME}/js/libs/p5/p5.dom.min.js`,
`/${APP_NAME}/js/sketch.js`,
`/${APP_NAME}/js/turtle.js`,
`/${APP_NAME}/js/swhelper.js`,
`/${APP_NAME}/css/main.css`,
`/${APP_NAME}/assets/offline/offline_examples.json`,
`/${APP_NAME}/assets/offline/offline_logocode.json`
];
// Perform install steps
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('Cache opened');
return cache.addAll(urlsToCache);
})
);
});
// Respond with cached resources
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((request) => {
return request || fetch(event.request).then((response) => {
return caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, response.clone());
return response;
});
}).catch((error) => {
if (event.request.url.split('?')[0].endsWith('.logocode')) {
return caches.match(`/${APP_NAME}/assets/offline/offline_logocode.json`);
} else if (event.request.url.endsWith('/examples')) {
return caches.match(`/${APP_NAME}/assets/offline/offline_examples.json`);
}
});
})
)
})
// Delete outdated caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keyList) => {
// `keyList` contains all cache names under your username.github.io
// filter out ones that has this app prefix to create white list
let cacheWhitelist = keyList.filter((key) => {
return key.indexOf(APP_NAME)
})
// add current cache name to white list
cacheWhitelist.push(CACHE_NAME);
return Promise.all(keyList.map((key, i) => {
if (cacheWhitelist.indexOf(key) === -1) {
console.log(`deleting cache: ${keyList[i]}`)
return caches.delete(keyList[i])
}
}))
})
)
})