Tuesday 20 August 2024

Sitecore SXA Code Datasource :: Caching!!

In this quick blog post I wanted to share my experience working with code: datasource type, if you didn't hear of this type of datasource in Sitecore with SXA, this blog will be a good article to go through, in addition I wanted to share how we can configure datasource caching OOTB and in some cases using custom code if needed, interested, keep reading! 


One of the very useful features in Sitecore SXA is the ability to use code to resolve a datasource for a component, This can be convenient when you want to return different items, for example, based on the time of the day or other business-specific criteria, its very simple to implement, here is where you can find all information about how you can implement your own custom datasource code resolver: 

https://doc.sitecore.com/xp/en/developers/sxa/latest/sitecore-experience-accelerator/use-a-prefix-to-set-the-data-source-context.html#resolve-the-data-source-using-code


As you may have seen in the documentation above, all the related logic for this type of datasource resolving exists in resolveRenderingDatasource pipeline, following are all the processors you can find in it: 


The one processor that we're interested in is the following: 

<processor type="Sitecore.XA.Foundation.LocalDatasources.Pipelines.ResolveRenderingDatasource.CodeDatasource, Sitecore.XA.Foundation.LocalDatasources" resolve="true" patch:source="Sitecore.XA.Foundation.LocalDatasources.config"/>

 In this processor you can find the logic behind the code datasource type, if you use a reflector to decompile Sitecore.XA.Foundation.LocalDatasources to find the code for CodeDatasource You will find that the code is design in a way to caching the resolved data source item, which is good on most cases, and you can find how the caching is basically configured using the following sitecore setting configuration: 


But, what if you want to disable this datasource caching in some special case you have, how you can do that, setting the above configuration to false will disable caching for all components using this type of datasource resolving which is not very good, so here is sample code below, where you can update line 34 with your special condition to disable/enable caching: 


Following is a configuration file patching to use the above custom code datasource instead of the OTTB one: 

It could be better if the rendering respect the caching setting on the rendering definition instead if always caching based on the sitecore setting mentioned above, but at least, now you have a way to workaround it in case needed, Hopefully it helps! 

No comments:

Post a Comment