worker加载完成后再加载entry.js
This commit is contained in:
parent
382a121ecf
commit
55a94424bc
52
game/game.js
52
game/game.js
|
@ -158,28 +158,8 @@ new Promise(resolve => {
|
|||
module._compile(require('fs').readFileSync(filename, 'utf8'), filename);
|
||||
};
|
||||
}
|
||||
if (location.protocol.startsWith('http') && 'serviceWorker' in navigator) {
|
||||
let scope = window.location.protocol + '//' + window.location.host + '/';
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
// 使serviceWorker加载完成后,再加载entry.js
|
||||
const loadEntryJs = () => {
|
||||
const script = document.createElement('script')
|
||||
script.type = "module";
|
||||
script.src = `${assetURL}game/entry.js`;
|
||||
|
@ -192,5 +172,33 @@ new Promise(resolve => {
|
|||
exit();
|
||||
}
|
||||
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`;
|
||||
});
|
||||
console.log(registrations);
|
||||
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();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -10,7 +10,7 @@ if (typeof ts != 'undefined') {
|
|||
console.log(`ts undefined`);
|
||||
}
|
||||
|
||||
console.log('version 2.1');
|
||||
console.log('version 2.2');
|
||||
|
||||
self.addEventListener("install", (event) => {
|
||||
// The promise that skipWaiting() returns can be safely ignored.
|
||||
|
@ -18,6 +18,12 @@ self.addEventListener("install", (event) => {
|
|||
self.skipWaiting();
|
||||
});
|
||||
|
||||
self.addEventListener("activate", (event) => {
|
||||
// 当一个 service worker 被初始注册时,页面在下次加载之前不会使用它。 claim() 方法会立即控制这些页面
|
||||
// @ts-ignore
|
||||
event.waitUntil(clients.claim());
|
||||
});
|
||||
|
||||
self.addEventListener('message', event => {
|
||||
console.log(event.data);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue