The most straightforward approach will likely be to extend and customize the Sitecore.JavaScriptServices.ViewEngine.LayoutService.Serialization.LayoutTransformer
, specifically, the Transform(RenderedItem rendered)
method.
The default LayoutTransformer
is declared via config in /App_Config/Sitecore/LayoutService/Sitecore.LayoutService.Jss.config
.
You'll see the jss
named configuration for Layout Service:
<config name="jss">
<rendering type="Sitecore.LayoutService.Configuration.DefaultRenderingConfiguration, Sitecore.LayoutService">
<placeholdersResolver type="Sitecore.LayoutService.Placeholders.DynamicPlaceholdersResolver, Sitecore.LayoutService" />
<itemSerializer type="Sitecore.JavaScriptServices.ViewEngine.LayoutService.JssItemSerializer, Sitecore.JavaScriptServices.ViewEngine" resolve="true">
<AlwaysIncludeEmptyFields>true</AlwaysIncludeEmptyFields>
</itemSerializer>
<renderingContentsResolver type="Sitecore.LayoutService.ItemRendering.ContentsResolvers.RenderingContentsResolver, Sitecore.LayoutService">
<IncludeServerUrlInMediaUrls>true</IncludeServerUrlInMediaUrls>
</renderingContentsResolver>
</rendering>
<serialization type="Sitecore.JavaScriptServices.ViewEngine.LayoutService.SerializationConfiguration, Sitecore.JavaScriptServices.ViewEngine">
<transformer type="Sitecore.JavaScriptServices.ViewEngine.LayoutService.Serialization.LayoutTransformer, Sitecore.JavaScriptServices.ViewEngine" resolve="true" />
</serialization>
</config>
And within that named configuration is the LayoutTransformer
declaration (config/rendering/serialization/transformer
).
Lastly, a custom layout transformer may look something like below. Note that if the custom condition is met, then the route
object will not be in the final output.
public class CustomLayoutTransformer : LayoutTransformer
{
public CustomLayoutTransformer(IPlaceholderTransformer placeholderTransformer) : base(placeholderTransformer)
{
}
public override dynamic Transform(RenderedItem rendered)
{
var baseResult = base.Transform(rendered);
if (myCustomCondition)
{
dynamic newResult = new ExpandoObject();
newResult.sitecore = new
{
baseResult.context
};
return newResult;
}
return baseResult;
}
}