Safari seems to remember the URL from which the Service Worker was initially installed, and as long as you register the Service Worker with this same address, it imposes a systematic network validation, even with updateViaCache: “all”
.
Why does Safari do this?
It's probably a security measure to avoid a scenario where a site registers a Service Worker with updateViaCache: “all”
. This Service Worker would never be updated and would remain cached indefinitely, unless the URL changed.
Safari therefore seems to have a different policy from other browsers:
updateViaCache: “all”
.As a solution, I added these lines to the previous script :
if (!initialRegistration) {
await navigator.serviceWorker.register('/service-worker.js', { updateViaCache: 'all' });
}
When the client downloads the application for the first time, it doesn't yet have a registered Service Worker, so I register what I call the “initial Service Worker” to set the URL to mydomain.com/service-worker.js
. Then, immediately afterwards, I register the Service Worker again (the same one) with a different URL, as this one mydomain.com/service-worker.js?v=NEW_VERSION
. This process makes updateViaCache operational as soon as it's installed.