Sitecore CDN Integration

 sitecore cdn

Sitecore was founded in 2001 and is a company which offers two major products including a content management system (CMS) and a digital marketing system (DMS). The CMS is built upon and is commonly used on an enterprise level enabling marketers to easily add content while allowing developers to customize as they see fit. Using the .NET framework has also allowed Sitecore to take advantage of it’s scalability, flexibility, and security benefits which makes it a robust competitor in the enterprise CMS market.

As many large companies are delivering content to users across the globe, taking advantage of a Sitecore CDN integration can help improve website speed and reduce latency times. This article walks users through setting up a Sitecore CDN integration with KeyCDN to help supercharge the delivery of a website’s static assets.

How to Complete a Sitecore CDN Integration

This tutorial assumes that you have already created a Zonealias and configured your DNS settings. Follow the steps below to configure your Sitecore site to begin delivering static assets via KeyCDN.

  1. Define your Sitecore’s environment variables. Replace with your Zonealias and with your KeyCDN Zone URL.
    sitecore environment variables
  2. Set your Zonealias as an Allowed Environment Domain.sitecore-environment-domains
  3. Define your Frontend Resource Host as your Zonealias and set your default resource folder to the path pointing to your site. sitecore-frontend-resources
  4. This will return the Sitecore-configured frontend resource host address in Sitecore that is environment dependent
    public static string GetResourceHost() 
        var resourceHost = GetSettingValue("Resource Host").ToString(); 
        return UseEnvironmentDomain(resourceHost);
  5. This will replace the URL domain with the corresponding environment domain if available.
    public static string UseEnvironmentDomain(string url) 
        if (!Uri.IsWellFormedUriString(url, UriKind.Absolute)) 
            return url; 
        var uri = new Uri(url); 
        var sitecoreDb = Sitecore.Context.Database; 
        var allowedDomains = GetSettingValue("Allowed Environment Domains", 
                (value) => { return value.ToString().Split('|'); }) as string[]; 
        Parallel.ForEach(allowedDomains, (id, loopState) => 
            if (TryReplaceEnvironmentDomain(sitecoreDb, id, ref uri)) 
        var newUrl = uri.ToString(); 
        return newUrl.EndsWith("/") 
            ? newUrl.Substring(0, newUrl.Length - 1) 
            : newUrl;
  6. How to actually link the assets.
        !function (a, b) {
            b.jsQueue = [], b.utag_data = <%= GetUtagDataJson() %>, a.documentElement.className = a.documentElement.className.replace(/(^|\s)no-js(\s|$)/,                         
            var c = a.createElement("link"); c.type = "text/css", c.rel = "stylesheet", c.href = "<%=LinkUtils.GenerateResourceUrl("main.css") %>", a.getElement
        }(document, window); 

Once you have completed the Sitecore CDN integration, double check your HTML source code to ensure that your URLs have been rewritten to reflect the CDN URL.

Benefits of Using a Sitecore CDN Solution

There are many benefits in using a Sitecore CDN solution. It is especially important for websites with a global presence to be optimized since as the users are further away from the origin server, the longer it will take to deliver the content. Using a CDN helps ensure that users are being delivered resources from the closest available server. A few additional benefits to using KeyCDN include: