本文介紹了如何檢測 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 何時返回緩存的資源?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!