79478653

Date: 2025-03-02 05:22:53
Score: 0.5
Natty:
Report link

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:

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.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: ppc