[英]Cache dynamic JavaScript generated by PHP
I use JShrink with a custom function to combine 8 uncompressed JavaScript files to a single compressed (minified) one, like this: 我将JShrink与自定义函数结合使用,将8个未压缩的JavaScript文件组合为一个压缩(最小化)的文件,如下所示:
<?php
// Filename: js.php
header('Content-type: text/javascript');
require_once '../JShrink.php';
function concatenateFiles($files)
{
$buffer = '';
foreach($files as $file) {
$buffer .= file_get_contents(__DIR__ . '/' . $file);
}
return $buffer;
}
$js = concatenateFiles([
'core.min.js',
'promise.js',
'welcome.js',
'imagesloaded.js',
'cropper.js',
'translate.js',
'custom.js',
'masonry.js',
]);
$output = \JShrink\Minifier::minify($js);
echo $output;
Then I call this php file in my index page footer: 然后,我在索引页脚中将此php文件称为:
<script type="text/javascript" src="<? echo $url ?>/js/js.php"></script>
It is not being cached. 它没有被缓存。
I modify my JS codes daily and I don't like to keep combining them manually, but also I need a way to get the echoed JS code cached, only that code and not all php files on the server. 我每天修改我的JS代码,但我不想一直手动进行合并,但是我还需要一种方法来缓存已回显的JS代码,仅缓存该代码,而不缓存服务器上的所有php文件。
How can I do this, and how would the cache purge process be? 我该怎么办?缓存清除过程将如何进行?
Thanks in advance. 提前致谢。
In theory, you need to use a header("...") with proper expiration. 从理论上讲,您需要使用具有适当到期时间的header(“ ...”)。 In practice, that's not working properly. 实际上,这不能正常工作。 You can spend your life googling for proper examples of "Cache-Control" and "Expires:" and none of what you find will work. 您可以花费大量时间搜索“ Cache-Control”和“ Expires:”的正确示例,而发现的任何示例均无效。 So I suggest you to read this: 因此,我建议您阅读以下内容:
https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching
ETags are the modern solution to tell the browser when your resource has changed - or not. ETags是一种现代的解决方案,可以告诉浏览器您的资源何时已更改-是否已更改。
如果高速缓存文件不存在,或者稍后有任何文件修改时间戳记,则将其缓存,呈现,然后将其保护到高速缓存中,然后回显高速缓存或呈现的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.