php - css/js 上的客户端缓存

标签 php html css .htaccess caching

首先我要说的是,我花时间阅读了这种情况下的所有主题,但没有成功,最近我在我的网站上遇到了客户端/浏览器缓存的问题,它发生时没有任何变化。服务器端缓存很好,但客户端工作很糟糕,我应该每次都按 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" />

如您所见,我尝试了所有可能的方法来处理烦人的缓存但没有成功。我知道我可以在 cssjs 的末尾添加 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:

source

那么,在正确的方向上去除cssjs 重缓存的最佳方法是什么?

最佳答案

<?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/

相关文章:

php - 为什么我不应该将我的 PHP 文件放在 public_html 文件夹中?

javascript - 使用 URL 中的参数填充多个输入字段...(半工作)

javascript - 灯箱内的 AMP 轮播无法打开录制/单击的图像

html - 将文本放入文本框

jQuery 根据所选值更改选择的文本

jquery - 后台位置上的调用中心值失败

php - WordPress图片上传时间错误: Post-processing of the image failed

PHP session_start() 与 error_reporting() 冲突?

php - 如何添加我想要的链接以使用 PHP Universal FeedParser 阅读

css - 某些像素下的 HTML CSS 响应式 div