Service worker JavaScript update frequency (every 24 hours?)
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.
shailesh mishra
Updated on March 30, 2021Comments
-
shailesh mishra about 3 years
As per this doc on MDN:
After that it is downloaded every 24 hours or so. It may be downloaded more frequently, but it must be downloaded every 24h to prevent bad scripts from being annoying for too long.
Is the same true for Firefox and Chrome? OR does update to service worker javascript only happens when user navigates to site?
-
shailesh mishra almost 8 yearsthanks Jeff for nice explanation. clarified all queries.
-
Pankaj about 7 yearswhat happens if a service-worker call is intercepted in the fetch and cached. Does that also expire after a day.
-
Jeff Posnick almost 7 yearsA service worker's
fetch
event handler is not triggered when the page requests a service worker script to register. -
Simon over 6 years>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?
-
Jeff Posnick over 6 years"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.
-
csvan almost 3 yearsWhat happens if the service-worker.js file is REMOVED from the server and and installed SW tries to refresh itself? Will it deregister itself altogether, or else how do you get rid of an old SW which you do not want your clients to use at all anymore (rather than update it)?
-
Jeff Posnick almost 3 yearsNo, there is no automatic unregistration. See github.com/w3c/ServiceWorker/issues/204. You need to either respond to the service worker update request with a valid, if empty, JS response, or alternatively, execute code in the
window
context of the web pages that explicitly unregisters the SW.