简体   繁体   English

如果您使用的是Service Workers,您是否还需要缓存控制标头?

[英]If you are using Service Workers do you still need cache-control headers?

Is there any use-case where the use of cache-control headers should be preferred over Service Workers? 是否有任何用例比服务工作者优先使用cache-control标头? Is there any benefit using both of them (apart from the fact the SW are not cross-browser supported) ? 两者都使用有什么好处(除了不支持跨浏览器的事实)?

Absolutely. 绝对。 HTTP cache, which is controlled by HTTP cache headers sits between the network and the service worker. HTTP缓存由HTTP缓存头控制,位于网络和服务工作者之间。 All fetch requests initiated from the service worker still use HTTP cache. 从服务工作者发起的所有fetch请求仍将使用HTTP缓存。

Let's say you have a script with long max-age . 假设您的脚本的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. 大多数Service Worker每次安装时都会重新填充其缓存,也就是说,每次Service Worker脚本中发生任何更改时都会重新填充它们。 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缓存标头,则要包含在Service Worker缓存中的许多资源仍将存在于HTTP缓存中,并且可以在不涉及网络的情况下进行获取。

Properly configured HTTP headers are also essential for intermediary caching proxies (CDNs) to work properly. 正确配置的HTTP标头对于中间缓存代理(CDN)正常工作也是必不可少的。 Proxies do not know anything about Service Worker and its caches. 代理对Service Worker及其缓存一无所知。

More on this in Caching best practices & max-age gotchas by Jake Archibald. 杰克·阿奇博尔德(Jake Archibald)的《 缓存最佳实践和最大寿命陷阱》中对此有更多介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM