Mobile app version of vmapp.org
Login or Join
Jennifer507

: How should cache control be set in the requests I know that Cache-Control headers can be set on the pages coming out from the web-server (as part of the response). Does anyone know how Cache-Control

@Jennifer507

Posted in: #Cache

I know that Cache-Control headers can be set on the pages coming out from the web-server (as part of the response). Does anyone know how Cache-Control header should be set on the request? Should it be done via client-side scripting? If yes, how?

10.05% popularity Vote Up Vote Down


Login to follow query

More posts by @Jennifer507

5 Comments

Sorted by latest first Latest Oldest Best

 

@Voss4911412

Cache-Control in request allows client to override server's header suggestions. For example:


Server sets Cache-Control: 'max-age=604800, public'
but you request with

Cache-Control: 'no-cache'


Standard browsers will make call to server for same resource, even though server suggested caching.


Server sends

Cache-Control: 'no-cache'


You may add
Cache-Control: 'max-age=604800, public' in your request header to force fetch from cache before sending http request to server.

So how do you control request header? Either by tweaking browser (absurd), or in the ajax requests fired by you (common usecase).

jQuery example:

<script>
$(function(){
$.ajax({
type: 'GET',
url:"http://theypi.net/img/reflection/en/86.jpg",
cache: true,
headers: {
'Cache-Control':'max-age=604800, public'
},
});
});


Request-Headers through this ajax:

Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control max-age=604800, public
Host theypi.net
Origin localhost Referer localhost/admin/test.php User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:41.0) Gecko/20100101 Firefox/41.0


Note: If you're using CORS, you also need to allow the header you want server to accept

I use the .htaccess on server

<FilesMatch ".(php|jpg)$">
Header set Access-Control-Allow-Origin *
Header set Access-Control-Allow-Methods: GET
Header set Access-Control-Allow-Headers: Cache-Control
</FilesMatch>

10% popularity Vote Up Vote Down


 

@Moriarity557

All right, this is an old question, but here goes:

Cache control headers mainly have meaning for responses, i.e. what the server sends to the client. Setting cache control headers on the request is usually incorrect and meaningless.

The origin server sets the cache control headers, based on what is needed for the specific content. The person or organization managing the origin server configures this.

Caveats:

It is possible for the client software to disobey the cache control headers in violation of the RFCs, and store content which is explicitly marked cache-control: no-store and expires: <date-in-the-past> . Such behavior is wrong, but since the client software (browser) is under the end users control, it's up to him to address this.

It is also possible for intermediary http caches (proxies) to disobey the caching headers on responses. And similarly, the individual or organization controlling the intermediary cache should fix this (and end users shouldn't permit intermediary caches which they don't trust).

Special case:

In some circumstances, clients can specify certain criteria for which responses the client is willing to accept. See RFC 2616 section 13.1.6 Client-controlled Behavior. This is rarely used, and I wouldn't be surprised if a few intermediary caches out in the real world (less common corporate firewalls / anti-virus gateways / WAN accelerators) have bugs in the handling of this.

Lastly: I always recommend Mark Nottingham's Caching Tutorial for Web Authors and Webmasters as a really good, practical introduction to HTTP caching mechanisms.

10% popularity Vote Up Vote Down


 

@Yeniel560

Cache-control requests are managed by the browser, there is no real reason to try and modify them. Firstly, for any URL visited, and all external requests from that page (images, scripts, styles) there is no way to control the requests.

For AJAX requests made after the page has loaded, it is possible to send specific headers (as detailed here), but it's complex and probably pointless. You don't say what your use-case is but you should probably just make your server send appropriate headers for the files in the first place.

10% popularity Vote Up Vote Down


 

@Kevin317

I admit there might be cases when you want to alter the request headers, and it is easy enough to do for AJAX requests. I don't know any way of doing it for the "normal" click-on-link-request from the browser. Cf stackoverflow.com/questions/374885/can-i-change-the-headers-of-the-http-request-send-by-the-browser
But why would you alter the request headers? Are there anything like a cached request? The whole idea of constructing a request and send it to the server seems to be dynamic to me. What is it you want to achieve?

10% popularity Vote Up Vote Down


 

@Berryessa370

I set cache control through PHP server side. At the top of your page, use the following code:

<?
// Prevent caching
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>


Using a date from the past ensures that the newest version of the page will always be downloaded.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme