admin管理员组

文章数量:1277395

I'm looking to force refreshes of JS/CSS dependencies.

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?

I'm looking to force refreshes of JS/CSS dependencies.

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?

Share Improve this question asked Feb 21, 2011 at 3:29 TimFooleryTimFoolery 1,9251 gold badge19 silver badges29 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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.

No, it controls only current document. If you dont want ugly URIs with random query-strings, its the time to configure your server. Assuming 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:

  1. AllowOverride FileInfo is in effect
  2. Either mod_expires or mod_headers is enabled

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. Described setup is rather mon 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)

The above answer works, though I'd probably rather use a ?version=1at the end, so that it will cache when there are no changes. Also setting the webservers cache-policies is effective.

This is a good article on explaining caching for webpages: http://www.mnot/cache_docs/

本文标签: javascriptRecommended cache control methodStack Overflow