Tag Archives: Javascript

Static resources versioning in web applications

I recently had trouble with a Javascript-heavy application which was deployed to the end users quite frequently in its initial stages. The problem was that users were seeing bugs which had been fixed with the latest release and that was because their browsers were caching the static resources of the web application for too long. A forced refresh (Ctrl+F5 or Ctrl+R) solved the problem but I wanted a better, automatic way.

The solution was to add a version to the path of each static resource and then use url rewriting to get the real file. In practice in my HTML I refer to resources like this:

<link rel='stylesheet' type='text/css' href='1.0.6/css/styles.css' />
<script src='1.0.6/js/script.js'></script>

In my case the 1.0.6 is the pom.version which I get using Maven resource filtering. I then use the wonderful URL Rewrite Filter (but I could also have used Apache’s mod_rewrite ) to rewrite URLs matching

^/[0-9]+[a-zA-Z0-9\.\-]+/(js|css|img)/(.*)$

to

/$1/$2

I think I’ll go back and apply this fix to all my apps 🙂

Share