如何防止缓存网站内容

How to prevent caching of website content?

本文关键字:网站 缓存 何防止      更新时间:2023-09-26

我有一个动态网页,其中一小部分内容一直在变化。

每个请求的不同内容由以下部分组成:Javascript和HTML

为了使网站正确显示,javascript和HTML需要从服务器中100%新鲜。我检查了不时发生的页面错误是旧的/以前加载的javascript或HTML的结果,而不是来自服务器的新数据。

我在 .htacces 中绑定了以下设置:

Header Set Cache-Control "max-age=0, no-store"

上述设置有效,但问题是每次都需要重新加载图像,这是不必要的,并且对站点性能不利。

我也在.htacces中尝试了以下设置:

### turn on the Expires engine
ExpiresActive On
### expires after a month in the client's cache
ExpiresByType image/gif A36000
ExpiresByType image/png A36000
ExpiresByType image/jpg A36000
ExpiresByType image/x-icon A36000
ExpiresByType application/pdf A36000
### ExpiresByType application/x-javascript A36000
### ExpiresByType text/plain A36000

但是,上述内容似乎不起作用,因为我检查的内容并不总是新鲜的,导致页面错误。

我的问题:

如何正确配置服务器,以便始终(通过生成的 php 脚本)使用新的 HTML 和 javascript?

text/html 和 css 文件的过期时间与图像不同怎么办?

ExpiresByType text/html "access plus 5 seconds"
ExpiresByType text/css "access plus 5 seconds"
ExpiresByType image/jpg "access plus 5 minutes"
etc
当我

的应用程序具有相同的更新要求时,我使用了以下 (PHP):

// disable cache (ajax useful)
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");