Customizing CSSLink and ScriptLink for public Sharepoint site

MSDN has quite a thorough topic on optimizing a Sharepoint Server for public facing site:

http://msdn.microsoft.com/en-us/library/bb727371.aspx

I would just like to add a few more points and share some thoughts on our experiences of customizing for performance in addition to this guide.

  1. Design your Sharepoint website with performance in mind.  If you had it as an after thought and think "ooh, I'll just add performance bits at the end", then your options are far more limited.  Not to mention the extra testing time you'll need.  If you are in this situation, I'd say just stick with the caching options.
  2. Most visitors to a public facing site would be anonymous users - in fact, only a very small percentage of people may actually want to modify the website.  So switch on anonymous login and start your optimizations there
  3. The easiest bits to set up are the caching options for Sharepoint.  Sharepoint Server 2007 provides 3 means of caching:
    1. Page Output Caching - this is basically ASP.NET output caching, wrapped within Sharepoint.  Follow the discussion in the article above will get you sorted.  This has the biggest performance improvement for large amount of anonymous visitors.
    2. Object Caching - this is Sharepoint caching recently accessed object's properties (and lists) so that Sharepoint doesn't have to re-access them from Database.
    3. Binary Large Objects (BLOB) caching - this is Sharepoint caching large objects that are otherwise stored in the database on a file server.  Things like pictures, javascript files, audio, SilverLight, Flash, movies, etc.  This has to be set via the web.config (defaults to 10 GB of space allocated for this).
    4. OK, these are the easy stuff - read the MSDN link if you need instructions to set this up.
  4. The article further discusses rendering core.js for only authenticated users.  This takes a bit more effort to figure out.
    1. The idea behind the RegisterCoreWhenAuthenticatedControl is good, but proper implementation requires understanding of how ScriptLink works.
    2. A ScriptLink control can be specified on the page at one point, and subsequent ScriptLink.Register will add more scripts to be added at that point.
    3. A ScriptLink control will always register core.js unless it is in minimal mode
    4. To create a ScriptLink in minimal mode, create one in the markup without the name attribute (yes.  this is really cryptic - here's why you need Reflector)
    5. <Sharepoint:ScriptLink runat="server"/>
    6. Follow by the control specified in the article:
    7. <NoCoreLoad:RegisterCoreWhenAuthenticatedControl runat="server"/>
  5. We took the idea a bit further and thought if we can get rid of the core.js file, how about get rid of the core.css file too, or in fact, anything.  What we need is a wrapper container of some sort.
    1. There are two related classes for CssLink
    2. The CssRegistration class adds CSS files to the Sharepoint context
    3. The CssLink control renders all CSS files added to the Sharepoint context
    4. The CssLink can have additional DefaultUrl and AlternativeUrl specified,  but it will always render the core.css file.  There is no minimal mode
    5. The way we use is to create a wrapper container for CssLink

      public class AuthenticatedPanel : Panel
      {
          protected override void Render(HtmlTextWriter writer)
          {
              if (HttpContext.Current.Request.IsAuthenticated)
              {
                  base.Render(writer);
              }
          }
          public override void RenderBeginTag(HtmlTextWriter writer)
          {
              //disable rendering <div>
              //base.RenderBeginTag(writer);
          }
          public override void RenderEndTag(HtmlTextWriter writer)
          {
              //disable rendering </div>
              //base.RenderEndTag(writer);
          }
      }
    6. <AuthenticatedPanel>
          <CssLink runat="server"/>
          <CssRegistration runat="server ... />
      </AuthenticatedPanel>

      <link rel="stylesheet" type="text/css" href="...path" />
  6. I like the AuthenticatedPanel that we created a lot more than the RegisterCoreWhenAuthenticatedControl - it is far more flexible in terms of the control we get on what gets rendered on the page at the end.

Good luck!