-
Notifications
You must be signed in to change notification settings - Fork 1
/
service-worker.js
52 lines (50 loc) · 1.3 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
// - 每 10s 检查一次 -
const CHECK_CRASH_INTERVAL = 10 * 1000;
// - 25s 超过 25s 没有心跳则认为已经 crash -
const CRASH_THRESHOLD = 25 * 1000;
let timer;
// - 主页传递参数 - 上报的对象 -
const pages = {};
// ---- 检测页面是否 crash ----
function checkCrash() {
const now = Date.now();
for (var id in pages) {
let page = pages[id];
if ((now - page.t) > CRASH_THRESHOLD) {
// 上报 crash
const location = self.location;
const app_key = location.search.split("appKey=")[1] || "";
fetch("https://dev-ones.cn/api/report/crash", {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
app_key: app_key,
origin: location.origin,
}),
}).then((res) => {})
delete pages[id];
}
}
if (Object.keys(pages).length == 0) {
clearInterval(timer);
timer = null;
}
}
this.addEventListener('message', (e) => {
const data = e.data;
if (data.type === 'running') { // 正常心跳
pages[data.id] = {
t: Date.now(),
}
if (!timer) {
timer = setInterval(function () {
checkCrash();
}, CHECK_CRASH_INTERVAL);
}
} else if (data.type === 'clear') {
delete pages[data.id]
}
})