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?
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.
Yes you can cache JSON responses.
Browsers can cache the file but intermediate caches cannot. public . The response can be stored by any cache.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With