mike hodnick -> mhodnick <-> kindohm

Mike Hodnick is a geeky consultant guy living in Chaska, MN. He enjoys writing code, writing music, and doing fun stuff with his wife and daughter. This is his personal website, where you can get to his blog, music library, and other nonsense.

Latest From Twitter...

The Blog

The title is a trick title.  Plain and simple, just don't use Web Folders.

Be careful where you put your images, style sheets, JavaScript files, and other resources on your SharePoint site.  In short, store those items in either a Document Library or an Image Library, or don't store them in SharePoint at all.  Why?  The only other way to store them is in a "Web Folder".  When I say Web Folder, I mean a folder that is created either 1) by a Feature Module or 2) by hand in SharePoint Designer.  Items stored in Web Folders automatically have a "max age" of zero, have no expiration and cause unnecessary browser/server traffic.  A 304 status code on each of those items is sent back to the browser, even if the browser has already loaded those resources already in the past.  On small sites, this likely isn't a big problem.  But on high-traffic sites that serve out pages containing dozens or hundreds of images, it can be a very big deal.  In extreme cases, Web Folder resources can produce server errors and server timeouts (e.g. the server will attempt to serve out the resource for 30 seconds and then just give up). 

304's are typically passed around when the browser wants to know if the server has a new version of a file.  The browser shouldn't have to make this check on each page load.  More information on status codes can be found here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Let's look at an example.  I've developed a custom master page on a MOSS site.  It uses some out-of-the-box resources but also contains two important customizations: a header banner image and a custom CSS:

page

Using Fiddler, let's take a look at the interaction between the server and the browser on this page:

fiddler01

I've visited this page before, and you can see that for each page resource (CSS style sheets and the JPG header banner image) each produce a 304 status code.  If I visit the page again (without refreshing), the same status codes for each file are generated.

Notice the path of the resources.  All of the CSS files are stored in an out-of-the-box document library named "Style Library" (or style%20library for the URL-encoding gifted).  The header banner image (bigpisablur.jpg) is stored under the "/Assets" path.  "Assets" is not a document or image library.  It's a web folder, created with a custom Feature I created for my page branding.  Within the depths of the Feature, I'm using a module to create the web folder and add the image:

<Elements 
	xmlns="http://schemas.microsoft.com/sharepoint/">
	<Module 
	Url="Assets/KindohmMIS/Images" 
	Path="" 
	RootWebOnly="TRUE" 
	Name="Images" >
		<File 
		Url="BigPisaBlur.jpg" 
		Path="assets\KindohmMIS\images\BigPisaBlur.jpg" 
		IgnoreIfAlreadyExists="TRUE" />
  </Module>
</Elements>

While not preferred, another way to create a Web Folder is to create a folder in the site using SharePoint Designer:

spd

So, how do we eliminate these unnecessary 304 status codes? 

For items stored in a Document or Image Library, the answer is Blob Caching.  The Blob Cache will keep a cache of files stored in Document Libraries and prevent the 304 status codes.  You can enable Blob Caching in the site's web.config file by changing the Blob Cache element's "enabled" value to "true":

<BlobCache 
    location="C:\blobCache" 
    path="\.(gif|jpg|png|css|js)$" 
    maxSize="2" 
    enabled="true" />

You can read more on setting up Blob Caching in this TechNet article: http://technet.microsoft.com/en-us/library/cc263099(TechNet.10).aspx#section7.

After enabling the Blob Cache, the Fiddler output changes after loading the page (without a Refresh):

Fiddler02

As you can see, the resources stored in the Style Library no longer produce the 304 status codes.  Less traffic across the wire!

With the /Assets web folder, we will never be able to eliminate the 304 status codes if we keep it as a web folder.  There is nothing within the Module or File elements within the custom Feature that will allow us to control content expiration or the "max age".  Blob Caching can only affect resources stored in Document or Image Libraries. 

Lets try eliminating the Assets folder as a Web Folder and changing it to a file-system folder under the root of the site.  To do this, we can re-name the folder using SharePoint Designer.  I'll rename it to Assets_OLD:

assets_old

Then just to check/prove that the path is no longer valid, the page loads without the header banner:

site_nobanner

Now, I'll add the Assets folder (along with its contents) to the file system directly under the virtual root of the web site in IIS:

assets_FS

Now let's use Fiddler and see if we took care of the 304 status on the background image:

no304

No more 304 status codes!

In a small web site with a small amount of traffic, getting rid of the 304 status codes may or may not be worth this amount of effort.  However in a large site that serves out dozens or hundreds of images on a single page, I'd encourage you to eliminate these codes.  That's a lot of extra chatter going across the wire that you don't need.

Technorati Tags:   
posted on Friday, June 20, 2008 11:03 AM |

Comments

No comments posted yet.
Post Comment
Title *
Name *
Email
Url
Comment *  
Please add 5 and 7 and type the answer here: