大网数据广告活动
网站运营与故障问答

Force visitors to get newest versions of resources (bypassing browser cache)

· 52CE 我爱测

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 thei…

问题原文

原帖链接 · 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:

  1. Original version: <link rel="stylesheet" href="pro-webmasters.css">
  2. Previous version: <link rel="stylesheet" href="pro-webmasters.css?v=0.1">
  3. 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; ?>">

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。