temp: try unregister sw

This commit is contained in:
fly_mc 2024-10-25 21:03:08 +08:00
parent 7864a631c4
commit 574798d019
2 changed files with 28 additions and 11 deletions

View file

@ -8,11 +8,28 @@ import { lang } from '@@/js/config.js';
export async function initializeSw() {
if (!('serviceWorker' in navigator)) return;
navigator.serviceWorker.register('/sw.js', { scope: '/', type: 'classic' });
navigator.serviceWorker.ready.then(registration => {
try {
const registrations = await navigator.serviceWorker.getRegistrations();
await Promise.all(
registrations.map(registration => registration.unregister())
);
console.info('Successfully unregistered old service worker(s)');
const registration = await navigator.serviceWorker.register('/sw.js', {
scope: '/',
type: 'classic'
});
await navigator.serviceWorker.ready;
registration.active?.postMessage({
msg: 'initialize',
lang,
});
});
console.info('Successfully registered and initialized new service worker');
} catch (error) {
console.error('Service worker registration/initialization failed:', error);
}
}