Update ServiceWorker without page reload on Safari

Viewed 1559

I want my PWA to periodically update itself while it is running, not waiting for the user to refresh the page (e.g. on iOS 12 it is actually quite hard to trigger page reload of a PWA).

I know there is ServiceWorkerRegistration.update() method, but it's not supported by Safari.

So, are there any workarounds to get my ServiceWorker to self-update without page reload on iOS?

Additional information:

Currently I periodically poll version.json file in which I put current app's version, compare it, and force a page reload. It works fine, but it requires two page reload to get the new version (this + after the activated event to load the new app's assets).

Here's my current solution, if you need more context: https://github.com/dimaip/calendar/blob/master/app/serviceWorker.js#L31

1 Answers

ServiceWorkerRegistration.update() is supported by all browsers that support service workers, and should accomplish what you want.

If you wanted to confirm for yourself that it's actually supported in Safari, try running

reg = await navigator.serviceWorker.ready
reg.update()

in Safari's JS console, with the Network panel open. You should see a request made for your service worker after you run that.

Related