July 12 2010

Versioning CSS and Javascript Files

I was reading a hints article regarding MVC and I came across a novel way of ensuring that you never have to ask a client to “Try clearing your cache”.

All you have to do is:

  1. Implement auto version numbering on your application
  2. Append the version number to the url for the .css or .js resource

So your resources will be cached until your version rebuilds, when the resource url will change!

This is actually getting into my next blog post, but figure out a way to version your JavaScript and CSS.  This really isn’t MVC specific, you should do this in almost any project.  The key reason is to help you with a browser’s cache.  You know you have a problem when the first thing someone tells you, when asking for help, is that they cleared their cache already.   My thought on this is your web app’s dll should have a version number, set the project to auto version, and then pop that onto the end of the css/javascript file call.  So it might look like this:  ”http://myapp/…/file.css?version=1.0.0.256″.   In my sample code, when in development I stick a timestamp on the file in the same way.

Comments (View)
blog comments powered by Disqus

Please...

Leave a comment if this has helped or offended you.

StackOverflow Id