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

Many editors and developers are facing this issue where the Sitecore media URL is not updating in the CDN. They are facing this issue even after updating the content or image and it does not get updated quickly. There is a longer wait time for getting the updated image. Also, to get it instantly updated, they need to invalidate the image URL.

But there is one solution for this. We can add a query string to the DN image URL and get an updated image because by adding the query string, it will be considered as a new URL for the CDN. There is of course one more issue here that can arise is that by applying the static query string to the image URL, it will be the old URL for the third updated image. Let us try to understand this scenario in a better way.

Our current CDN image URL is

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

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

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

This is why you need to apply a new query string again and to avoid that, you can use dynamic content which changes the query string value each time like the date, time with seconds, because the date and time be same till a minute but the seconds will keep getting 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 get changed. So, if you are using this, it will come up with a 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