1

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?

  • I would go for caching your files for a shorter period, so the cache will be removed automatically, and new content will be able to show up – Jakub Kliský Apr 10 '17 at 13:00
3

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; ?>">
  • I've created a .htaccess file on my public_html in cpanel, and copy pasted the Apache mod_expires.c code, is that fine? Should it now stop caching my sites? Thanks – Panic Apr 5 '17 at 8:26
  • Assuming your enabled the .htaccess then yes, it'll set the cache at approx 1 minute. If your using Cloudflare or another transparent proxy such as vanish then you need to purge the cache on those also. – Simon Hayter Apr 5 '17 at 14:11
  • Stupid question, but how do I enable it in cpanel? There was no file called .htaccess I just created one and put this code in it, oh yeah I changed 60secs to 2 hours – Panic Apr 6 '17 at 8:14
  • It is by default enabled in cPanel unless it has been disabled in WHM by you or your web host which is very rare for them to do so. – Simon Hayter Apr 6 '17 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.