Support

Find answers, guides, and tutorials to supercharge your content delivery.

Sitecore CDN Integration

Updated on October 4, 2018
Sitecore CDN Integration

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 ASP.net 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 Zone Alias 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 static.example.com with your Zone Alias and example-hexid.kxcdn.com with your KeyCDN Zone URL.

  2. Set your Zone Alias as an Allowed Environment Domain.

  3. Define your Frontend Resource Host as your Zone Alias and set your default resource folder to the path pointing to your site.

  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))
                loopState.Break();
        });
    
        var newUrl = uri.ToString();
    
        return newUrl.EndsWith("/")
            ? newUrl.Substring(0, newUrl.Length - 1)
            : newUrl;
    }
    
  6. How to actually link the assets.

    <script>
        !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);
    </script>
    

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:

Supercharge your content delivery 🚀

Try KeyCDN with a free 14 day trial, no credit card required.

Get started
KeyCDN uses cookies to make its website easier to use. Learn more