问题原文
原帖链接 · Zanic L3 · CC BY-SA 3.0
How can I (force) my visitors on my website to clean their cache, and get the new updated content? I find myself sometimes linking progress on some of my projects and sending it to others for feedback, but they have to clean their cache first to see the actual changes.
I was wondering if theres something that I can do from my position to load in the new content instantly instead of loading it from their cache?
已采纳解答
原帖链接 · Simon Hayter · CC BY-SA 3.0
Sounds like your site is using HTTP caching which can be adjusted using some form of Leverage Browser Caching or using version strings on your files.
Below you can 3 methods: Apache mod_expires, Apache FilesMatch and versioning strings using PHP.
Apache mod_expires.c
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 60 seconds"
ExpiresByType text/html "access plus 60 seconds"
ExpiresByType image/x-icon "access plus 60 seconds"
ExpiresByType image/gif "access plus 60 seconds"
ExpiresByType image/jpeg "access plus 60 seconds"
ExpiresByType image/png "access plus 60 seconds"
ExpiresByType text/css "access plus 60 seconds"
ExpiresByType text/javascript "access plus 60 seconds"
ExpiresByType application/x-javascript "access plus 60 seconds"
</IfModule>
Apache FilesMatch
<FilesMatch "\.(?i:gif|jpe?g|png|ico|css|js|swf)$">
<IfModule mod_headers.c>
Header set Cache-Control "max-age=60, public, must-revalidate"
</IfModule>
</FilesMatch>
Versioning
Adding a query string to the end of the file will make the browser believe that the file is not the same and it'll re-download it.
Each time you make a edit to the a resource you need to update the query string, for example:
- Original version:
<link rel="stylesheet" href="pro-webmasters.css"> - Previous version:
<link rel="stylesheet" href="pro-webmasters.css?v=0.1"> - Latest version:
<link rel="stylesheet" href="pro-webmasters.css?v=0.2">
Obviously this becomes time consuming when you have multiple css files, JavaScript files and other resources, this can be made easier by using PHP variables, for example:
<?php $VerNumber= "0.0.2"; ?>
<link rel="stylesheet" href="style.css?v=<?php echo $VerNumber; ?>">
<script src="jquery.js?v=<?php echo $VerNumber; ?>"></script>
<img src="image.jpg?v=<?php echo $VerNumber; ?>">
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
