在服务工作者/fetch()中识别HTTP 304 [英] Recognize HTTP 304 in service worker / fetch()

查看:130
本文介绍了在服务工作者/fetch()中识别HTTP 304的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个服务人员,该服务人员总是以缓存中的数据作为响应,然后在后台将请求发送到服务器.如果服务器使用 HTTP 304-未修改进行响应,那么一切都很好,如果服务器使用 HTTP 200 进行响应,则意味着数据已更改并且新文件已放入缓存,还会通知用户并要求刷新页面.

I build a service worker which always responds with data from the cache and then, in the background, sends a request to the server. If the server responds with HTTP 304 - not modified everything is fine, if the server responds with HTTP 200, that means the data was changed and the new file is put into the cache, also the user is notified and asked for a page refresh.

我使用 not-modified-since / last-modified 标头来确保客户端获得最新版本.当通过 fetch()发送请求时,请求将通过HTTP缓存传递给网络-当响应到达客户端时,响应也会通过HTTP缓存.问题是,当响应的状态为 304 时-未经修改,HTTP缓存将使用缓存的版本对服务工作者进行响应,并用 200 替换状态(如前所述)在获取规范-HTTP-network-or-cache-fetch ).在服务工作者中,不可能找出 200 响应最初是由服务器发送的(用户需要更新)还是由缓存发送的,并且服务器最初以 304 (已经加载了最新版本).

I use the not-modified-since / last-modified headers to make sure the client gets the most up-to-date version. When a request is sent via fetch() the request passes the HTTP-cache on it's way to the network - also the response passes the HTTP cache when it arrives on the client. The problem is when the response has the status 304 - not modified the HTTP cache responds to the service worker with the cached version and replaces the status with 200 (as it is described in the fetch specification - HTTP-network-or-cache-fetch). In the service worker there is no possibility to find out whether the 200 response was initially sent by the server (the user needs to be updated) or it was sent by the cache and the server originally responded with 304 (most up-to-date version is already loaded).

有一个缓存模式标志可以设置为 no-cache ,但这在请求发送到服务器时也会绕过HTTP缓存,这意味着 if-modified-since 标头未设置,服务器没有机会找出客户端具有哪个版本.另外,此标志仅受firefox每晚支持.

There is the cache-mode flag which can be set to no-cache, but this bypasses the HTTP-cache also when the request is sent to the server, which means that the if-modified-since header is not set and the server has no chance to find out which version the client has. Also this flag is only supported by firefox nightly right now.

我认为最好的解决方案是在服务器以 200 响应时,设置一个自定义HTTP标头,例如 x-was-modified .可以在服务工作者中访问此自定义标头,即使HTTP缓存用 200 替换了 304 状态,该自定义标头也可以用于查找资源是否已更新.

I think the best solution is to set a custom HTTP header like x-was-modified when the server responds with 200. This custom header can be accessed in the service worker and can be used to find out whether a resource was updated or not - even if the HTTP cache replaces the 304 status with 200.

  • 这是合法的解决方案/解决方法吗?是否有解决此问题的建议方法?
  • 在实现服务工作者缓存时,我什至应该依赖应该用于处理HTTP缓存的HTTP标头吗?还是我应该使用自定义的 x-if-modified-since / x-last-modified 标头,并使用indexedDB在客户端上存储信息并将其附加到每个请求中?
  • 如果缓存中有最新版本,为什么为什么 fetch()甚至用 200 替换 304 代码?
  • Is this a legit solution/workaround? Are there any suggested approaches to solve this problem?
  • Should I even rely on HTTP headers which are supposed to handle the HTTP cache when implementing the service worker cache? Or should I rather use custom x-if-modified-since / x-last-modified headers and use indexedDB to store the information on the client and append it to each request?
  • Why does fetch() even replace the 304 code with 200 if there is a up-to-date version in the cache?

推荐答案

您不能依靠状态码(304和200)来确定是否有所更改.如果您的代码的其他部分请求相同的资源,从而更新了浏览器的缓存,该怎么办?

You can’t rely on the status code (304 vs. 200) to determine whether something has changed. What if some other part of your code requests the same resource, thus updating the browser’s cache?

相反,只需将响应的 Last-Modified 标头与您在 If-Modified-Since 中发送的内容或您在 Last-Modified中最后看到的内容进行比较即可.如果这些值不匹配,则说明有所更改.

Instead, simply compare the response’s Last-Modified header to what you sent in If-Modified-Since, or whatever you last saw in Last-Modified. If the values don’t match, something has changed.

为了获得更高的精度(如果数据可以在1秒内更改多次),请考虑使用 ETag 而不是 Last-Modified .

For more precision (if the data can change several times in 1 second), consider using ETag instead of Last-Modified.

如果缓存中有最新版本,为什么 fetch()甚至用 200 替换 304 代码?

因为通常人们只是想获取新鲜的内容,而不管它来自何处.304响应仅对实现自己的HTTP缓存的用户感兴趣.

Because usually people just want to get fresh content, regardless of where it comes from. A 304 response is only interesting to those who implement their own HTTP caches.

这篇关于在服务工作者/fetch()中识别HTTP 304的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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