Busted Mug

A blog that documents solutions to the most frustrating problems that occur during development in technologies such as Java, XML, AJAX, SQL, CSS and others that make me want to throw my coffee mug against the cube wall.

Friday, March 02, 2007

The External JavaScript File (.JS) Caching Dilemma

The apps I work on always seem to run into issues because users have cached old versions of our external javascript files. I'll add a call to a new function defined in a .js to a page and the user's browser will puke because it is using the cached version. No good.

While looking for a way to prevent my .js files from being cached I ran across this: DivineNTD.com Journal: Preventing JavaScript File Caching

That doesn't really solve the problem since you're just changing source, but I'll take it. It will keep your .js file at the newest version. Dirty, but effective.

To push the solution forward a little, what I've decided to do is to add a member to a static class that is loaded in the constructor. I then create one of these when the app is started. Since I am working in Java and it is compiled, the app will be restarted when new code is introduced. This means that whenever the app is restarted, the date changes. I append THAT, after a ? to my .js addresses and voila: .js files are flushed out of the cache only when they are changed. ;)


Post a Comment

<< Home