如果您使用的是Service Workers,您是否还需要缓存控制标头? [英] If you are using Service Workers do you still need cache-control headers?

查看:76
本文介绍了如果您使用的是Service Workers,您是否还需要缓存控制标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在用例比Service Worker优先使用 cache-control 标头的情况?两者都使用有什么好处(除了不支持跨浏览器的软件之外)?

Is there any use-case where the use of cache-control headers should be preferred over Service Workers? Is there any benefit using both of them (apart from the fact the SW are not cross-browser supported) ?

推荐答案

绝对.HTTP缓存由HTTP缓存头控制,位于网络和服务工作者之间.从服务工作者发起的所有 fetch 请求仍然使用HTTP缓存.

Absolutely. HTTP cache, which is controlled by HTTP cache headers sits between the network and the service worker. All fetch requests initiated from the service worker still use HTTP cache.

假设您的脚本的最长使用期限为 max-age .大多数Service Worker每次安装时都会重新填充其缓存,也就是说,每次Service Worker脚本中发生任何更改时都会重新填充它们.但是,如果正确配置了HTTP缓存标头,则要包含在Service Worker缓存中的许多资源仍将存在于HTTP缓存中,并且可以在不涉及网络的情况下进行获取.

Let's say you have a script with long max-age. Most Service Workers repopulate their caches every time they are installed, which is to say, every time something/anything changes in the service worker script. However, if HTTP cache headers are properly configured many resources to be included in Service Worker cache will be still present in HTTP cache and can be fetched without involving the network.

正确配置的HTTP标头对于中间缓存代理(CDN)正常工作也是必不可少的.代理对Service Worker及其缓存一无所知.

Properly configured HTTP headers are also essential for intermediary caching proxies (CDNs) to work properly. Proxies do not know anything about Service Worker and its caches.

有关更多详情,请参见缓存最佳做法和技术.杰克·阿奇博尔德(Jake Archibald)的最大年龄陷阱.

More on this in Caching best practices & max-age gotchas by Jake Archibald.

这篇关于如果您使用的是Service Workers,您是否还需要缓存控制标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆