PWA not auto-updating - Our solution

"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.

1 Like

Walter. I have mine updating perfectly using the method previously posted from an apache server.

Hello Ken,
I also tried your suggestion but unfortunately it didn't work. We have no influence on the config of the web server. Maybe the issue lies there, but we just don't know.
On the other hand, the solution presented has the additional benefit that you pretty much are in control what exactly should happen when an update is available.
Did you btw finally manage to present a progress bar when updating?
Walter

No, as it loads in the background it became unnecessary. Also works 100% of the time.

Ken, would you mind to confirm that the attached serviceworker is exactly the one you successfully have in use?
Thanks a lot, Walter

ServiceWorkerRelease.zip (703 Bytes)

Hi Walter,

Yes, it is the same except for one crucial thing. Im my file I have the comment:

// Version 1.18

Which I change for every new version I upload. This is because the service worker file uploaded has to be bitwise different to the previous one for it to be automatically downloaded and processed.

Regards,

Ken

Thank you Ken!
As I use auto build numbering, any new generated serviceworker is always binary different from the previous build because of the new file name the main js file gets. And still, this doesn't change anything. The app just doesn't automatically update on iOS.
My solution to this issue works and so I have made my peace with it.
Regards, Walter