Friday, February 11, 2011

ASP.NET Specify which files should be cached

I am developing a website in ASP.NET and I am using various javascript frameworks/libraries. The different files belonging to these frameworks/libraries rarely changes thus there is no reason to refresh those files once they have been sent to the client browser (atleast not everytime a page is served).

I see that the HttpContext object can be used somehow, that I can set the content expiration on the files/folders on the IIS, or maybe setup somekind of caching in the web.config file.

1. What is best practice/what approach should I take?

If the content expiration on the IIS works, then this is just great (and easy). But during development, I use the ASP.NET Development Server, which does not interact with the external IIS - thus no performance gain achieved during development :(

2. How can I use cache here (depending on question 1)?

I would like to modify this question to be used as a "this-is-how-its-done" thread for future reference for me and others.

  • The cache settings in the HttpContext only control how the aspx page is cached, not the caching of files that the browser include (javascript, css, images...).

    The browser caches the javascript files by default, and that is pretty much the same regardless if you are running the site locally or live. Normally you have to make a Ctrl+F5 or purge the cache if you make changes in a script to get the browser to load the new version.

    So, you don't have to do anything at all, the browswer already caches the files.

    Chau : I actually suspected that what you're saying is true. But when I look at the NET tab in FireBug, it claims that it is using above 10s to recieve a +1MB javascript file - and this is locally. The response header for this file says cache-control: private, and the request header says cache-control: max_age=0.
    From Guffa
  • I think if you are looking for the caching of the files then that has to be done on the IIS end. You can try YSloy (by Yahoo) extension with FireBug. This is very good and should be used as best practice. Hope that will be helpfull :)

    Chau : YSlow is now included in my developer tools. Regarding the javascript files and caching, YSlow complains about none of the having a "far-future expiration date" - which corresponds with the lack of caching I am experiencing.
    From Anil Namde

0 comments:

Post a Comment