开发者

PHP content caching for javascript and css

开发者 https://www.devze.com 2022-12-16 11:04 出处:网络
I have a unique problem, which is proving difficult to solve using google. I am consolidating all of my javascript and css into separate php files, which use require_once() to pull the contents of the

I have a unique problem, which is proving difficult to solve using google. I am consolidating all of my javascript and css into separate php files, which use require_once() to pull the contents of the files in. The javascript file looks something like this:

<?php
header('Content-Type: text/javascript');
require_once('jquery.form.js');
require_once('jquery.jqtransform.js');
require_once('jquery.validate.js');
?>

My specific problem is that web browsers will 'see' that this is a dynamic page, because of the php file extension, and then request the content anew each time a page on the site is loaded. What I am trying to do is get the time of last request from the browser, and then check each file modification time to see if I really do need to send the file contents again. It is proving difficult to find the time of last request by the user. Also, I have not yet started to tackle the problem of finding the last modified date of the files that are included, so 开发者_Python百科if there is information regarding finding the file details of a file on the server, that would also be appreciated.

Just to be clear, the reason I am doing this is because (I think) it takes advantage of the gzip compression better than individually gzipped files.

Thanks in advance


I wrote a series of posts about this issue specifically. See Supercharging Javascript in PHP and Supercharging CSS in PHP. These cover:

  • Combining files;
  • Gzipping best practices;
  • Caching best practices; and
  • Versioning output.


Your premise is incorrect. Browsers don't "see" the PHP file extension and decide not to cache things. See http://www.enhanceie.com/redir/?id=httpperf for information on how browsers really work.

You should set an ETAG on your response, and then you can simply check the If-None-Match request header and return a 304 if the content is unchanged.


Browsers don't determine if a page or a file is dynamic or static by its extension. Its headers do. Just set the proper headers so the browser knows it can cache the results.

Also, ditch the closing ?>. It's not required and is bad practice.


Alister Bulman just mentioned a neat library solution for this problem but placed it as a comment. I'm repeating his comment as an answer since I found it valuable:

Minify - code.google.com/p/minify - is a library designed to do what is required here - concatenate the files and send the appropriate headers, also trimming down the contents, and quite possibly gzipping them, while caching the results on disk. – Alister Bulman Jan 10 '10 at 10:44


You can enable auto-gzipping of files using apache mod_deflate.

You can also use apache mod_rewrite to refer to these files in the html as js files and redirect the request to the php files, avoiding your server caching issues.

Something like this:

RewriteEngine On
RewriteRule (.*).js $1.php

Put this code in a .htaccess file in your directory.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号