0

We noticed that if an image is not published, pages containing that image in a Responsive Image rendering variant are broken. And not just missing the image - but actually broken.

The site is running on Sitecore 10.2 and is patched with the latest hotfix packages so actually running on 10.2.1 (actually even 10.2.2 now).

Once the image is published, the issue is gone. If no image is selected, there is also no issue. It seems that if there is a reference to an image that does not exist in the web database, the component fails.

1 Answer 1

2

In the release notes for version 10.3 we found the matching bug:

If a Responsive Image variant field points to a broken link, a NullReferenceException error occurs.

Upgrading is 10.3 is not an option though. But I found a way to "patch" the issue. The problem is located in RenderFallbackResponsiveMImage which uses the (deprecated) StylelabsDataExtractor.GetSrcAttributeValue. Replacing that with the new version for that function actually already solves the issue.

Note that this code came with the hotfix (10.2.1).

If you want to fix this without upgrading, create a class like this:

using System.Web.UI;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Web.UI.WebControls;
using Sitecore.XA.Feature.Stylelabs.Services;
using Sitecore.XA.Foundation.Variants.Abstractions.Pipelines.RenderVariantField;

namespace Foundation.Infrastructure
{
    public class RenderFallbackResponsiveMImage : Sitecore.XA.Feature.Stylelabs.Pipelines.RenderVariantField.RenderFallbackResponsiveMImage
    {
        public RenderFallbackResponsiveMImage(IStylelabsDataExtractor stylelabsDataExtractor) : base(stylelabsDataExtractor)
        {
        }

        public override void RenderField(RenderVariantFieldArgs args)
        {
            var variantField = args.VariantField;
            if (args.Item.Paths.IsMediaItem || string.IsNullOrWhiteSpace(variantField?.FieldName))
            {
                base.RenderField(args);
            }
            else if (PageMode.IsExperienceEditorEditing)
            {
                args.ResultControl = (Control)new FieldRenderer()
                {
                    Item = args.Item,
                    FieldName = variantField.FieldName,
                    DisableWebEditing = !args.IsControlEditable
                };
                args.Result = this.RenderControl(args.ResultControl);
            }
            else
            {
                var field1 = args.Item.Fields[variantField.FieldName];
                if (field1 == null)
                {
                    return;
                }

                if ((FieldTypeManager.GetField(field1) is ImageField field2 ? field2.MediaItem : (Item)null) == null)
                {
                    field2 = (ImageField)field1;
                    string srcAttributeValue = this.StylelabsDataExtractor.GetAttributeValue("src", field2);
                    Control fieldRenderer = this.CreateFieldRenderer(args);
                    this.RenderControl(args, fieldRenderer, srcAttributeValue);
                }
                else
                {
                    base.RenderField(args);
                }
            }
        }
    }
}

Note that we are using this.StylelabsDataExtractor.GetAttributeValue("src", field2) now - this is the line that solves the problem.

Patch the config:

<sitecore>
  <pipelines>
    <renderVariantField>
      <processor patch:instead="*[@type='Sitecore.XA.Feature.Stylelabs.Pipelines.RenderVariantField.RenderFallbackResponsiveMImage, Sitecore.XA.Feature.Stylelabs']"
                type="Foundation.Infrastructure.RenderFallbackResponsiveMImage, Foundation.Infrastructure" resolve="true" />
    </renderVariantField>
  </pipelines>
</sitecore>

and you're good to go.

ps: Don't forget to delete this code if you upgrade to 10.3.

2
  • Is there a Hotfix DLL provided by Sitecore Support Team for the same? Commented Oct 24, 2023 at 18:46
  • Not that I know of - we contacted support but they told us about the fix in the 10.3 version. To be honest, I didn't want to wait for a fix as it was an easy fix and we will never upgrade this project.
    – Gatogordo
    Commented Oct 24, 2023 at 19:04

Your Answer

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

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