Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is caching in browser automatic?

Tags:

http

caching

I have a JavaScript app that sends requests to REST API, the responses from server have cache headers (like ETag, cache-control, expires). Is caching of responses in browser automatic, or the app must implement some sort of mechanism to save the data?

like image 905
zdarsky.peter Avatar asked Jun 13 '15 22:06

zdarsky.peter


People also ask

Does browser cache clear automatically?

By default - any cache associated with active sessions that expire on browser close will be cleared with the close down of chrome. Any content set to not cache by the web server (meta tags) will get dropped on page/domain change.

How does caching in browsers work?

The basic idea behind it is the following: The browser requests some content from the web server. If the content is not in the browser cache then it is retrieved directly from the web server. If the content was previously cached, the browser bypasses the server and loads the content directly from its cache.

Are GET requests automatically cached?

Usually, browsers treat all GET requests as cacheable. POST requests are not cacheable by default but can be made cacheable if either an Expires header or a Cache-Control header with a directive, to explicitly allows caching, is added to the response.

How long do browsers keep cache?

If a user stops using the browser it is indefinitely. If he/she uses the browser rarely, it will be until the expiration - either by internal policy or by HTTP headers. If he/she uses the browser heavily, it can be 12 minutes or even less.


2 Answers

An AJAX request is no different from a normal request - it's a GET/POST/HEAD/whatever request being sent by the browser, and it is handled as such. This is confirmed here:

The HTTP and Cache sub-systems of modern browsers are at a much lower level than Ajax’s XMLHttpRequest object. 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.

As per the jQuery documentation, caches can also be invalidated in at least one usual way (appending a query string):

cache (default: true, false for dataType 'script' and 'jsonp')

Type: Boolean

If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.

So in short, given the same headers, AJAX responses are cached the same way as other requests.

like image 157
YamiTenshi Avatar answered Oct 19 '22 14:10

YamiTenshi


Browser handles automatically cache of resources. What you seem to be asking about is the actuall response from the server.

You will need to set that up yourself in your application. You can do so both on front-end and backend.

Most of JS frameworks have cache control implemented, for example:

jQuery

$.ajaxSetup({
    // Disable caching of AJAX responses
    cache: false
});

AngularJS

$http.defaults.cache = false;

etc.

On backend it really depends on what language you using, what server engine etc.

Checkout Memcached for example http://memcached.org/

As with anything with web development there are odd things here and there, for example some IE versions are automatically chaching requests and you have to add unique id to the url to prevent that.

like image 39
Tomas Avatar answered Oct 19 '22 13:10

Tomas