-
Notifications
You must be signed in to change notification settings - Fork 313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
consider fetching service worker scripts with no-cache by default #893
Comments
This could be tweak for production environments by adding an option to the registration: navigator.serviceWorker.register('worker.js', { cache: <policy> }); |
I'm pretty open to this, it does seem like a gotcha. I use & recommend The only reason that it feels "wrong" is because avoiding the cache is the exception, but it does feel like the pragmatic option in this case. Interested to know the opinion of those doing a lot of SW development. +@naskooskov @n8schloss @bmaurer. |
This would be really useful for companies providing services via service workers. We are deploying SW onto clients who can host scripts, but don't have a straight-forward way to set response headers. |
So, for us the current behavior is actually kind of useful. When we push out new code for the site we'll slowly ramp things up and some Facebook servers will be on the newest version while some will be on the old version. In the middle of the code push for any individual request it's just as likely that someone will hit new code or that they'll hit old code. For service workers this means that during code push we often see service workers constantly in the upgrade state. We have some mechanisms to ensure that we don't install an old version of the service worker, however we did still enter the install state more than is ideal. To solve this we actually cache the service worker script for an hour, this allows a level of stability that would otherwise be hard to achieve. |
(that being said, I do agree that defaulting to no cache with an option to re-enable the current behavior is probably a better developer experience) |
Coming from same perspective as @muralisr (we provide service workers to our clients and we are not in direct control), we find that they usually cache the service worker script similar to other JS files on their server, which tends to have a long cache time if they are doing things correctly. They should ideally add a separate server rule making the service workers script cache for a lesser time which they typically do not end up doing. I like @delapuente's way of declaring the cache time during the service worker registration process. |
Pre F2F notes: do we want to make this change? We shouldn't do it without offering an API to stick with the current behaviour. Should these options be part of the registration call, or in the activate event? |
F2F:
|
|
|
At the moment third party servers can use |
Regarding the opt-in: the concurrent fetch feature will sit on the registration object, so maybe this should too. |
I just ran in to this. There definitely needs to be a way to force updates for fetching the SW script. I have to reiterate that as middleware/framework developers we are always developing for servers we don't control, so tweaking HTTP headers is simply not possible. Note this is also the case if you don't control the host (e.g. Dropbox public folder, Neocities, Github pages etc). I just tried testing on a server which defaults all .js files to cache with a max-age of 50 days, presumably set up to use versioned URLs. Of course there's no sensible way to use versioned URLs on the sw.js script if the main page is cached by the SW itself, so I kept getting the stale sw.js returned from cache with no apparent workaround other than to change the server configuration, wait 24 hours or click the manual "update" button in Chrome devtools. In our case none of these are good options for our framework. I don't mind which way you choose to default this, but I think it's essential to have some way of saying "bypass cache for SW updates". |
Resolved: navigator.serviceWorker.register('/sw.js', {
useCache: true
}); |
Just realised it's pretty similar to my first comment. |
Applause for this decision!! |
I'm reviewing gecko code to implement this and came up with a couple questions:
@jakearchibald, what do you think? |
I asked Jake about the short-circuit in step 5.3. He suggests the short-circuit should only happen if the useCache value is the same:
|
Also, I agree we should have |
This introduces service worker registration's use cache field and its related APIs: options.useCache to register method and registration.useCache for ServiceWorkerRegistration objects. This changes the default cache mode of fetching SW scripts to "no-cache". After the change, 24 hours limit and job's force bypass cache flag rules are still enforced. register() method's options value set to { useCache: true } re-enables the previous default behavior provided before this change. Fixes #893.
This introduces service worker registration's use cache field and its related APIs: options.useCache to register method and registration.useCache for ServiceWorkerRegistration objects. This changes the default cache mode of fetching SW scripts to "no-cache". After the change, 24 hours limit and job's force bypass cache flag rules are still enforced. register() method's options value set to { useCache: true } re-enables the previous default behavior provided before this change. Fixes #893 #894.
I thought of a case where "hosting 3rd party libraries" is done in github or some similar environment where server-side setup is not accessible to devs. Let me know if that doesn't make sense.
No opposition. Just wanted to check if we can have a good compromise considering the end-to-end picture. But if opt-in to "no-cache" doesn't help much, I'd strongly support your idea. |
See w3c/ServiceWorker#893 (comment) ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=141919403
Chromium tracking bug: https://bugs.chromium.org/p/chromium/issues/detail?id=675540 |
FYI, we've implemented this and landed it in FF53 (includes WPT tests): https://bugzilla.mozilla.org/show_bug.cgi?id=1290944#c65 We are defaulting to no-cache right now with the This will ship in our release channel in April. If there is serious concern that we should switch the default please let us know before that point. |
I am in a situation where I want to build an immutable JavaScript secure loader. The secure loader only loads JS files with a suitable cryptographic signature. The 24 hours cache limit prevents me from building such a secure loader that persists longer than 24 hours. (The idea is that the secure loader would persist even if the servers that delivers Have you considered removing the 24 hours cache limit if explicitly opted-in with the |
@JustinDrake Your use-case has been raised in some other issues as well. The short story is that the use-case is at odds with the intent of ServiceWorkers and the web security model. If you can create a benevolent SW that can never be removed so it can survive when the server is compromised, then an attacker can construct a nefarious SW that can never be removed the one time the server is compromised. And browsers can't distinguish between benevolent and nefarious, just whether the TLS cert is valid. I think many of us are interested in this specific use-case, but SW is not going to be the solution for that on its own. The most practical solution at this time is to use the increasingly cross-browser WebExtensions efforts that are already built on a packaged/release model and APIs like webRequest that lets you intercept requests to specific sites and validate/enforce their contents, etc. It's also possible spec efforts like https://w3ctag.github.io/packaging-on-the-web/ may bear fruit at some point. |
Dear @jakearchibald need a little help here. I've been also set this up in my .htaccess to make the service-worker not being cached, yet not successful.
Thank you Best regards, Albert |
See w3c/ServiceWorker#893 (comment) ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=141919403
@albertsylvester if this is still a problem, can you post it to stackoverflow? I (or someone else) will answer it there. |
The F2F resolution was useCache would be an enum with the default to cache import scripts but not cache the top-level script. See issue #1104 |
We settled on https://w3c.github.io/ServiceWorker/#dom-serviceworkerregistration-updateviacache defaulting to "imports" and have tests: https://github.com/w3c/web-platform-tests/blob/master/service-workers/service-worker/registration-updateviacache.https.html. Closing. If I missed anything, please reopen. |
Related to https://www.netlify.com/blog/2018/06/28/5-pro-tips-and-plugins-for-optimizing-your-gatsby---netlify-site/#4-get-your-service-workers-um-working Recommandation is not to cache service worker file : w3c/ServiceWorker#893
…fy (#9680) Related to https://www.netlify.com/blog/2018/06/28/5-pro-tips-and-plugins-for-optimizing-your-gatsby---netlify-site/#4-get-your-service-workers-um-working Recommandation is not to cache service worker file : w3c/ServiceWorker#893 <!-- Q. Which branch should I use for my pull request? A. Use `master` branch (probably). Q. Which branch if my change is a bug fix for Gatsby v1? A. In this case, you should use the `v1` branch Q. Which branch if I'm still not sure? A. Use `master` branch. Ask in the PR if you're not sure and a Gatsby maintainer will be happy to help :) Note: We will only accept bug fixes for Gatsby v1. New features should be added to Gatsby v2. Learn more about contributing: https://www.gatsbyjs.org/docs/how-to-contribute/ -->
What does useCache mean exactly, to cache the service worker file or not? please explain |
…fy (gatsbyjs#9680) Related to https://www.netlify.com/blog/2018/06/28/5-pro-tips-and-plugins-for-optimizing-your-gatsby---netlify-site/#4-get-your-service-workers-um-working Recommandation is not to cache service worker file : w3c/ServiceWorker#893 <!-- Q. Which branch should I use for my pull request? A. Use `master` branch (probably). Q. Which branch if my change is a bug fix for Gatsby v1? A. In this case, you should use the `v1` branch Q. Which branch if I'm still not sure? A. Use `master` branch. Ask in the PR if you're not sure and a Gatsby maintainer will be happy to help :) Note: We will only accept bug fixes for Gatsby v1. New features should be added to Gatsby v2. Learn more about contributing: https://www.gatsbyjs.org/docs/how-to-contribute/ -->
@kgrosvenor I suppose by default browsers now use |
Lately I've found myself recommending to people that they use
Cache-control: no-cache
ormax-age: 0
on their service worker script resources. It seems best practice in order for service worker updates to be detected as devs expect.Should Update algorithm just fetch the service worker script with
Request.cache='no-cache'
by default? One could argue that an "update check" is semantically similar to pressing the refresh button.Edit: Removed the erroneous statement about the firefox http cache.
The text was updated successfully, but these errors were encountered: