Derek Adair
Derek Adair

Reputation: 21935

Avoiding browser cache for updates

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

Upvotes: 0

Views: 263

Answers (2)

9000
9000

Reputation: 40904

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.

Upvotes: 1

methodin
methodin

Reputation: 6710

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).

Upvotes: 1

Related Questions