简体   繁体   English

用于javascript和css的PHP内容缓存

[英]PHP content caching for javascript and css

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: 我正在将我的所有javascript和css整合到单独的php文件中,这些文件使用require_once()来提取文件的内容.javascript文件看起来像这样:

<?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. 我的具体问题是,由于php文件扩展名,Web浏览器会“看到”这是一个动态页面,然后每次加载网站上的页面时都会重新请求内容。 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 compression better than individually gzipped files. 为了清楚起见,我这样做的原因是因为(我认为)它比单独的gzip压缩文件更好地利用了gzip压缩。

Thanks in advance 提前致谢

I wrote a series of posts about this issue specifically. 我专门写了一系列关于这个问题的帖子。 See Supercharging Javascript in PHP and Supercharging CSS in PHP . 请参阅使用PHP增强Javascript和使用PHP 增强CSS These cover: 这些包括:

  • Combining files; 组合文件;
  • Gzipping best practices; Gzipping最佳实践;
  • Caching best practices; 缓存最佳实践; and
  • Versioning output. 版本控制输出。

Your premise is incorrect. 你的前提是不正确的。 Browsers don't "see" the PHP file extension and decide not to cache things. 浏览器不会“看到”PHP文件扩展名并决定不缓存内容。 See http://www.enhanceie.com/redir/?id=httpperf for information on how browsers really work. 有关浏览器如何工作的信息,请访问http://www.enhanceie.com/redir/?id=httpperf

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. 您应该在响应上设置ETAG,然后只需检查If-None-Match请求标头,如果内容不变则返回304。

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. Alister Bulman刚刚提到了一个针对这个问题的整洁的库解决方案,但将其作为评论。 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. Minify - code.google.com/p/minify - 是一个专门用于执行此处所需操作的库 - 连接文件并发送相应的标题,同时修剪内容,并且很可能将它们压缩,同时将结果缓存在磁盘上。 – Alister Bulman Jan 10 '10 at 10:44 - Alister Bulman 2010年1月10日10:44

You can enable auto-gzipping of files using apache mod_deflate. 您可以使用apache mod_deflate启用自动gzipping文件。

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. 您还可以使用apache mod_rewrite将html中的这些文件称为js文件,并将请求重定向到php文件,从而避免服务器缓存问题。

Something like this: 像这样的东西:

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

Put this code in a .htaccess file in your directory. 将此代码放在目录中的.htaccess文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM