服务人员响应缓存头 [英] Service Worker Response Cache Headers

查看:67
本文介绍了服务人员响应缓存头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚服务工作者如何在响应中缓存标头。我现在已经实施了几个服务人员,但是从来不用担心担心缓存标头,应该为项目缓存多长时间等。我现在在企业生产站点上实施它,这实际上很重要。 p>

基本上,当使用服务工作者时,http缓存是否被完全绕过?



然后,我需要构建一个框架来处理资源过期/无效,例如用于我们的http缓存?还是我在说垃圾?



如果有人可以提供一些澄清的信息,那将非常有帮助。我认为这有3种可能的情况:



A)。网络请求=>服务工作者获取=>(浏览器缓存?)< =>服务器



B)。网络请求< =>(浏览器高速缓存?)< =>服务人员取回< =>服务器



C)。网络请求=>服务人员获取< =>服务器



我已经在本地进行了测试,似乎C)。是正确的实现,因此我们开发人员已经牺牲了缓存头/持续时间抽象来进行控制。



我对此表示满意,只想在我开始之前澄清一下,

解决方案

A)是正确的模型。如果服务工作者控制页面,则所有网络请求都将在咨询浏览器缓存或网络之前触发服务工作者的 fetch 事件处理程序。



反过来,无论何时服务人员发出网络请求,要么显式地通过 fetch()要么隐式地通过 cache.add() / cache.addAll(),首先查询浏览器的传统缓存以获取响应。只有在浏览器缓存中没有有效响应的情况下,才会向服务器发出网络请求。



这有时对您有利,有时会使您暴露



https://jakearchibald.com/2016/caching-best-practices/ ,具体涵盖了应避免的事情采取方法这种行为的优点


I'm trying to figure out how the service worker works in regards to cache headers in responses. I have implemented a couple of service workers now but have never had to bother worrying about caching headers, how long items should be cached for etc. I'm now implementing it on an enterprise production site, whereby this stuff actually really matters.

Basically when using a service worker, is the http cache completely bypassed?

Do I then need to build a framework to handle resource expiration/invalidation like the http cache used to do for us? Or am I talking rubbish?

Would be super helpful if someone could provide some clarification of this. The way I see it there are 3 potential scenarios:

A). Network request => Service worker fetch => (Browser cache?) <=> Server

B). Network request <=> (Browser cache?) <=> Service worker fetch <=> Server

C). Network request => Service worker fetch <=> Server

I've tested this locally and it seems that C). is the correct implementation, whereby we the developer have sacrificed cache header/duration abstraction for control.

I'm fine with this, just want it clarifying before I run off and build a framework for reading and honouring caching headers in the service worker.

解决方案

A) is the correct model. If a service worker controls a page, all network requests will trigger the fetch event handler of the service worker prior to consulting the browser cache or the network.

In turn, any time the service worker makes a network request, either explicitly via fetch() or implicitly via cache.add()/cache.addAll(), the browser's "traditional" cache is consulted for a response first. A network request to the server will only be made if there isn't a valid response in the browser cache.

This sometimes works in your favor, and sometimes can expose you to subtle bugs if you don't expect that behavior.

There's a very detailed explanation of this interaction at https://jakearchibald.com/2016/caching-best-practices/, specifically covering things to avoid and ways to take advantage of this behavior.

这篇关于服务人员响应缓存头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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