Using .htaccess, I'm setting PHP handler to all my .css and ,js in order to output user-agent based code:
AddHandler application/x-httpd-php .css .js
For example:
<?PHP if ($CurrentBrowser == 'msie') { ?>
.bind('selectstart', function(event) { ... })
<?PHP } ?>
So, in fact, my code files are dynamically created but can be considered static files. That's because, once they have been compiled for the first time, browsers can get them back from cache and reuse them until I change their content. That's why I'm using fingerprinting/versioning and long time expiration on them:
[INDEX.PHP]
<script type="application/javascript" src="<?PHP echo GetVersionedFile('/script.js'); ?>"></script>
<script type="application/javascript" src="/script.1316108341.js"></script>
[.HTACCESS]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule "^(.+)\.(\d+)\.(css|js)$" $1.$3 [L]
The problem is that those files, even if I send them with a proper header, are never being cached by any browser (I never get a 304 code, always 200). This is a log of my server responses:
[CHROME]
Request URL:http://127.0.0.1:8888/script.1316108341.js
Request Method:GET
Status Code:200 OK
-----
Cache-Control:max-age=31536000, public
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:6150
Content-Type:application/javascript
Date:Thu, 15 Sep 2011 21:41:25 GMT
Expires:Fri, 14 Sep 2012 21:41:25 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.17 (Win32) PHP/5.3.6
Vary:Accept-Encoding
X-Powered-By:PHP/5.3.6
[MOZILLA]
Request URL:http://127.0.0.1:8888/script.1316108341.js
Request Method:GET
Status Code:200 OK
-----
Date Thu, 15 Sep 2011 21:43:26 GMT
Server Apache/2.2.17 (Win32) PHP/5.3.6
X-Powered-By PHP/5.3.6
Content-Encoding gzip
Vary Accept-Encoding
Cache-Control max-age=31536000, public
Expires Fri, 14 Sep 2012 21:43:26 GMT
Content-Type application/javascript
Content-Length 6335
Keep-Alive timeout=5, max=100
Connection Keep-Alive
-----
Last Modified Thu Sep 15 2011 23:43:26 GMT+0200 (= time i loaded the page) (???)
Last Fetched Thu Sep 15 2011 23:43:26 GMT+0200 (= time i loaded the page) (???)
Expires Fri Sep 14 2012 23:43:26 GMT+0200
Data Size 6335
Fetch Count 10
Device disk
What could be the problem? How can I force caching on these files? Many, many thanks!
Since the requests for PHP and CSS files are being handled by PHP, your PHP code with its conditionals is being executed each time.
Apache/PHP have no idea if the content is cacheable or if it should be regenerated so it executes your PHP code each time.
If you send the last modified header, or use your versioning/fingerprinting method, then it is your responsibility in your PHP script to check the fingerprint or version and determine if it is still valid. If so, then you can send a 304 Not Modified header and terminate any further processing. You can also check the request headers for a Last-Modified tag and use that method.
Another approach would be to cache the response for various browsers and dates to a file so you can serve that file up for first time users rather than regenerating it with php. Then you can check the modification time of that file to determine if you can send a 304 header.
This SitePoint article explains several methods of using PHP to cache. Hope that helps.