Merge pull request #930 from nonameShijian/PR-Branch
更新http协议完成后延迟重启,初次加载serviceWorker重启,使用xhr获取数据时加入状态码判断
This commit is contained in:
commit
a97069f092
|
@ -62,7 +62,9 @@ boot().then(() => {
|
|||
const thisWindow = remote.getCurrentWindow();
|
||||
thisWindow.loadURL(url);
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
location.href = url;
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
10
game/game.js
10
game/game.js
|
@ -136,10 +136,19 @@ new Promise(resolve => {
|
|||
}
|
||||
if (location.protocol.startsWith('http') && 'serviceWorker' in navigator) {
|
||||
let scope = window.location.protocol + '//' + window.location.host + '/';
|
||||
navigator.serviceWorker.getRegistrations().then(registrations => {
|
||||
let findServiceWorker = false;
|
||||
for (let registration of registrations) {
|
||||
if (registration && registration.active && registration.active.scriptURL == `${scope}service-worker.js`) {
|
||||
findServiceWorker = true;
|
||||
}
|
||||
}
|
||||
navigator.serviceWorker.register(`${scope}service-worker.js`, {
|
||||
updateViaCache: "all",
|
||||
scope,
|
||||
}).then(registration => {
|
||||
// 初次加载worker,需要重新启动一次
|
||||
if (!findServiceWorker) location.reload();
|
||||
navigator.serviceWorker.addEventListener('message', e => {
|
||||
console.log(e);
|
||||
});
|
||||
|
@ -148,6 +157,7 @@ new Promise(resolve => {
|
|||
}).catch(e => {
|
||||
console.log('serviceWorker加载失败: ', e);
|
||||
});
|
||||
});
|
||||
}
|
||||
const script = document.createElement('script')
|
||||
script.type = "module";
|
||||
|
|
|
@ -263,6 +263,11 @@ export class LibInit extends Uninstantable {
|
|||
const xmlHttpRequest = new XMLHttpRequest();
|
||||
let data;
|
||||
xmlHttpRequest.addEventListener("load", () => {
|
||||
if (![0, 200].includes(xmlHttpRequest.status)) {
|
||||
// @ts-ignore
|
||||
if (typeof onError == 'function') onError(new Error(oReq.statusText || oReq.status));
|
||||
return;
|
||||
}
|
||||
data = xmlHttpRequest.responseText;
|
||||
if (!data) {
|
||||
if (typeof onError == 'function') onError(new Error(`${scriptSource}加载失败!`));
|
||||
|
@ -303,7 +308,14 @@ export class LibInit extends Uninstantable {
|
|||
sScriptURL = url + str;
|
||||
}
|
||||
const oReq = new XMLHttpRequest();
|
||||
if (typeof onload == 'function') oReq.addEventListener("load", onload);
|
||||
if (typeof onload == 'function') oReq.addEventListener("load", result => {
|
||||
if (![0, 200].includes(oReq.status)) {
|
||||
// @ts-ignore
|
||||
if (typeof onerror == 'function') onerror(new Error(oReq.statusText || oReq.status));
|
||||
return;
|
||||
}
|
||||
onload(result);
|
||||
});
|
||||
if (typeof onerror == 'function') oReq.addEventListener("error", onerror);
|
||||
oReq.open("GET", sScriptURL);
|
||||
oReq.send();
|
||||
|
@ -330,7 +342,14 @@ export class LibInit extends Uninstantable {
|
|||
sScriptURL = url + str;
|
||||
}
|
||||
const oReq = new XMLHttpRequest();
|
||||
if (typeof onload == 'function') oReq.addEventListener("load", onload);
|
||||
if (typeof onload == 'function') oReq.addEventListener("load", result => {
|
||||
if (![0, 200].includes(oReq.status)) {
|
||||
// @ts-ignore
|
||||
if (typeof onerror == 'function') onerror(new Error(oReq.statusText || oReq.status));
|
||||
return;
|
||||
}
|
||||
onload(result);
|
||||
});
|
||||
if (typeof onerror == 'function') oReq.addEventListener("error", onerror);
|
||||
oReq.open("GET", sScriptURL, false);
|
||||
oReq.send();
|
||||
|
@ -340,6 +359,11 @@ export class LibInit extends Uninstantable {
|
|||
static json(url, onload, onerror) {
|
||||
const oReq = new XMLHttpRequest();
|
||||
if (typeof onload == 'function') oReq.addEventListener("load", () => {
|
||||
if (![0, 200].includes(oReq.status)) {
|
||||
// @ts-ignore
|
||||
if (typeof onerror == 'function') onerror(new Error(oReq.statusText || oReq.status));
|
||||
return;
|
||||
}
|
||||
let result;
|
||||
try {
|
||||
result = JSON.parse(oReq.responseText);
|
||||
|
@ -368,6 +392,11 @@ export class LibInit extends Uninstantable {
|
|||
}
|
||||
const oReq = new XMLHttpRequest();
|
||||
if (typeof onload == 'function') oReq.addEventListener("load", () => {
|
||||
if (![0, 200].includes(oReq.status)) {
|
||||
// @ts-ignore
|
||||
if (typeof onerror == 'function') onerror(new Error(oReq.statusText || oReq.status));
|
||||
return;
|
||||
}
|
||||
let result;
|
||||
try {
|
||||
result = JSON.parse(oReq.responseText);
|
||||
|
|
Loading…
Reference in New Issue