Updating cache when page has new version?

un4gvnar

New Member
Messages
3
Reaction score
0
Points
1
I've searched around a bit and nothing seems to quite hit the nail on the head with my question;
How can you make sure the visitor is viewing the latest version of your website? Granted they would be return visitors and already have a previous version of the website occupying their cache.

I've noticed it as I've been updating the live version, I've had to clear the cache or ctrl+shift+r the page to reload it manually, but somebody viewing the website wouldn't know to do this and accept that the content being displayed is the latest, when there could be a bit missing!

The most simple fix imo is just putting a message asking the visitor to clear their cache every now and then if nothing has updated for a while, but how unprofessional is that?!

Any help or clarification with this would be amazing, cheers people.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
You can use the .htaccess file to advise the browser that it should not cache pages (or to keep them for a limited amount of time, say a couple of days) or other files (css, javascript, images)

Better to set a short expiration date/time than use no-cache, for performance issues.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
There are only two hard problems in computer science: cache invalidation, and naming things.

~~ Phil Karlton, Netscape

Phil was right, but oddly enough one of those hard problems can largely solve the other, provided that you pay attention to a few other details. As @descalzo noted, the first step is to ensure that you are not over-caching things that you want cached. Most of your assembled HTML pages (and data sources, like JSON or XML responses) should have relatively short lives if they are cached at all. That decision would be based on things like server resources and traffic, as well as your usual update pattern. A site that is mostly static and infrequently updated can usually afford a bit of a lag pushing updates to the users; a lively forum not so much. You can get very fine-grained control of this using HTTP headers (via .htaccess and scripting languages) and META tags.

The other half of the equation, though, is stuff you want to get stuck in cache until you say different. That includes things like JavaScript libraries, CSS stylesheets and the images you use for your site's decoration and user interface. Use versioned filenames for these. It doesn't matter that /css/common.css will be cached until the heat death of the universe if the HTML is telling the browser to use /css/common_2_0.css to display the page. And that new style sheet can be safely cached until your HTML tells the browser to use a different one. As long as your library resources (CSS, JavaScript, branding images) are maintained/stored in separate directories, you can get pretty radical with the .htaccess cache values provided that you use filename changes to invalidate the cache. You can use version numbers (as in the example) or publication dates as part of the filename; either way, the HTML will tell the browser which version to use, and it will only use the cached version if the filenames (URIs) match.
 
Top