I have a web app that is working and relying on service workers to keep all the cached files in check and to make sure users are on the correct version of the app.
Our client is currently wanting the device to check for an update at specific points (When reopening the app) etc, as currently when you open the app it can take up to 5minutes before the device realises its on an outdated version.
Am I able to force the device to check the service worker for any new changes instead of waiting for the app to check for me?
Thanks!
There are two main points you should refine for instant update of your SW.
Shortly
sw.js
also from more often visited pages than your app's index page.skipWaiting()
method to activate your SW instantly.In detail
1)
navigator.serviceWorker.register
method callYou should note that newer verison of the SW will only be checked by a
navigator.serviceWorker.register()
call.And typically
register()
method will be called in one of your pages only. If that is the case, you should add that command to all of your pages. Because if user just opens another app without closing your PWA and then returns to your app after, say 2 hours, they will avoid the index page of the app where yourregister()
call probably is.So, my suggestion is to put it to many pages if not every. Downside is clients will make much more calls to
sw.js
. Upside is clients will retrieve last version of SW without losing time.2) Activating service worker
Service Worker's
activate
event will be called when SW no longer has any active clients. So new version will be activated only after every instance of the app but one closed, and the remaining tab is refreshed/one tab re-opened. See quote from MDN about the matter:And solution to this one would be using
skipWaiting()
method in conjunction withClients.claim()
Usage example of those taken from MDN. Click to see more about it on that page: