HTTP caching confusion

Posted by Keith on Stack Overflow See other posts from Stack Overflow or by Keith
Published on 2010-06-07T14:42:02Z Indexed on 2010/06/07 14:52 UTC
Read the original article Hit count: 306

Filed under:
|
|
|

I'm not sure whether this is a server issue, or whether I'm failing to understand how HTTP caching really works.

I have an ASP MVC application running on IIS7. There's a lot of static content as part of the site including lots of CSS, Javascript and image files.

For these files I want the browser to cache them for at least a day - our .css, .js, .gif and .png files rarely change.

My web.config goes like this:

<system.webServer>
    <staticContent>
        <clientCache cacheControlMode="UseMaxAge" 
                     cacheControlMaxAge="1.00:00:00" />
    </staticContent>
</system.webServer>

The problem I'm getting is that the browser (tested Chrome, IE8 and FX) doesn't seem to be caching the files as I'd expect. I've got the default settings (check for newer pages automatically in IE).

On first visit the content downloads as expected

HTTP/1.1 200 OK
Cache-Control: max-age=86400
Content-Type: image/gif
Last-Modified: Fri, 07 Aug 2009 09:55:15 GMT
Accept-Ranges: bytes
ETag: "3efeb2294517ca1:0"
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Date: Mon, 07 Jun 2010 14:29:16 GMT
Content-Length: 918

<content>

I think that the Cache-Control: max-age=86400 should tell the browser not to request the page again for a day.

Ok, so now the page is reloaded and the browser requests the image again. This time it gets an empty response with these headers:

HTTP/1.1 304 Not Modified
Cache-Control: max-age=86400
Last-Modified: Fri, 07 Aug 2009 09:55:15 GMT
Accept-Ranges: bytes
ETag: "3efeb2294517ca1:0"
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Date: Mon, 07 Jun 2010 14:30:32 GMT

So it looks like the browser has sent the ETag back (as a unique id for the resource), and the server's come back with a 304 Not Modified - telling the browser that it can use the previously downloaded file.

It seems to me that would be correct for many caching situations, but here I don't want the extra round trip. I don't care if the image gets out of date when the file on the server changes.

There are a lot of these files (even with sprite-maps and the like) and many of our clients have very slow networks. Each round trip to ping for that 304 status is taking about a 10th to a 5th of a second. Many also have IE6 which only has 2 HTTP connections at a time. The net result is that our application appears to be very slow for these clients with every page taking an extra couple of seconds to check that the static content hasn't changed.

What response header am I missing that would cause the browser to aggressively cache the files?

How would I set this in a .Net web.config for IIS7?

Am I misunderstanding how HTTP caching works in the first place?

© Stack Overflow or respective owner

Related posts about http

Related posts about iis