-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSerWork.js
142 lines (134 loc) · 4.27 KB
/
SerWork.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var CACHE = "cache-first";
self.addEventListener("install", function(evt){
self.skipWaiting();
});
self.addEventListener("fetch", function(evt){
//evt.respondWith(NetworkFirst(evt.request));
evt.respondWith(new Promise(function(respondWith){
var netPromise = NetworkFirst(evt.request);
var timeoutTimer = setTimeout(async function(){
var request = evt.request;
var cache,response;
if(!checkNeed(request,{ok:true})){
return;
}
cache = await caches.open(CACHE);
if(!timeoutTimer) return;
response = await cache.match(request);
if(!timeoutTimer) return;
if(response) {respondWith( response);
timeoutTimer = 0;
console.log(`response from timeout`,response);}
},1000);
netPromise.then(function(response){
if(!timeoutTimer) return;
else {
clearTimeout(timeoutTimer);
timeoutTimer=0;
respondWith(response);
}
console.log(`response from networkFirst`,response);
});
}));
});
async function NetworkFirst(request){
var cache;
request.url = request.url.split('?')[0];
var fet = fetch(request);
var response,ret;
var cloner;
if(!checkNeed(request,{ok:true})){
try{
return await fet;
}catch(e){
return offlineErrorPage(e);
}
}
try{
response = await fet;
}catch(e){
try{
cache = await caches.open(CACHE);
response = await cache.match(request);
if(response) return response;
return offlineErrorPage('预期情况:缓存捞不到页面。');
}catch(e){
return errorPage(e);
}
}
try{
if(!checkNeed(request,response)){
return response;
}
cloner = await responseCloner(response);
cache = await caches.open(CACHE);
await cache.put(request,cloner());
return cloner();
}catch(e){
return errorPage(e);
}
}
function offlineErrorPage(error){
var html = `
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>≥﹏≤ —— 傻瓜弹曲</title>
<body style="background:gray">
<div style="
margin:3em auto;
max-width:330px;
padding:1em;
border:black 1px solid;
background:white;
">
<h1>未找到网页</h1>
<p>我们不能从网络下载这张网页,并且不能从网页存储中捞出来这张页面。</p>
<p>最大的可能是您不在线,尽管也有可能是本网站被和谐了。</p>
<p>如果您真的不在线,请您连接网络后再次尝试访问。如果能从网站下载这张网页,我们会把它存入网页存储,期望下回您可以离线使用本程序。</p>
<script>setTimeout('location.reload()',5000);</script>
</div>
<xmp style="overflow-x:auto;max-width:100%">${error.stack || error}</xmp>
</body>
`;
return new Response(new Blob([html],{type:'text/html'}),{status:504,statusText:'Gateway Timeout -- Check your network'});
}
function errorPage(error){
var html = `
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>≥﹏≤ —— 傻瓜弹曲</title>
<body style="background:gray">
<div style="
margin:3em auto;
max-width:330px;
padding:1em;
border:black 1px solid;
background:white;
">
<h1>BUG</h1>
<p>您遭遇了一个臭虫。到留言板来,我去杀他。(记得把框框外面的字也给我贴一份)</p>
<p>尝试强制刷新网页(按下Shift同时按刷新按钮)。如果不能,清理缓存,关闭本网站其它页面然后刷新本页面直到内容出现。</p>
</div>
<xmp style="overflow-x:auto;max-width:100%">${error.stack || error}</xmp>
<script>
navigator.serviceWorker.getRegistration().then(function(r){r.unregister()})
</script>
</body>
`;
return new Response(new Blob([html],{type:'text/html'}),{status:502,statusText:'Bad Gateway of Server Worker'});
}
function checkNeed(request,response){
if(request.url.split('?')[0].toLowerCase().indexOf('.d') > 0
|| request.url.split('?')[0].toLowerCase().indexOf('voice.jpg') >= 0
|| request.url.split('?')[0].toLowerCase().indexOf('voice.png') >= 0){
// Niao Source
return false;
}
return response.ok;
}
async function responseCloner(response){
var res = response.clone();
return function(){
return res.clone();
};
}