How to tell if an XMLHTTPRequest hit the browser cache

后端 未结 5 1533
别跟我提以往
别跟我提以往 2020-12-25 12:13

If it possible to tell (within javascript execution) if a GET XMLHTTPRequest hit the browser cache instead of getting its response from the server?

5条回答
  •  孤城傲影
    2020-12-25 13:07

    From http://www.w3.org/TR/2012/WD-XMLHttpRequest-20121206/

    For 304 Not Modified responses that are a result of a user agent generated conditional request the user agent must act as if the server gave a 200 OK response with the appropriate content. The user agent must allow author request headers to override automatic cache validation (e.g. If-None-Match or If-Modified-Since), in which case 304 Not Modified responses must be passed through. [HTTP]

    I find this rather vague. My assumption would be if a resource is conditionally requested, you would see the 304 response code. But, as I explained in another comment (source: https://developers.google.com/speed/docs/best-practices/caching), there might not even be a request if the last response server http header for that resource had set Cache-Control: max-age or Expires set sometime in the future. In this case, I'm not sure what ought to happen.

提交回复
热议问题