"Normally", a PWA should update itself as soon as the files on the server change. In fact, we could not get this to work, and after month of fiddling with the serviceworker and applying tons of "guaranteed to work" fixes from the internet, at least for our project we finally decided to state that the auto-update mechanism is just broken, or at least not working reliably. In particular on iOS, we can state it never auto-updates, no matter what, and only reinstalling the app updates it. There are people claiming that it works for their specific PWAs, but we could never reproduce this reliably and finally gave up.
To those who have similar problems, I will describe how we are dealing with this in order to roll out updates to our PWA. Of course, while there are many other approaches thinkable, this is just our way:
- Our PWA talks to our server, written in PHP, via a self written REST API
- With every answer from the REST API, we also send back the version number the PWA client code should be on
- The PWA checks if the version number reported by the server is different from the version noted in the Web Core code and if so, shows a dialog informing the user of a new update and asking to install it
- If the user decides to install the update, the following code will be executed
ASM
// Taken from here:
// https://forum.quasar-framework.org/topic/2560/solved-pwa-force-refresh-when-new-version-released/38
if ('serviceWorker' in navigator) {
navigator.serviceWorker.getRegistrations().then(function (registrations) {
for (let registration of registrations) {
registration.update()
}
})
}
window.location.reload(true);
END;
- The "serviceWorker" related code seems to deal with caching of files and the
"window.location.reload(true);"
forces the PWA to reload.
While this works most of the time right on the first approach, sometimes it fails on the first approach and the user has to do it a 2nd or even a 3rd time. But finally, it reliably updates the client.
Hope this helps someone having similar problems.