1

Environment: Sc 10, SXA 10x, Azure PaaS

Sitecore adds query string param rev=-1 to the media url whenever there is no language version available for asset other than default Language.

ex: /-/media/project/site/home/hero-banner.jpg?rev=-1

This happens with Multilanguage environment, We have media library setup with assets based on Unrevisioned Image Template !! Although our default site language is en-us we prefer to manage all assets in the fallback language en (this helps to have single asset across langs)

This works as expected with asset rendering as the media blob is shared field which means the actual asset is shared across languages, however due to rev being added as -1 have been causing issues with caching whenever the asset is modified (detaching and attaching a new asset causes the rev not to be updated and url is not unique anymore).

Could someone help let me know if this is expected behavior to have media assets in all language versions for all the available site languages ? (despite the Media field is Shared)

1 Answer 1

2

Post some investigation noticed that sitecore adds this rev=-1 whenever there is no language version for the media item in the current language scope !!

Although it was a recommended practice for performance to have media items in default languages as the Mediafield blob is Shared anyway which was enough to fetch un-revisioned assets ,

However the issue lies in the behavior of Build method Sitecore.Links.UrlBuilders.MediaUrlBuilder code in fetching the revision.

Reason: As the revision field being Unshared and when the media item does not exists for a default language item the Build method tries to resolve the Revision field on this language context and is not found hence defaults to -1.

Solution: I have a workaround to fetch the revision from fallback language which is en in our case whenever there is no relevant media item in default language , this shall ensure to always have the revision which helps CDN caches to work as expected with UniqueUrls whenever assets being modified on same media item.

Patch : <links> <mediaUrlBuilder> with the following override:

public override string Build(MediaItem item, MediaUrlBuilderOptions options)
    {
        var mediaUrl = base.Build(item, options);

        if(item == null || item.InnerItem == null)
        {
            return mediaUrl;
        }

        if(mediaUrl.IndexOf("rev=-1", StringComparison.OrdinalIgnoreCase) > -1)
        {
            var defaultEnItem = Sitecore.Context.Database.GetItem(item.InnerItem.ID, Sitecore.Globalization.Language.Parse("en"));
            var fallbackItemRevision = defaultEnItem?.Fields[new ID(_revision)]?.ToString();
            if (!string.IsNullOrWhiteSpace(fallbackItemRevision)) {
                return Guid.TryParse(fallbackItemRevision, out Guid result) ? mediaUrl.Replace("rev=-1", $"rev={result.ToString("N")}") : mediaUrl;
            } 
        }

        return mediaUrl;
    }

Note: I an not sure if this is fixed in latest versions of Sitecore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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