0

After reading this question I was wondering if I could prevent browser cache by appending my application version to the uri at the top of my index file.... like so:

$revision = getRevision();
if($_GET['v'] != $revision){
  header('Location: index.php?v=' . $revision);
}

Will this work?

The end goal is to reset the entire cache - JavaScript, CSS, images - when i push an update in, perferably, a nice little one liner

Community
  • 1
  • 1
Derek Adair
  • 21,846
  • 31
  • 97
  • 134

2 Answers2

1

If the page itself is cached, yes. You will also have to do it to any images/css/js or external files that are available via a URL on your site (assuming you are passing cache headers so they are, in fact, cached) and are contained in that page because the browser caches all available URLs and they aren't grouped together by what page called them - they are all independent. So if index.php contains an IMG then the IMG will still be called from cache even if you change index.php?v=1234. You'd have to add ?v=1234 on the image as well in order to re-fetch both the page and the image.

The version system is typically something you append to all cacheable URLs that can be modified (like css and js) but want to be invalidated as soon as it is updated. You typically append ?VERSION to the URL or ?version=VERSION to all URLs in whatever fashion makes sense (making sure not to break URL parameters).

methodin
  • 6,717
  • 1
  • 25
  • 27
  • Is there a way to append it to all URLS in single easy fashion? – Derek Adair Dec 18 '10 at 19:52
  • In short yes and no. If you use a common function to generate all your URLs then you would obviously have an easy time making this modification. Similarly if you use a template system you can just add something like {{version}} to the end of all your URLs and handle the value from your back-end code. – methodin Dec 18 '10 at 20:44
1

Yes you can; appending a random ignored parameter is a classic way to defeat caching. You can also play with headers: Cache-control: no-cache and Expires: set really way back in time.

9000
  • 39,899
  • 9
  • 66
  • 104
  • Just an fyi for anyone who was thinking of putting their faith in cache-control headers: They may not work all of the time. Cache-control headers are just a recommendation to the browser. Also, a proxy can ultimately decide what to send to the browser after the browser has made a request. – BumbleB2na Nov 01 '11 at 15:49