Let’s Talk

We would love to hear from you. Want to know more about
our services or have any questions? Say Hi!

Sitecore Media URL not updating in CDN

November 9, 2022
Sitecore Media URL not updating in CDN
Ketan Garala
Ketan Garala
CTO, Sitecore MVP
sitecore-media-url-not-updating-in-cdn

Editors and Developers are facing this issue so many times that even after updating the content/Image it’s not get updated quickly we need to wait for the new updated image.

And to get it instantly updated image we have to invalidate the image url.

But we have one solution n for that we can add a query string to the DN image URL and get an updated image because the added query string will be considered as the new URL for the CDN. But here we have one more issue we might face if by applying the static query string to the image URL it will be the old URL for the third updated image. Let’s understand the scenario in a better way.

My Current CDN image URL is

https://cdn.demo.com/-/media/image%20uploader/image.ashx

Now if I update the image and keep the image name same and I want to see the updated image into same URL we can add query string and image will get reflected

https://cdn.demo.com/-/media/image%20uploader/image.ashx?image=newimage

Now If I check the URL in browser it will come up with the new image but still if you upload new image with same name and location and will check with the above URL so it will not show us updated image so we need to apply new query string again and to avoid that we can use the dynamic content which changes the query string value each time like the datetime with seconds because date and time could be same till a minute but the second will be keep updated so we can use that For Sitecore users they can use the Revision ID of the item because after each save the item revision value will be changed so if we use this it will come up with fresh query string

Here is a quick way to get a revision value of item in our custom code:

var revision = Sitecore.Context.Item["__Revision"]; 

var imageUrl = "https://cdn.demo.com/-/media/image%20uploader/image.ashx?revision=" + revision; 
                    

Sitecore have made some changes to the default LinkProvider and added a new setting to enable it in Sitecore 8.2 update 5 onwards:

<!-- MEDIA ALWAYS APPEND REVISION If true, Sitecore will append media item revision when it uses the MediaProvider API and/or the link provider to render media URLs. Default value: false (do not append media item revision) -->

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

This means that you no longer require a custom provider for cache busting, simply set the above setting to true. Sitecore 9.1+ always includes an example patch config with all the setting needed to enable CDN support in \App_Config\Include\Examples\CDN.config.example

As you can see the above code and settings will help you out to see the updated image into your website.


YOU MAY ALSO LIKE