Enabling your Sitecore website to effectively store and use digital assets, can prove somewhat challenging. We offer solutions to a few common issues when dealing with content delivery networks:

As the trend for websites skews to bigger, better imagery and a strong multimedia presence, developers require elegant solutions for storing and presenting a large number of digital assets. Our own Sitecore developers are often required to integrate digital assets that are manged within the Sitecore Media Library with a content delivery network.

The main advantage of using content delivery networks is to remove highly consuming payloads from a website's server. Lazy loaded CDNs like Akamai or AWS offer better performance and caching mechanisms. 

In Sitecore, we can achieve this with the setup of different domains that will redirect the request for a Sitecore image to the actual content server (your CDN). 

Sitecore digital asset management with CDN

Since Sitecore version 6.4, we've had out-of-the-box configuration to define the URL prefix for items coming from the Media Library. The entry for the web.config looks like this:

Sitecore content delivery networks

You'll notice in the comment section that it mentions that this is typically used “when your solutions is configured to store Sitecore media on a content delivery network.”

Unfortunately there are a few problems with this approach: firstly, media items will always be delivered using the defined URL prefix, regardless of whether you're authoring content and on preview mode or actually browsing the published site. To avoid this issue, you could separate your environments and make sure each one has its own Media.MediaLinkServerUrl configured accordingly. 

Secondly, the URL generated for a given file doesn't provide information about its version or published date/time. For example, if you have an image item called logo, its URL might be something like http://cdnhosturl/~media/images/logo.ashx. If the logo image was updated, as the URLs remains the same, the CDN would not know if its cached should be cleared or not. 

To avoid this issue, solutions such as the Sitecore CDN Connector append URL parameters to ensure the CDN is aware of the version of the file. In our example, the logo file would be delivered as something like http://cdnhosturl/~media/images/logo.ashx?vs=1&d=20142910T120000.

The module provides its own set of configuration settings to be appended to the web.config file and a more granular custom URL rewriting logic where, through the appropriate parameters, specific file extensions can be defined to be included or excluded for rewriting.

Multiple CDN URL prefixes

Another issue you may run into with existing solutions is one where they may only accommodate a single URL prefix for CDN. In some cases, clients may have different URL prefixes for different types of media. For example, one CDN for images and another for videos. 

To implement that, the methods that return the CDN host of the Sitecore CDN Connector need to be modified to receive the type of media as a parameter:

Sitecore content delivery networks
Sitecore content delivery networks      

As well, the method that actually does the replacement of URLs (ReplaceMediaURL method inside CDNProvider.cs) also needs to be modified to accommodate the selection of different hostnames based on the media type:

        cdnHostname = GetCDNHostName(GetMediaType(mediaItem));
        url.HostName = cdnHostname;

Finally, the GetMediaType method needs to be implemented to return the appropriate types that will have different CDN host names. For example, we could have the following types in the config file:

<sites>
      <site name="website">
          <patch:attribute name="cdnHostName">yourcdnhostname</patch:attribute>
          <patch:attribute name="cdnHostNameImages">images.yourcdnhostname</patch:attribute>
          <patch:attribute name="cdnHostNameVideos">videos.yourcdnhostname</patch:attribute>
      </site>
</sites>

The bottom line

Most of the hard work around making Sitecore render digital media assets to be properly cached and then delivered by a CDN was already done and is available on the Sitecore Marketplace. Extending it and making it also rewrite URLs for multiple host names serving different types of media assets in a same Sitecore site can be achieved using the proposed solution in this post.

comments powered by Disqus