views:

274

answers:

1

I have blobcaching enabled on a publishing site.

For authenticated users the max-age property in the Cache-Control HTTP header is set properly according the setting in the blobCache element in web.config, for anonymous users it is set to 0. This causes a lot of 304 requests by the browser trying to see if an image has changed and affects performance negatively. This is a problem only for files in the "/Style Library".

There are a few mentions of this problem in blogs but no solution found so far. Is there any way I can fix this or force setting of this header through some other means? I've tried implementing an HTTP handler to do this but it still comes up as 0.

+2  A: 

How would it be if you set caching for the whole site and then on the page(s) you don't want anonymous users to get cached content you add something along the lines of this in:

protected override void OnInit(EventArgs e)
{
    if (user.IsAnonymous())
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        Response.Cache.SetExpires(DateTime.MinValue);
    }
    base.OnInit(e);
}

Which will produce this:

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Length: 15965
Content-Type: text/html; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Date: Mon, 03 Aug 2009 01:07:26 GMT

This should make the webpage not be cached but whatever caching is applied to the images, css, js etc. should remain. If the images aren't getting caching headers applied to them then it sounds like you'll have to write a custom http handler to intercept the request for those images and then apply the correct caching headers along with setting on a per page basis what pages you don't want cached.

Brian Surowiec
I would like to set caching for the whole site but that's not working for files in the /Style Library!I have 'solved' this by moving the files in there to a location within the _layouts path. At least there I can control caching through IIS.
ArjanP