We are using IIS7 and dotnet 3.5 in our company to build different web applications used by our internal as well as external customers. It has been recommended that we start using static content caching to cache image and html files.
My understanding is that in web.config file we can set the cachecontrolmaxage to specify the number of days which the files are required to be cached.
Further it has been recommended that we call these static pages by passing an argument say myjsfile.js?verfile=1234 which can be changed whenever a new version of the file is put in production.
I hope that I go the above right? Now what am looking for is a better way to achieve the second part, instead of having to pass a new version number everytime a new file is created can we have this deployed in some other way so that any new changes to the static file is there in production environment. What I guess am looking for is a simple way to promote multiple images and static files in production without worrying about changing the version number.
Since IIS7 all IIS configuration can be applied through the web.config file, more specifically the system.webServer section. In this case you should check at the Caching Section and create a custom profile (make sure to set the varyByQueryString attribute to true to make the “?version=xxx” work).
Going into the deployment question it’s necessary that the url changes since the client will not check for a new version of the file while the cache is valid (and you say you may want to set days as the cache duration). One common pattern is to autogenerate the url based on the modification date for instance if your original line is:
<script src='functions.js' />You may change it to:
<script src='<%=GetFilenameWithModificationDate("functions.js")%>' />The function should get the File modification DateTime and append it to the file, so if file was last modified con 2010-01-01 at 10:12:34 it should generate something like this:
<script src='functions.js?version=20100101101234' />This way whenever you modify the file a new querystring will be included and cache will be updated.
Since you are working in caching your static files files I’m assuming that performance is a consideration so you should consider the penalty of checking the modification date for each file and you may want to use caching in the helper function, control or whichever mechanism you decide to use.
HTH