Skip to main content

Using Dianoga's async image optimization on CDN


Dianoga is an automatic image optimizer for the Sitecore media library. It reduces the size of your images served from Sitecore by 8-70% automatically. Dianoga is great for situations where content editors may not be image editing experts.

How Dianoga works?

By default, Dianoga runs asynchronously after the image is saved into the media cache. This means it has practically no effect on the site's frontend performance (though it does use some CPU time in the background).

Dianoga updates the cache after the first image request. This means the first hit to images will serve the unoptimized version of image. Subsequent requests will receive the optimized version from cache.

Where is the problem?

Dianoga's behavior of serving unoptimized images on first request creates problem while using CDNs. CDNs cache the responses of media requests. Since first hit to images serve unoptimized image, CDN will cache the unoptimized version. CDN will serve this unoptimized version of image in its cache to all subsequent requests to image.

How to mitigate this?

You can enable Dianoga.Strategy.GetMediaStreamSync.config.disabled and disable Dianoga.Strategy.MediaCacheAsync.config. This will cause optimization to occur synchronously as the media is first requested, which is appropriate if the media is being sent to a CDN. However, this will cause a delay for the first hit user before they start seeing images and can impact the page metrics.

What we did?

We decided to keep using async strategy but set the max-age header to 2 minutes in case an unoptimized version of image is being returned in response. This ensures that on first request for an image, user doesn't experience a delay and also, that once the optimized version of image is available in media cache folder, optimized version of image is served via CDN.

How To Do It?

To achieve this, you have to override DoProcessRequest method of MediaRequstHandler as in code snippet below -

 public class MediaRequestHandler : Sitecore.Resources.Media.MediaRequestHandler

    {

        private readonly string[] disabledRangeRetrievalMimeTypes;


        public MediaRequestHandler()

        {

            this.disabledRangeRetrievalMimeTypes = ((IEnumerable<string>)Settings.GetSetting("Media.DisableRangeRetrievalRequestMimeTypes").Split(',')).Select<string, string>((Func<string, string>)(x => x.Trim())).Where<string>((Func<string, bool>)(x => !string.IsNullOrEmpty(x))).ToArray<string>();

        }


        protected override bool DoProcessRequest(

          HttpContext context,

          MediaRequest request,

          Sitecore.Resources.Media.Media media)

        {

            Sitecore.Diagnostics.Assert.ArgumentNotNull((object)context, nameof(context));

            Sitecore.Diagnostics.Assert.ArgumentNotNull((object)request, nameof(request));

            Sitecore.Diagnostics.Assert.ArgumentNotNull((object)media, nameof(media));

            if (this.Modified(context, media, request.Options) == Tristate.False)

            {

                Event.RaiseEvent("media:request", (object)request);

                this.SendMediaHeaders(media, context);

                context.Response.StatusCode = 304;

                return true;

            }

            this.ProcessImageDimensions(request, media);

            MediaStream stream = media.GetStream(request.Options);

            if (stream == null)

                return false;

            Event.RaiseEvent("media:request", (object)request);

            bool flag = this.IsRangeRetrievalAllowedType(media);

            if (Settings.Media.EnableRangeRetrievalRequest && Settings.Media.CachingEnabled && flag)

            {

                using (stream)

                {

                    this.SendTempOrCorrectMediaHeaders(stream, media, context);

                    RangeRetrievalRequest retrievalRequest = RangeRetrievalRequest.BuildRequest(context, media);

                    if (retrievalRequest.Precondition is Sitecore.Resources.Media.Streaming.Preconditions.IfUnmodifiedSincePrecondition)

                    {

                        Sitecore.Support.Resources.Media.Streaming.Preconditions.IfUnmodifiedSincePrecondition sincePrecondition = Sitecore.Support.Resources.Media.Streaming.Preconditions.IfUnmodifiedSincePrecondition.TryBuild(retrievalRequest, context);

                        if (sincePrecondition != null)

                            retrievalRequest.Precondition = (IPrecondition)sincePrecondition;

                    }

                    new RangeRetrievalResponse(retrievalRequest, stream).ExecuteRequest(context);

                    return true;

                }

            }

            else

            {

                this.SendTempOrCorrectMediaHeaders(stream, media, context);

                this.SendStreamHeaders(stream, context);

                using (stream)

                {

                    context.Response.AddHeader("Content-Length", stream.Stream.Length.ToString());

                    WebUtil.TransmitStream(stream.Stream, context.Response, Settings.Media.StreamBufferSize);

                }

                return true;

            }

        }


        private bool IsRangeRetrievalAllowedType(Sitecore.Resources.Media.Media media)

        {

            return !((IEnumerable<string>)this.disabledRangeRetrievalMimeTypes).Contains<string>(media.MediaData.MediaItem.InnerItem["Mime Type"]);

        }


        private void ProcessImageDimensions(MediaRequest request, Sitecore.Resources.Media.Media media)

        {

            Sitecore.Diagnostics.Assert.ArgumentNotNull((object)request, nameof(request));

            Sitecore.Diagnostics.Assert.ArgumentNotNull((object)media, nameof(media));

            Item innerItem = media.MediaData.MediaItem.InnerItem;

            int result1;

            int.TryParse(innerItem["Height"], out result1);

            int result2;

            int.TryParse(innerItem["Width"], out result2);

            bool flag = false;

            int maxHeight = Settings.Media.Resizing.MaxHeight;

            if (maxHeight != 0 && request.Options.Height > Math.Max(maxHeight, result1))

            {

                flag = true;

                request.Options.Height = Math.Max(maxHeight, result1);

            }

            int maxWidth = Settings.Media.Resizing.MaxWidth;

            if (maxWidth != 0 && request.Options.Width > Math.Max(maxWidth, result2))

            {

                flag = true;

                request.Options.Width = Math.Max(maxWidth, result2);

            }

            if (!flag)

                return;

            Log.Warn(string.Format("Requested image exceeds allowed size limits. Requested URL:{0}", (object)request.InnerRequest.RawUrl), (object)this);

        }


        protected virtual void SendTempOrCorrectMediaHeaders(MediaStream mediaStream, Sitecore.Resources.Media.Media media, HttpContext context)

        {

            // if it's memory stream, that means the Dianoga has not compressed image yet

            if (mediaStream.Stream is MemoryStream)

            {

                this.SendTemporaryMediaHeaders(media, (HttpContextBase)new HttpContextWrapper(context));

            }

            else

            {

                this.SendMediaHeaders(media, context);

            }

        }


        protected virtual void SendTemporaryMediaHeaders(Sitecore.Resources.Media.Media media, HttpContextBase context)

        {

            TimeSpan delta = TimeSpan.FromMinutes(2);

            DateTime date = media.MediaData.Updated;


            if (date > DateTime.UtcNow)

                date = DateTime.UtcNow;


            HttpCachePolicyBase cache = context.Response.Cache;

            cache.SetLastModified(date - delta);

            cache.SetETag(media.MediaData.MediaId + "_temp");

            cache.SetCacheability(Settings.MediaResponse.Cacheability);

            

            if (delta > TimeSpan.Zero)

            {

                if (delta > TimeSpan.FromDays(365.0))

                    delta = TimeSpan.FromDays(365.0);

                cache.SetMaxAge(delta);

                cache.SetExpires(DateTime.UtcNow + delta);

            }

            Tristate slidingExpiration = Settings.MediaResponse.SlidingExpiration;

            if (slidingExpiration != Tristate.Undefined)

                cache.SetSlidingExpiration(slidingExpiration == Tristate.True);

            string cacheExtensions = Settings.MediaResponse.CacheExtensions;

            if (cacheExtensions.Length > 0)

                cache.AppendCacheExtension(cacheExtensions);

            string varyHeader = this.GetVaryHeader(media, context.ApplicationInstance.Context);

            if (string.IsNullOrEmpty(varyHeader))

                return;

            context.Response.AppendHeader("vary", varyHeader);

        }

    }


Like always, like and share :) Thanks for your time!

Popular posts from this blog

Sitecore PowerShell Script to create all language versions for an item from en version

  We have lots of media items and our business wants to copy the data from en version of media item to all other language versions defined in System/Languages. This ensures that media is available in all the languages. So, we created the below powershell script to achieve the same -  #Get all language versions defined in System/Languages $languages = Get-ChildItem /sitecore/System/Languages -recurse | Select $_.name | Where-Object {$_.name -ne "en"} | Select Name #Ensuring correct items are updated by comparing the template ID  $items = Get-ChildItem -Path "/sitecore/media library/MyProjects" -Recurse | Where-Object {'<media item template id>' -contains $_.TemplateID} #Bulk update context to improve performance New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) { foreach($item in $items){    foreach($language in $languages){ $languageVersion = Get-Item -Path $item.Paths.Path -Language $language.Name #Check if language versi

Export Sitecore media library files to zip using SPE

If you ever require to export Sitecore media files to zip (may be to optimize them), SPE (Sitecore Powershell Extension) has probably the easiest way to do this for you. It's as easy as the below 3 steps -  1. Right click on your folder (icons folder in snap)>Click on Scripts> Click on Download 2. SPE will start zipping all the media files placed within this folder. 3. Once zipping is done, you will see the Download option in the next screen. Click Download Zip containing the media files within is available on your local machine. You can play around with the images now. Hope this helps!! Like and Share ;)

Make Sitecore instance faster using Roslyn Compiler

When we install the Sitecore instance on local, the first load is slow. After each code deploy also, it takes a while for the Sitecore instance to load and experience editor to come up. For us, the load time for Sitecore instance on local machines was around 4 minutes. We started looking for ways to minimize it and found that if we update our Web.config to use Roslyn compiler and include the relevant Nugets into the project, our load times will improve. We followed the simple steps - Go to the Project you wish to add the NuGet package and right click the project and click 'Manage NuGet Packages'. Make sure your 'Package Source' is set to nuget.org and go to the 'Browse' Tab and search Microsoft.CodeDom.Providers.DotNetCompilerPlatform. Install whichever version you desire, make sure you note which version you installed. You can learn more about it  here . After installation, deploy your project, make sure the Microsoft.CodeDom.Providers.DotNetCompilerPlatform.d

Experience of a first time Sitecore MVP

The Journey I have been working in Sitecore for almost 10 years now. When I was a beginner in Sitecore, I was highly impressed by the incredible community support. In fact, my initial Sitecore learning path was entirely based on community written blogs on Sitecore. During a discussion with my then technology lead Neeraj Gulia , he proposed the idea that I should start giving back to developer community whenever I get chance. Just like I have been helped by many developers via online blogs, stackoverflow etc., I should also try to help others. Fast forward a few years and I met  Nehemiah Jeyakumar  (now an MVP). He had a big archive of his technical notes in the form Sitecore blogs. I realized my first blog dont have to be perfect and it can be as simple as notes to a specific problem for reference in future. That's when I probably created my first blog post on Sitecore. At that time, I didn't knew about the Sitecore MVP program. Over the years, I gained more confidence to write

Clean Coding Principles in CSharp

A code shall be easy to read and understand. In this post, I am outlining basic principles  about clean coding after researching through expert recommended books, trainings and based on my experience. A common example to start with is a variable declaration like - int i  The above statement did not clarify the purpose of variable i. However,  the same variable can be declared as -  int pageNumber The moment we declared the variable as int pageNumber, our brain realized that the variable is going to store the value for number of pages. We have set the context in our brain now and it is ready to understand what the code is going to do next with these page numbers. This is one of the basic advantages of clean coding. Reasons for clean coding -  • Reading clean code is easier - Every code is revisited after certain amount of time either by the same or different developer who created it. In both the cases, if the code is unclean, its difficult to understand and update it. • To avoid s