-
Notifications
You must be signed in to change notification settings - Fork 1
/
pwa-sw.js
86 lines (77 loc) · 2.67 KB
/
pwa-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
let CACHE_VERSION = '15';
let CACHE_NAME = 'instant-markdown-offline-cache-v' + CACHE_VERSION;
self.addEventListener('message', async e => {
if (e.data && e.data.type === 'LOG-VERSION')
console.log('-> cache version: ', CACHE_VERSION);
});
self.addEventListener('install', e => {
console.log('install...');
// cache files
e.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('...cache open');
let cacheURLs = [
'./',
'./favicon.png',
'./css/general.css',
'./font/OpenSans-Bold.ttf',
'./font/OpenSans-Bold.woff',
'./font/OpenSans-Light.ttf',
'./font/OpenSans-Light.woff',
'./js/file-handling.js',
'./js/general.js',
'./js/markdown.js',
'./js/pwa.js',
'./js/settings.js',
'./js/utils.js',
'./pwa/icon-192.png',
'./pwa/icon-512.png',
'./pwa/manifest.json'
].map(url => url + '?v' + CACHE_VERSION);
console.log('...cache:', cacheURLs);
cache.addAll(cacheURLs);
})
.then(() => console.log('-> installed', CACHE_NAME))
);
});
self.addEventListener('activate', e => {
console.log('activate...');
// remove old caches
e.waitUntil(
caches.keys().then(cacheNames => Promise.all(
cacheNames.map(cacheName => {
if (cacheName !== CACHE_NAME) {
console.log('...delete old cache', cacheName);
return caches.delete(cacheName);
}
})
))
.then(() => console.log('-> activated', CACHE_NAME))
);
});
self.addEventListener('fetch', e => {
if (e.request.url.endsWith('GET-VERSION')) {
e.respondWith(new Response(CACHE_VERSION));
return;
}
let query = 'get: ' + new URL(e.request.url).pathname;
// console.log(query + ': fetch...');
let request = e.request.url + '?v' + CACHE_VERSION;
e.respondWith(
caches.match(request).then(cacheResponse => {
if (cacheResponse) {
console.log(query + ' -> in cache');
return cacheResponse;
}
// console.log(query + ': ...not in cache');
return fetch(request).then(onlineResponse => {
if (onlineResponse && onlineResponse.status === 200)
console.log(query + ' -> downloaded');
else
console.log(query + ' -> failed');
return onlineResponse;
});
})
);
});