How to detect when XHR returns a cached resource?(如何检测 XHR 何时返回缓存的资源?)
问题描述
我想知道是否有一种方法可以检测何时从本地缓存返回响应?是否可以?
I'm wondering if there is a way how to detect when a response is returned from a local cache? Is it possible?
解决方案应该是通用的并且适用于无条件请求.在这种情况下,响应代码总是 200 OK,但是 XHR 为第二个请求返回一个缓存的资源(例如,第一个响应包含 Expires 标头,因此无需在到期日期之前向服务器请求新资源).
The solution should be general and work for unconditional requests. In this case, the response code is always 200 OK, but XHR returns a cached resource for the second request (e.g. the first response contains Expires header, so there is no need to ask a server for a new resource before the expiration date).
推荐答案
答案是Date header
The answer is Date header
- 如果日期标头在发送日期之前,则响应来自缓存.
- 如果日期标头晚于发送请求的日期,则响应是新的.
例如
- 来自缓存:请求于 11:00 发送,响应日期为 10:59
- 无缓存:请求在 11:00 发送,响应日期为 11:01
这篇关于如何检测 XHR 何时返回缓存的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检测 XHR 何时返回缓存的资源?


基础教程推荐
- 在 contenteditable 中精确拖放 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01