views:

51

answers:

2

I host about 10 websites for clients. Every so often a client will ask for an update to their website. It may be a simple image change, new PDF or a simple text change. I make the change and then send them a link to the web page with the update. About an hour later I will get an email back from the client telling me they still see the old page. I will then explaining to them how to empty their browsers cache. What I'm trying to figure out is if there is a way I can tell their browser that I made an update to the website and that it should reload the page and update the cache. I thought about trying a meta tag but I read that they are not very reliable. Also I would still like the page to cache I just want to be able to clear it when I make an update. Is this possible? I'm an advanced front end web developer (HTML, CSS, Javascript) and know some PHP. Cache is just one of those things I don't really understand that well.

A: 

Depending how front-end you are, you may not have access or the interest in modifying headers.

One very simple solution is to rename resources when you change them. Obviously going from "image.jpg" to "image2.jpg" each time it changes would be a pain to maintain, doubly so depending on your source control, but going from "image.jpg?v=1" to "image.jpg?v=2" would allow you to keep your file names the same while forcing browsers and caching intermediaries to reload the resource.

A common practice is to use a hash of the file as the parameter, so you don't have to remember to increment it each time the file changes.

As for your HTML file, the easy solution is not to cache it at all. I'm assuming it's not going to be a big issue for you to regenerate it each time its viewed, so adding the headers shouldn't be a problem.

Add a Cache-Control header and Expires header. Together they should keep all browsers and intermediaries from caching the html file, forcing a reload every time:

<?php
  header("Cache-Control: no-cache,no-store,must-revalidate");
  header("Expires: 0");
?>

Just make sure your header() calls are before you output any page data, as the HTTP header needs to be sent before any of the page is sent.

Parker
I'm not afraid to create an .htaccess file or use some PHP. I like the method you described for files but what about the HTML page it's self? I can't change the file name since it would hurt SEO.
Jared Christensen
A: 

Not sure about a server side command to empty user cache... but hopefully the client is smart enough to have their internet settings to check for a new version on every visit to the page.

08Hawkeye