-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw.js
86 lines (82 loc) · 2.83 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
'use module';
let config;
const getbase = url => url.replace(/^http(s)?:\/\/[^/]+|[^/]*(\?.*)?(#.*)?$/g, ''),
base = getbase(location.href),
homeReg = RegExp('^' + location.href.replace(/[^/]*(\?.*)?(#.*)?$/, '').replace(/[.*(){[\^$\\]/g, '\\$&') + '(index\\.html)?(\\?.*)?(#.*)?$'),
find = url => {
for (let i = 0; i < config.dir.length; i++) {
if (url.length > config.dir[i].length && url.substr(0, config.dir[i].length) === config.dir[i]) {
return true;
}
}
};
caches.open(base + 'home').then(cache => cache.match('config')).then(response => response && response.json()).then(json => config = json);
oninstall = skipWaiting;
onmessage = function (evt) {
if (getbase(evt.source.url) === base && evt.data) {
if (typeof evt.data === 'string') {
config = new URL(evt.data, evt.source.url).href;
} else if (evt.data.file && evt.data.dir) {
config = evt.data;
for (let i = 0; i < config.file.length; i++) {
config.file[i] = new URL(config.file[i], evt.source.url).href;
}
for (let i = 0; i < config.dir.length; i++) {
config.dir[i] = new URL(config.dir[i] + '/', evt.source.url).href;
}
caches.open(base + 'dir').then(cache => cache.keys().then(keys => keys.forEach(request => {
if (!find(request.url)) {
cache.delete(request);
}
})));
caches.open(base + 'file').then(cache => cache.keys().then(keys => keys.forEach(request => {
if (config.file.indexOf(request.url) < 0) {
cache.delete(request);
}
})));
}
caches.open(base + 'home').then(cache => cache.put('config', new Response(JSON.stringify(config))));
}
};
onfetch = function (evt) {
if (config && evt.request.method === 'GET') {
if (typeof config === 'string') {
if (evt.request.url.length >= config.length && evt.request.url.substr(0, config.length) === config) {
evt.respondWith(fetch(evt.request, {
mode: 'no-cors',
cache: 'no-cache'
}));
}
} else {
let type;
if (config.file.indexOf(evt.request.url) >= 0) {
type = 'file';
} else if (find(evt.request.url)) {
type = 'dir';
} else if (homeReg.test(evt.request.url)) {
type = 'home';
}
if (type === 'home') {
evt.respondWith(caches.open(base + type).then(cache => fetch(evt.request, {
mode: 'no-cors',
cache: 'no-cache'
}).then(response => {
cache.put(base, response.clone());
return response;
}, e => cache.match(base).then(response => response ? response : new Response(e.message, {
status: 502
})))));
} else if (type) {
evt.respondWith(caches.open(base + type).then(cache => cache.match(evt.request).then(response => response ? response : fetch(evt.request, {
mode: 'no-cors',
cache: 'no-cache'
}).then(response => {
cache.put(evt.request, response.clone());
return response;
}, e => new Response(e.message, {
status: 502
})))));
}
}
}
};