Me gustaría mostrarle a mi usuario que hay una actualización disponible. Por lo tanto, necesito saber cómo verificar si ya usa la última versión.
Actualmente configuro un elemento en localStorage llamado "updateAvailable" tan pronto como el serviceWorker me dice que hay una nueva versión disponible. Eso funciona bien.
Sin embargo, lo que no funciona bien es cuando me gustaría configurar "updateAvailable" en falso. El elemento no está configurado en localStorage.
¿Cómo configuro este elemento en localStorage cuando se usa la última versión de mi PWA?
function registerValidSW(swUrl: string, config?: Config) { console.log("test 5") navigator.serviceWorker .register(swUrl) .then(registration => { registration.onupdatefound = () => { const installingWorker = registration.installing; if (installingWorker == null) { return; } installingWorker.onstatechange = () => { if (installingWorker.state === 'installed') { if (navigator.serviceWorker.controller) { // At this point, the updated precached content has been fetched, // but the previous service worker will still serve the older // content until all client tabs are closed. console.log( 'New content is available and will be used when all ' + 'tabs for this page are closed. See [hidden link]' ); localStorage.setItem("updateAvailable", "true") // Execute callback if (config && config.onUpdate) { config.onUpdate(registration); } } else { // At this point, everything has been precached. // It's the perfect time to display a // "Content is cached for offline use." message. console.log('Content is cached for offline use.'); localStorage.setItem("updateAvailable", "false") // Execute callback if (config && config.onSuccess) { config.onSuccess(registration); } } } }; }; }) .catch(error => { console.error('Error during service worker registration:', error); }); }ese bloque de código solo se ejecuta en el evento 'onupdatefound'.