5

Our site is implemented using SXA. Currently,caching is set on CloudFlare for our site. Due to this caching, any media file(eg: pdf, excel) we update in sitecore does not reflect on the site. I can download updated media item by appending some query string to the media url but not using the original URL.

Is there an ideal solution for this issue?

I can think of adding a query string params based on the media modified date? is this a right approach?

any advice and suggestions on this will be greatly appreciated.

4

Adding a query string will fix your issue. But it will break all your analytics reports(For example you won't be able to track number of downloads for any given pdf etc)

I would utilise cloudflare API to purge the individual media item cache when upload a new one. https://api.cloudflare.com/#zone-purge-individual-files-by-url-and-cache-tags

2

The simplest solution is to append an additional querystring parameter such as the updated date or revision id like in this previous answer I provided. You can do this by inheriting MediaProvider and override GetMediaUrl():

using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Resources.Media;

namespace MyProject.CMS.Custom.Media
{
    public class MediaProvider: Sitecore.Resources.Media.MediaProvider
    {      
        public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
        {
            string mediaURL = base.GetMediaUrl(item, options);

            mediaURL = Sitecore.Web.WebUtil.AddQueryString(mediaURL, new string[] {"revision", ((Item)item).Statistics.Revision });
            //OR, you only need one of these
            mediaURL = Sitecore.Web.WebUtil.AddQueryString(mediaURL, new string[] {"modified", ((Item)item).Statistics.Updated.ToString("yyyyMMddHHmmss") });

            return mediaURL;
        }
    }
}

And then create a config patch to use the new provider.

<mediaLibrary>
  <mediaProvider>
    <patch:attribute name="type">MyProject.CMS.Custom.Media.MediaProvider, MyProject.CMS.Custom</patch:attribute>
  </mediaProvider>
</mediaLibrary>

The URLs generated for the media items should now be in the format /-/media/path-to/image.png?la=en&h=123&w=123&hash={hash}&modified=20170817134500, the modified parameter will be updated whenever there is a change causing CloudFlare to request the media asset from the CD server.

If you need to track downloads of certain media items then you can track Download events in Sitecore, although they may tracking individually in external analytics tools as @JB has stated.

  • Any thoughts on how to do this in Sitecore 8.2, since that Sitecore.Resources.Media.MediaProvider class is marked as obsolete? – shiva Sep 14 '17 at 21:22
  • I tried to use Sitecore.Resources.Media.DefaultMediaManager but could not get it to working. I get an error "No matching constructor was found" even when I have the constructor created. I think it is due to Sitecore 8.2 new DI model. Any thoughts on how I can Override Sitecore.Resources.Media.DefaultMediaManager? – shiva Sep 14 '17 at 21:33
  • @shiva That's interesting. Although MediaProvider is marked Obsolete no alternative is provided and it is still used as the default media provider by Sitecore. All references point back to this single implementation. My guess would be that this Class has been incorrectly mared as Obselete although more likely in a future release an alternative will be provided. I know that there were some things that could not be refactored fully in the 8.2 release. – jammykam Sep 16 '17 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.