views:

80

answers:

4

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.

A: 

Yes, the bit about using query string with version is correct and it works. However, I am unsure if changing setting in web.config will actually apply to static contents. The caching for static contents is managed at IIS by IIS admin base on developer recommendation at where I work.

At the moment I manage the version number in one place and can be controlled with the admin UI of the application. The downside is that once version number is changed, version number of all static files are changed for the whole application.

Alternatively, each static file is named with the version as part of the file name, so once contents are deployed there is nothing need to be changed. However, it also mean that code which reference the static contents need to refer to them using the full file name as well and need to be changed when static content is changed.

Hope that help.

airmanx86
A: 

Keep in mind that IIS will only invoke the .NET runtime for files that are mapped to the asp.net ISAPI. So static content like .html, .jpg, etc. will not be affected by any web.config setting. You could map them, but you'll lose performance that way. Probably not the best route.

For script code, I usually use a system like airmanx said above. A global setting in a .config that is appended to the URL, and updated when there is a new version. You could automate that by grabbing the build # of the app and pass that in.

For static content, you could consider serving all of the content from a different domain (e.g, content.domain.com) and managing the headers in IIS for caching, content expiration, etc. Its a pretty easy solution that doesn't require too much overhead, though you'll have to get your ops people involved if you need to expire content. It's not a fully automated solution, but I've found it works pretty well on most large sites I've worked on.

ericvg
+1  A: 

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

CriGoT
A: 

I think you might find this article interesting:

Creating Static Content Website in IIS 7

Lucifer