Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome doesn't send "If-Modified-Since"

I want browsers to always add (except first time) "If-Modified-Since" request header to avoid unnecessary traffic.

Response headers are:

Accept-Ranges:bytes
Cache-Control:max-age=0, must-revalidate
Connection:Keep-Alive
Content-Length:2683
Content-Type:text/html; charset=UTF-8
Date:Thu, 05 Apr 2012 13:06:19 GMT
Keep-Alive:timeout=15, max=497
Last-Modified:Thu, 05 Apr 2012 13:05:11 GMT
Server:Apache/2.2.21 (Red Hat)

FF 11 and IE 9 both send "If-Modified-Since" and get 304 in response but Chrome 18 doesn't and get 200.

Why? How to force Chrome to sent "If-Modified-Since" header? I do not know if it important or not but all requests going through HTTPS.

like image 749
GarryIV Avatar asked Apr 06 '12 07:04

GarryIV


People also ask

What does if-modified-since mean in HTTP?

The If-Modified-Since request HTTP header makes the request conditional: the server sends back the requested resource, with a 200 status, only if it has been last modified after the given date.

What are the If-modified-since and if-none-match headers used for?

The If-Modified-Since header is used to specify the time at which the browser last received the requested resource. The If-None-Match header is used to specify the entity tag that the server issued with the requested resource when it was last received.

Do you see an if-modified-since line in the HTTP GET?

Now inspect the contents of the second HTTP GET request from your browser to the server. Do you see an “IF-MODIFIED-SINCE:” line in the HTTP GET? If so, what information follows the “IF-MODIFIED-SINCE:” header? Answer: Yes.

What HTTP status code means that the requested file was found but has not changed since date provided in the IF-modified-since header?

If the status of a particular resource is 304 Not Modified, this means that the file has not changed and there is no need to download it again.


2 Answers

I've been chasing this issue for some time, thought I'd share what I found.

"The rule is actually quite simple: any error with the certificate means the page will not be cached."

https://code.google.com/p/chromium/issues/detail?id=110649

If you are using a self-signed certificate, even if you tell Chrome to add an exception for it so that the page loads, no resources from that page will be cached, and subsequent requests will not have an If-Modified-Since header.

like image 132
Neek Avatar answered Sep 21 '22 08:09

Neek


I just now found this question, and after puzzling over Chrome's If_Modified_Since behavior, I've found the answer.

Chrome's decision to cache files is based on the Expires header that it receives. The Expires header has two main requirements:

  1. It must be in Greenwich Mean Time (GMT), and
  2. It must be formatted according to RFC 1123 (which is basically RFC 822 with a four-digit year).

The format is as follows:

Expires: Sat, 07 Sep 2013 05:21:03 GMT

For example, in PHP, the following outputs a properly formatted header.

$duration = time() + 3600 // Expires in one hour.
header("Expires: " . gmdate("D, d M Y H:i:s", $duration) . " GMT");

("GMT" is appended to the string instead of the "e" timezone flag because, when used with gmdate(), the flag will output "UTC," which RFC 1123 considers invalid. Also note that the PHP constants DateTime::RFC1123 and DATE_RFC1123 will not provide the proper formatting, since they output the difference to GMT in hours [i.e. +02:00] rather than "GMT".)

See the W3C's date/time format specifications for more info.

In short, Chrome will only recognize the header if it follows this exact format. This, combined with the Cache-Control header...

header("Cache-Control: private, must-revalidate, max-age=" . $duration);

...allowed me to implement proper cache control. Once Chrome recognized those headers, it began caching the pages I sent it (even with query strings!), and it also began sending the If_Modified_Since header. I compared it to a stored "last-modified" date, sent back HTTP/1.1 304 Not Modified, and everything worked perfectly.

Hope this helps anyone else who stumbles along!

like image 31
theftprevention Avatar answered Sep 19 '22 08:09

theftprevention