优化findServiceWorker写法

This commit is contained in:
nonameShijian 2024-02-16 17:16:13 +08:00
parent cd2b07a48d
commit 054afb0976
1 changed files with 3 additions and 6 deletions

View File

@ -144,12 +144,9 @@ new Promise(resolve => {
if (location.protocol.startsWith('http') && 'serviceWorker' in navigator) { if (location.protocol.startsWith('http') && 'serviceWorker' in navigator) {
let scope = window.location.protocol + '//' + window.location.host + '/'; let scope = window.location.protocol + '//' + window.location.host + '/';
navigator.serviceWorker.getRegistrations().then(registrations => { navigator.serviceWorker.getRegistrations().then(registrations => {
let findServiceWorker = false; let findServiceWorker = registrations.find(registration => {
for (let registration of registrations) { return registration && registration.active && registration.active.scriptURL == `${scope}service-worker.js`;
if (registration && registration.active && registration.active.scriptURL == `${scope}service-worker.js`) { })
findServiceWorker = true;
}
}
navigator.serviceWorker.register(`${scope}service-worker.js`, { navigator.serviceWorker.register(`${scope}service-worker.js`, {
updateViaCache: "all", updateViaCache: "all",
scope, scope,