Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do modern browsers cache ajax responses?

I have recently found that we use Cache-Control:no-cache in all the WebApi responses. I know that before browsers caching did vary a lot and you never knew if the browser can cache you json, so it was important.

As far as I remember now, no modern browser is caching ajax responses so this header is not needed. But I would really like to double check and ask here about the behavior, since could not found any recent posts on the topic.

So the question really is: Do we still need to set Cache-Control:no-cache for web api calls from SPA application and if yes, then which browsers do the caching?

like image 633
Ilya Chernomordik Avatar asked Jan 27 '17 11:01

Ilya Chernomordik


People also ask

Do browsers cache ajax?

Fact #1 : Ajax Caching Is The Same As HTTP Caching At this level, the browser doesn't know or care about Ajax requests. It simply obeys the normal HTTP caching rules based on the response headers returned from the server. If you know about HTTP caching already, you can apply that knowledge to Ajax caching.

Do browsers cache JSON responses?

Yes you can cache JSON responses.

Do browsers cache API response?

Browsers can cache the file but intermediate caches cannot. public . The response can be stored by any cache.

Do ajax requests get cached?

ajax docs: By default, requests are always issued, but the browser may serve results out of its cache. To disallow use of the cached results, set cache to false.


1 Answers

According to HTTP/1.1 specification browser HTTP cache key is a combination of a request HTTP method and URI. Browser doesn't take into consideration how request was made:

The primary cache key consists of the request method and target URI. However, since HTTP caches in common use today are typically limited to caching responses to GET, many caches simply decline other methods and use only the URI as the primary cache key.

So if requests that you perform are cacheble you still need that header value. Check HTTP Caching MDN page and Which webbrowsers use http/1.1 by default? question for more details.

Also you may need to add another headers for IE specifically. Check Make IE to cache resources but always revalidate question for more information.

like image 114
Leonid Vasilev Avatar answered Sep 24 '22 11:09

Leonid Vasilev