Dewan159
Dewan159

Reputation: 3074

caching page assets

For the sake of reducing HTTP requests for the UI assets (JS/CSS), I wrote a PHP script to combine these files and offer it as : "assets/basic,login,jquery-ui-10.css" and this works fine yet I decided to take it a little bit farther and ask the browser to cache these Assets Combiner (as I call them) by adding an expire header in the PHP script. Of course here i am making use of htacess RewriteRule functionality to reference for this script assets.php?files=basic,login,jquery-ui&type=css&version=10 to the one above to make it look like a CSS file for the browser to cache it (and it looks good this way :)). I also use the "version" parameter to force the browser to update the file, but that does not happen !!, I have added these header information to cache the file :

    header("HTTP/1.1 304 Not Modified");
    header("Expires: Sat, 1 Jun 2015 12:00:00 GMT");
    header("Cache-Control: must-revalidate");

Yet when I try change the version parameter in the URL string above it doesn't download the new file content though the file name is changed > for the browser

What am I doing wrong here, how can I force the browser to re-download the new file > which is with a different name (but still sends the above headers)

Thanks in advance

Upvotes: 0

Views: 654

Answers (2)

symcbean
symcbean

Reputation: 48357

As Quentin says, you MUST only send a 304 response in response to a IF-Modified-Since OR a If-None_match request. By sending a 304 response you are telling the browser to use the cached copy even if it doesn't have one

In practice, using 304 responses can actually defeat the purpose of improving performance and it's usually better to ignore the conditional request and make a 200 response with content and new caching instructions

If you want the content to be cached then send a max-age as well as an Expires header (the expires header is only required for HTTP/1.0 clients - all modern browsers implement HTTP1/1 - even when you explicitly tell them to use HTTP/1.0).

e.g.

header("Expires: Sat, 1 Jun 2015 12:00:00 GMT");
header("Cache-Control: max-age=94608000,must-revalidate");

Upvotes: 1

Quentin
Quentin

Reputation: 943142

You can't. The whole point of "Not Modified" is that it tells the browser the document is the same as the last version it got and that it should not download it.

AFAIK, you should only send it in response to an If-Modified-Since request.

Upvotes: 1

Related Questions