Bust that cache!

<shamelessplug>I don’t know if I have shared this, but I work for Caesars Entertainment doing web development. Go make a hotel reservation and you’ll be using everything that my work has been for the last 2 years.</shamelessplug>

Anyway, as web development goes, the site is very javascript heavy, and we have some core functionality existing in a javascript file linked via a

<script type="text/javascript" language="javascript" src="blahblah.js"></script>

tag. Regardless of the specifics, certain browsers end up being a pain when it comes to caching. For instance, we make a change to blahblah.js, but the browser has an old version of it cached for speedy loading and it ends up not picking up the change. It takes some time for the browser to get around to updating, and every browser is different, so I did some searching.

I found a site (http://davidwalsh.name/prevent-cache) which basically says to do this:

<script type="text/javascript" language="javascript" src="blahblah.js?(insert some dynamic number based on current time here)"></script>

This effectively makes the browser see a new reference for the javascript file every time it loads the page… not completely ideal for all situations. Namely, browser cache can be a very helpful thing to improve site performance… we like browser cache, but we need to make it work for us and not against us.

The simple solution is:

<script type="text/javascript" language="javascript" src="blahblah.js?(insert static version # here)"></script>

The version # is actually loaded via a web.config file, but that’s neither here nor there… what is important is that every time we want the browser to reload the cached js file, we simply increment that version # and the browser doesn’t have that src reference cached, so it has to download it.

Cache busted!

  1. No comments yet.

  1. No trackbacks yet.