CDN Url in Sitecore Shell

Some time ago, I had to enable CDN on our Sitecore instances. After following the instructions available in Sitecore documentation, everything seemed to work fine.

After a while, I was approached by a QA specialist and presented with the following case:

When opening the Select Media dialog in Content Editor, no thumbnails are present, it seems that all the links are broken.

Apparently, when rendering the dialog, Sitecore includes the server url when generating media links.

To fix this, I created a simple processor which changes the link generation options for the shell site:

public class ShellSiteProcessor : GetMediaUrlOptionsProcessor
{
    public override void Process(GetMediaUrlOptionsArgs args)
    {
        Assert.ArgumentNotNull((object) args, nameof(args));

        if (Context.Site?.Name != "shell")
        {
            return;
        }

        args.MediaUrlBuilderOptions.AlwaysIncludeServerUrl = false;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getMediaUrlOptions>
        <processor type="Foundation.Media.Pipelines.ShellSiteProcessor, Foundation.Media" patch:after="processor[@type='Sitecore.Pipelines.GetMediaUrlOptions.Processors.SiteModeProcessor, Sitecore.Kernel']" />
      </getMediaUrlOptions>
    </pipelines>
  </sitecore>
</configuration>

After patching the processor in, everything was fine again in the land of Sitecore 🌈

Note: This post is based on a clean Sitecore 9.3 installation and was not tested on other versions.

Cover photo by Anastasia Dulgier on Unsplash