Cross browser client caching

by Mads Kristensen 26. August 2006 09:37

Internet Explorer and the Gecko based browsers (Firefox, Netscape and Mozilla) have different ways of managing their internal cache. The speed of the browser cache is much faster that a server caching, so be sure to do your client-side caching cross browser friendly.

The Gecko based browsers uses the ETag header and Internet Explorer uses the Last-Modified header, so you have to set both of them to make it work. Here’s a simple method that does just that.

  private void SetCachingHeaders(DateTime lastModified)

  {

    Response.Cache.SetLastModified(lastModified);

    Response.Cache.SetETag(lastModified.ToString());

  }

If you serve an image or some other file from ASP.NET, you can make the browser cache dependant of the file you are serving. That means that whenever the file changes, so will the client- and server cache and it all happens automatically.

  private void SetCachingHeaders(string fileName)

  {

    context.Response.AddFileDependency(fileName);

    context.Response.Cache.SetETagFromFileDependencies();

    context.Response.Cache.SetLastModifiedFromFileDependencies();

  }

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags:

ASP.NET

Comments

12/20/2007 5:09:06 AM #

pingback

Pingback from blog.olivier-duval.info

Zork[Yy]'s log : Développement Web & flux RSS : optimisations

blog.olivier-duval.info |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008