Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if an XMLHTTPRequest hit the browser cache

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

like image 398
rjkaplan Avatar asked Dec 09 '12 00:12

rjkaplan


People also ask

Does the browser cache XHR?

XHR responses are cached automatically in the browser cache if your HTTP cache headers permit it.


1 Answers

From the XMLHttpRequest spec:

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.

In other words, the browser will always give status code 200 OK, even for requests that hit the browser cache.

However, the spec also says:

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.

So, there is a workaround to make the 304 Not Modified responses visible to your JavaScript code.

like image 65
Feross Avatar answered Sep 27 '22 19:09

Feross