我的网站首页是静态HTML,但其内容可能会不时变化。我希望所有浏览器在上次加载后更改内容。同时,缓存非常适合图像,样式表,javascript和其他资源。
Cache-Control
标题是正确的选项吗?这个Cache-Control
标题是否会执行tricK?
Cache-Control: max-age=3600, must-revalidate
如果没有,哪个?
答案 0 :(得分:2)
Cache-Control: no-cache
将会很有效。
但是如果您需要缓存页面(特定部分除外),请尝试使用此替代方法:
Cache-Control: public, max-age=XYZ
缓存整个页面,其中XYZ是以秒为单位的时间。示例代码:
var loadSection = function(section) {
$.ajax({
url: '/section/' + section + '.html',
cache: false
}).done(function(html){
$('#main-content').html(html);
w.pageCache[section] = html;
});
}
};
有关实时实施,请参阅http://jagteshchadha.com
答案 1 :(得分:1)
为什么不使用.htaccess?
使用所有Expires Header,您希望按类型控制所有exipres文件:
<IfModule mod_expires.c>
ExpiresActive on
# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault "access plus 1 month"
# cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5)
ExpiresByType text/cache-manifest "access plus 0 seconds"
# your document html
ExpiresByType text/html "access plus 0 seconds"
# data
ExpiresByType text/xml "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType application/json "access plus 0 seconds"
# rss feed
ExpiresByType application/rss+xml "access plus 1 hour"
# favicon (cannot be renamed)
ExpiresByType image/x-icon "access plus 1 week"
# media: images, video, audio
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# htc files (css3pie)
ExpiresByType text/x-component "access plus 1 month"
# webfonts
ExpiresByType font/truetype "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType font/woff "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
# css and javascript
ExpiresByType text/css "access plus 2 months"
ExpiresByType application/javascript "access plus 2 months"
ExpiresByType text/javascript "access plus 2 months"
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</IfModule>
答案 2 :(得分:0)
对于您的css和javascript脚本,您可以使用以下版本:
<link rel="stylesheet" href="style.css?v=1.0.0" type="text/css" />
并在下次更新时更改版本。如果你真的需要它,你可以使用.js脚本和图像源。