admin管理员组文章数量:1317898
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 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 pression better than individually gzipped files.
Thanks in advance
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 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 pression better than individually gzipped files.
Thanks in advance
Share Improve this question edited Jan 10, 2010 at 3:08 cletus 625k169 gold badges919 silver badges945 bronze badges asked Jan 10, 2010 at 0:25 Scott M.Scott M. 7,34731 silver badges39 bronze badges 2- Minify - code.google./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 Commented Jan 10, 2010 at 10:44
- I thought about just concatenating and minifying, but I would like to retain some dynamic control over the contents, especially when I'm still developing the site and changing my code constantly. – Scott M. Commented Jan 11, 2010 at 4:12
5 Answers
Reset to default 3I 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./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 ment. I'm repeating his ment as an answer since I found it valuable:
Minify - code.google./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.
本文标签: PHP content caching for javascript and cssStack Overflow
版权声明:本文标题:PHP content caching for javascript and css - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742035811a2417285.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论