简体   繁体   English

推荐的缓存控制方法?

[英]Recommended cache control method?

I'm looking to force refreshes of JS/CSS dependencies. 我想强制刷新JS / CSS依赖项。

Will <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"> work for that, or will that only force a refresh of the content within the page itself? <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">为此工作,还是只会强制刷新页面内的内容?

You could use a server-side language to append a timestamp to each file being pulled in: 您可以使用服务器端语言为每个被拉入的文件附加时间戳:

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

I've found that meta cache tags don't work consistently cross-browser, so this is my go-to if I need to force-reload something on page refresh. 我发现元缓存标记不能跨浏览器一致地工作,所以如果我需要在页面刷新时强制重新加载某些东西,这是我的首选。

The above answer works, though I'd probably rather use a ?version=1 at the end, so that it will cache when there are no changes. 上面的答案是有效的,虽然我最后可能会使用?version=1 ,因此它会在没有变化时缓存。 Also setting the webservers cache-policies is effective. 另外,设置webservers缓存策略也很有效。

This is a good article on explaining caching for webpages: http://www.mnot.net/cache_docs/ 这是一篇关于解释网页缓存的好文章: http//www.mnot.net/cache_docs/

No, it controls only current document. 不,它只控制当前文档。 If you dont want ugly URIs with random query-strings, its the time to configure your server. 如果您不希望使用随机查询字符串的丑陋URI,则需要时间来配置您的服务器。 Assuming Apache: 假设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>

These directive groups will work in per-directory configs ( .htaccess files) too (in case of shared environment hosting), given following requirements met: 这些指令组也可以在每个目录配置( .htaccess文件)中工作(在共享环境托管的情况下),满足以下要求:

  1. AllowOverride FileInfo is in effect AllowOverride FileInfo生效
  2. Either mod_expires or mod_headers is enabled mod_expiresmod_headers启用

If both are enabled - note that groups are overlapping on max-age , you will want to remove it from Header and use finer control via ExpiresXXXX . 如果两者都启用 - 请注意组在max-age上重叠,您将需要从Header删除它并通过ExpiresXXXX使用更精细的控制。 Described setup is rather common for the shared hosting environment, so ask server admin or just try yourself (will return 500 Internal Server Error if corresponding module is not enabled or have no effect if .htaccess processing is not enabled) 对于共享托管环境,所描述的设置是相当常见的,因此请询问服务器管理员或仅尝试自己(如果未启用相应模块将返回500 Internal Server Error ,或者如果未启用.htaccess处理则不会生效)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM