asp.net-mvcsitecoresitecore8sitecore-mvcdevice-detection

How to invalidate html cache of sitecore in responsive website?


We have a sitecore based responsive website, which has different content for mobile device in some pages unlike desktop. We are identifying the device type by using Http Request user Agent and have different business logic based on it.

When we have enabled sitecore HTML caching for those renderings then the logic that works with user agent logic does not work. Because sitecore cache is not getting invalidated By Vary by Device. Therefore for different device also sitecore is giving the response from same HTML cache of the rendering.

How should we handle this scenario?


Solution

  • You can create your own custom “vary by” with your business logic for the user agent. Or use the Vary by Device but than you need to use Devices the way Sitecore support, create your devices below /sitecore/layout/Devices and use them.

    Create a RenderRenderingProcessor (Sitecore.Mvc.Pipelines.Response.RenderRendering) for the mvc.renderRendering pipeline. And add the checkbox to template /sitecore/templates/System/Layout/Sections/Caching/

    For a code example see Custom Cache Criteria with MVC in the Sitecore from John West

    More about caching see Sitecore Caching and Sitecore Custom Cache