首先我要说的是,我花时间阅读了这种情况下的所有主题,但没有成功,最近我在我的网站上遇到了客户端/浏览器缓存的问题,它发生时没有任何变化。服务器端缓存很好,但客户端工作很糟糕,我应该每次都按 CTRL+F5,我不想要这个,因为它对用户不利,我知道我可以在打开 devTool 时禁用缓存,但我说的是用户而不仅仅是我自己。这也发生在桌面和移动设备上。在移动设备中,我应该去设置/隐私/清除缓存。
这是我的网站代码与缓存相关的:
htaccess:mod_expires
ExpiresByType text/css "access plus 1 month"
我从 gzip 中删除了 css
,但没有成功。也将 1 个月更改为 1 秒。
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
PHP header :
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Pragma: no-cache");
header("Vary: Accept-Encoding");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
HTML 元:
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
如您所见,我尝试了所有可能的方法来处理烦人的缓存但没有成功。我知道我可以在 css
或 js
的末尾添加 version
但都知道这是清除缓存的坏习惯:
Remove query strings from static resources
Resources with a "?" in the URL are not cached by some proxy caching servers. Remove the query string and encode the parameters into the URL for the following resources:
那么,在正确的方向上去除css
和js
重缓存的最佳方法是什么?
最佳答案
<?php $filename "path/to/file.css"; ?>
<script src="<?php echo $filename; ?>?ver=<?php echo filemtime($filename); ?>">
这将缓存在浏览器中,但当您对文件进行更改时,文件时间将更新,客户端将获得文件的新副本。
关于php - css/js 上的客户端缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49282489/