1 Answer
1

Note: As of Firefox 57, and Chrome 68, as well as the versions of Safari and Edge that support service workers, the default behavior has changed to account for the updated service worker specification. In those browsers, HTTP cache directives will, by default, be ignored when checking the service worker script for updates. The description below still applies to earlier versions of Chrome and Firefox.

Every time you navigate to a new page that's under a service worker's scope, Chrome will make a standard HTTP request for the JavaScript resource that was passed in to the navigator.serviceWorker.register() call. Let's assume it's named service-worker.js. This request is only made in conjunction with a navigation or when a service worker is woken up via, e.g., a push event. There is not a background process that refetches each service worker script every 24 hours, or anything automated like that.

This HTTP request will obey standard HTTP cache directives, with one exception (which is covered in the next paragraph). For instance, if your server set appropriate HTTP response headers that indicated the cached response should be used for 1 hour, then within the next hour, the browser's request for service-worker.js will be fulfilled by the browser's cache. Note that we're not talking about the Cache Storage API, which isn't relevant in this situation, but rather standard browser HTTP caching.

The one exception to standard HTTP caching rules, and this is where the 24 hours thing comes in, is that browsers will always go to the network if the age of the service-worker.js entry in the HTTP cache is greater than 24 hours. So, functionally, there's no difference in using a max-age of 1 day or 1 week or 1 year—they'll all be treated as if the max-age was 1 day.

Browser vendors want to ensure that developers don't accidentally roll out a "broken" or buggy service-worker.js that gets served with a max-age of 1 year, leaving users with what might be a persistent, broken web experience for a long period of time. (You can't rely on your users knowing to clear out their site data or to shift-reload the site.)

Some developers prefer to explicitly serve their service-worker.js with response headers causing all HTTP caching to be disabled, meaning that a network request for service-worker.js is made for each and every navigation. Another approach might be to use a very, very short max-age—say a minute—to provide some degree of throttling in case there is a very large number of rapid navigations from a single user. If you really want to minimize requests and are confident you won't be updating your service-worker.js anytime soon, you're free to set a max-age of 24 hours, but I'd recommend going with something shorter on the off chance you unexpectedly need to redeploy.

what happens if a service-worker call is intercepted in the fetch and cached. Does that also expire after a day.
– PankajMay 22 '17 at 12:38

1

A service worker's fetch event handler is not triggered when the page requests a service worker script to register.
– Jeff PosnickJun 1 '17 at 20:49

3

>they'll all be treated as if the max-age was 1 day. doesn't this pretty much defeat the purpose of service workers? if my app is disconnected from the internet for 24 hours it stops working and there's nothing I can do about it? All just to save users from having to know about clearing their cache, or developers from adding a cache-buster to their urls. Am I missing something?
– SimonDec 1 '17 at 13:19

3

"If my app is disconnected from the internet for 24 hours it stops working" is not correct. Your previously cached content, and previously cached service worker JS files, will continue to be used whenever you're offline. This post contains details that pertain to when you're online, and how frequently updates are checked for then.
– Jeff PosnickDec 1 '17 at 14:42