Merge pull request #959 from nonameShijian/PR-Branch

worker加载完成后再加载entry.js, 扩展的enable为void 0的时候,改为true
This commit is contained in:
Spmario233 2024-02-16 20:09:04 +08:00 committed by GitHub
commit 33c7439149
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 64 additions and 48 deletions

View File

@ -158,28 +158,8 @@ new Promise(resolve => {
module._compile(require('fs').readFileSync(filename, 'utf8'), filename); module._compile(require('fs').readFileSync(filename, 'utf8'), filename);
}; };
} }
if (location.protocol.startsWith('http') && 'serviceWorker' in navigator) { // 使serviceWorker加载完成后再加载entry.js
let scope = window.location.protocol + '//' + window.location.host + '/'; const loadEntryJs = () => {
navigator.serviceWorker.getRegistrations().then(registrations => {
let findServiceWorker = registrations.find(registration => {
return registration && registration.active && registration.active.scriptURL == `${scope}service-worker.js`;
})
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);
});
registration.update();
// console.log(`set scope: ${scope}, service worker instance:`, registration);
}).catch(e => {
console.log('serviceWorker加载失败: ', e);
});
});
}
const script = document.createElement('script') const script = document.createElement('script')
script.type = "module"; script.type = "module";
script.src = `${assetURL}game/entry.js`; script.src = `${assetURL}game/entry.js`;
@ -192,5 +172,32 @@ new Promise(resolve => {
exit(); exit();
} }
document.head.appendChild(script); document.head.appendChild(script);
};
if (location.protocol.startsWith('http') && 'serviceWorker' in navigator) {
let scope = window.location.protocol + '//' + window.location.host + '/';
navigator.serviceWorker.getRegistrations()
.then(async registrations => {
let findServiceWorker = registrations.find(registration => {
return registration && registration.active && registration.active.scriptURL == `${scope}service-worker.js`;
});
try {
const registration_1 = await navigator.serviceWorker.register(`${scope}service-worker.js`, {
updateViaCache: "all",
scope,
});
// 初次加载worker需要重新启动一次
if (!findServiceWorker) location.reload();
navigator.serviceWorker.addEventListener('message', e => {
console.log(e);
});
registration_1.update();
} catch (e_1) {
console.log('serviceWorker加载失败: ', e_1);
}
}).finally(loadEntryJs);
} else {
loadEntryJs();
}
} }
}); });

View File

@ -4640,6 +4640,9 @@ export class Game extends Uninstantable {
* @param { string } extensionName * @param { string } extensionName
*/ */
static hasExtension(extensionName) { static hasExtension(extensionName) {
if (typeof lib.config[`extension_${extensionName}_enable`] != 'boolean') {
game.saveExtensionConfig(extensionName, 'enable', true);
}
return this.hasExtensionInstalled(extensionName) && lib.config[`extension_${extensionName}_enable`]; return this.hasExtensionInstalled(extensionName) && lib.config[`extension_${extensionName}_enable`];
} }
/** /**

View File

@ -5,12 +5,12 @@ var ts;
importScripts('./game/typescript.js'); importScripts('./game/typescript.js');
// @ts-ignore // @ts-ignore
if (typeof ts != 'undefined') { if (typeof ts != 'undefined') {
console.log(ts); console.log(`ts loaded`);
} else { } else {
console.log(`ts undefined`); console.log(`ts undefined`);
} }
console.log('version 2.1'); console.log('serviceWorker version 2.2');
self.addEventListener("install", (event) => { self.addEventListener("install", (event) => {
// The promise that skipWaiting() returns can be safely ignored. // The promise that skipWaiting() returns can be safely ignored.
@ -18,6 +18,12 @@ self.addEventListener("install", (event) => {
self.skipWaiting(); self.skipWaiting();
}); });
self.addEventListener("activate", (event) => {
// 当一个 service worker 被初始注册时,页面在下次加载之前不会使用它。 claim() 方法会立即控制这些页面
// @ts-ignore
event.waitUntil(clients.claim());
});
self.addEventListener('message', event => { self.addEventListener('message', event => {
console.log(event.data); console.log(event.data);
}); });