我有一个可以预览的幻灯片(自定义 html/js),并确保每次启动幻灯片预览时都能从数据库中获取最新版本,我的 html 文件中有这些缓存语句
<!DOCTYPE html>
<!-- preview:[true]-->
<HEAD>
<title>screen</title>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
但是有时我仍然需要清空浏览器缓存才能获得最新的更新。
还有什么我可以在我的 html 文件的头部放置以防止浏览器为我缓存它吗?Chrome 是我主要使用的,我有时会在其中体验到这种效果,但我想要一个适用于所有浏览器的通用答案。
---------------- 更新 ------------------- 所以我包括了一些缓存控制头(感谢 arkascha),但我仍然有点可疑=)
这是我在回复中添加的内容(php/sym2)
$response->expire();
$now = new \DateTime("now");
$response->setCache(array(
'last_modified' => $now,
'max_age' => 0,
's_maxage' => 0,
'private' => true,
));
这给了我以下响应标头
Cache-Control:max-age=0, private, s-maxage=0
Connection:Keep-Alive
Content-Type:text/html; charset=UTF-8
Date:Wed, 31 May 2017 08:41:19 GMT
Keep-Alive:timeout=5, max=100
Last-Modified:Wed, 31 May 2017 08:41:20 GMT
Server:Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8zh DAV/2 PHP/5.5.3
Transfer-Encoding:chunked
X-Powered-By:PHP/5.5.3
...并且请求标头显示为:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Cookie:main=1111111; PHPSESSID=70c2e1a6f81a9d3dad59ed908f25b585
Host:localhost
If-Modified-Since:Wed, 31 May 2017 08:40:18 GMT
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
但是在我的网络选项卡(chrome)上,对于不同的资源,我仍然在“大小”列中看到很多“来自内存缓存”和“来自磁盘缓存”(顺便说一句,这两者有什么区别?)。
http header 是否只影响当前页面而不影响 .json 文件等关联资源?