2

I created a first custom rendering for SXA with variant as described in the docs with a repository that inherits from VariantsRepository and it seems to work fine (we created one variant and that worked as expected).

I had to create another rendering that was rather similar - it belonged in the same feature, even in the same controller. So I created a new action method and placed a new GetModel function in my repository. Created the rendering in Sitecore, added a variant and all seemed fine.

But.. when I go to a page that has rendering B and then to a page with rendering A they seem to be interacting. Same thing happens the other way round. The first visited rendering seems to affect the second one as if caching is involved.

  • I checked all caching settings. There are none.
  • I added a breakpoint in both methods in my controller and both are called.

What could be causing this?

1 Answer 1

3

Further debugging showed me that the VariantsRepository from SXA inherits from a AbstractVariantsRepository and that one holds the variantItem and variantFields as an instance variable in the repository. Those get filled when you call FillBaseProperties, but only if they are not already filled.

So my own repository (based upon the VariantsRepository) also has those properties. But I was using DI and as I usually don't put properties in my repositories so I marked my repository to be a singleton.

This caused the repository to read the variants item and fields for the first called rendering and keep those for all next calls. Creating a separate repository for the second rendering would not fix this - I had to remove the Singleton setting and replace that by Transient.

Lesson learned: when using DI, always add the VariantsRepository based repositories as Transient so they are created each time. This way the variant data is correctly filled by the FillBaseProperties method.

You could also override the VariantItem and VariantFields property getters without the null checks but that does not sound like the best idea.

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.