Cachebusting Sitecore Media Images

Media.AlwaysAppendRevision setting

You may know that replacing the blob of an existing media item in Sitecore with another one will cause browser cache issues. The reason for this is that although the actual image may be replaced, the browser does not know that the image has changed because the URL remains the same.

There is a Sitecore setting to always append the revision to the URL:

<setting name="Media.AlwaysAppendRevision" value="true"/>

If the setting is set to true, Sitecore will append a query string with something like the following to the media image URL:


This is useful as now the URL is different, and thus the cache gets busted whenever an image is updated and published.

In a multilanguage solution, you may notice that the Media.AlwaysAppendRevision does not work and that setting it to true returns ?rev=-1 in every language except for “en”. This is because although the image is a unversion/image template, the __Revision field is versioned by default.

To get around this issue, enable item and field fallback, and enable field level fallback on the __Revision field of the unversion/image template.

Alternatively, if you wish to avoid touching native Sitecore templates, you can replace the mediaUrlBuilder with custom logic to always pull from the “en” language

Config File

          <mediaUrlBuilder type="Sitecore.Links.UrlBuilders.MediaUrlBuilder,
              <patch:attribute name="type">
                 YourNameSpace.CustomMediaUrlBuilder, YourNameSpace


using Sitecore;
using Sitecore.Links.UrlBuilders;
using System;

public class CustomMediaUrlBuilder : MediaUrlBuilder
   public const string Revision = "rev";
   public const string BadValue = "-1";

   public CustomMediaUrlBuilder(DefaultMediaUrlBuilderOptions defaultOptions,
      string mediaLinkPrefix)
      : base(defaultOptions, mediaLinkPrefix) { }

   protected override void AddQueryString(UrlBuildModel model,
      MediaUrlBuilderOptions options)
      base.AddQueryString(model, options);

      // Exit code if not unversioned image
      if (model.Item.TemplateID != TemplateIDs.UnversionedImage) return;

      // Exit code if AlwaysAppendRevision is false
      if (!options.AlwaysAppendRevision.GetValueOrDefault()) return;

      // if the query string has rev and it equals -1
      if (model.QueryStringParts.ContainsKey(Revision)
         && model.QueryStringParts[Revision] == BadValue)
         var en = Sitecore.Globalization.Language.Parse("en");
         var item = Context.Database.GetItem(model.Item.ID, en);

         Guid rev;
         if (Guid.TryParse(item.Statistics.Revision, out rev))
            model.QueryStringParts[Revision] = rev.ToString("N");