Question

Hello fellow earthlings! For some time I'm fantasizing possible solutions, but i'm out of clues for this strange problem. Let me describe the reason, then the problem:

In order to combine the javascripts files to reduce http request, I put them into php, a headache-less simple little yet happy solution that will cache for half a year. (Would life be lot nicer if more things would work like that?) A fire and forget solution. So I tought...

The website works fine and all, except, that when viewing the headers on the php-generated js file, it seems that this showstopper appeared:

An If-Modified-Since conditional request returned the full content unchanged.

What's wrong here in the php generated file combined.js? Any suggestive answers are much appreciated!

below file < allcombined.js >

<?php
header("Content-type: text/javascript; charset=UTF-8");
header("Expires: " . gmdate ("D, d M Y H:i:s", time() + 2419200) . " GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s", time() - 604800) . " GMT");

ob_start("compress");

include('script1.js');
include('script2.js');
include('script3.js');

ob_end_flush(); 
?>
Was it helpful?

Solution

To handle "If-Modified-Since" headers, you need to parse that header and compare it to your last modification date. This isn't done automatically unless you have a HTTP cache like Varnish in front of your web server. In PHP, if the date is satisfactory, then do not render a body, instead return a 304 Not Modified response. Here is a good example of this being done. [php.net]

The reason you're getting more requests for your asset than you expect is because you haven't set Cache-Control or Pragma headers. You probably want to do:

header("Cache-Control: public, max-age=2419200");
header("Pragma: public");

You may not want to set a max-age this long if you want the client to check in for updates now and again...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top