浏览器不缓存生成的图像 [英] Browser not caching generated images
问题描述
似乎在某些测试中,Firefox不会缓存动态生成(或从缓存服务器端加载)的图像。
It seems that during some tests, Firefox will not cache images that are dynamically generated (or loaded from the cache serverside).
我尝试使用标头启用缓存:
I tried enabling caching using headers:
$expires = 60*60*24*14;
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
但无济于事。我仍然得到200 OK而不是304 Not Modified。
But to no avail. I still get a "200 OK" instead of "304 Not Modified".
使用Stackoverflow上的搜索寻求答案,我发现有人暗示他们宁愿直接将浏览器重新定位到图像。
Seeking an answer using the search on Stackoverflow, i discovered a hint by someone that they would rather relocate the browser to the image directly.
喜欢:
header("Location: /img/generated/whatever.png");
我不喜欢这种方法,因为它会导致两个请求。
I don't like the approach because it causes two requests.
任何人都可以帮助我,让我知道如何让浏览器缓存这些文件?
Could anyone help me out and let me know how i could just get the browser to cache these files?
请求URL未更改,并且不包含任何种子或时间戳。
The Request URL is not changing and it does not contain any seeds or timestamps.
推荐答案
此处缺少的是您的响应标头中的最后修改标头。 ex Last-Modified:Wed,15 Feb 2012 10:07:48 GMT
。您可以通过在标题中使用此行来解决此问题。
What you are missing here is last modified header in your reponse header. ex Last-Modified:Wed, 15 Feb 2012 10:07:48 GMT
. you can solve this by using this line with your headers.
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
你之所以应该拥有这个,是因为浏览器使用最后一个modidfied标头来比较文件是不是修改后无需重新下载文件。 :
The reason why you should have this because the browser uses last modidfied header to compare if the file is not modified thus no need to re download the file. :
这篇关于浏览器不缓存生成的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!