推荐的缓存控制方法?

时间:2011-02-21 03:29:46

标签: javascript css dependencies

我希望强制刷新JS / CSS依赖项。

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">会为此工作,还是只会强制刷新页面内的内容?

3 个答案:

答案 0 :(得分:4)

您可以使用服务器端语言为每个被拉入的文件附加时间戳:

<?php $timestamp = time(); ?>
<link href="shell.css?timestamp=<?=$timestamp?>" rel="stylesheet" type="text/css" />

我发现元缓存标记不能跨浏览器一致地工作,所以如果我需要在页面刷新时强制重新加载某些内容,这是我的首选。

答案 1 :(得分:3)

上面的答案有效,但我最后可能宁愿使用?version=1,以便在没有变化时进行缓存。另外,设置webservers缓存策略也是有效的。

这是一篇关于解释网页缓存的好文章:http://www.mnot.net/cache_docs/

答案 2 :(得分:3)

不,它只控制当前文档。如果您不希望使用随机查询字符串的丑陋URI,则需要时间来配置您的服务器。假设Apache:

# mod_expires directives: 
# enable expires/max-age headers and set default to 0 seconds from last access time
ExpiresActive On
ExpiresDefault A0
# configure ExpiresByType on your specific types, eg ExpiresByType text/css A0



# mod_headers directives:
# send variety of no-cache directives, should cover any quirky clients and gateways
Header set Cache-Control "max-age=0, private, no-cache, no-store, must-revalidate, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
# enclose this in <Files> directive for specific file eg <Files *.js>

这些指令组也可以在每个目录配置(.htaccess文件)中工作(在共享环境托管的情况下),满足以下要求:

  1. AllowOverride FileInfo生效
  2. 启用mod_expiresmod_headers
  3. 如果两者都已启用 - 请注意,max-age上的群组重叠,您需要将其从Header中删除,并通过ExpiresXXXX使用更精细的控制。 对于共享托管环境,所描述的设置是相当常见的,因此请询问服务器管理员或仅尝试自己(如果未启用相应模块将返回500 Internal Server Error或如果未启用.htaccess处理则无效)

相关问题