Configure HTML caching

Version:

The HTML cache is an output cache that caches renderings, not whole pages. You use the HTML cache to improve the performance of websites.

To enable the HTML output cache:

  1. Patch the site specification of predefined sites in the sites node of the \App_Config\Sitecore\CMS.Core\Sitecore.Sites.config file and set the cacheHtml property of sites to true to enable the HTML output cache for that site.

    Note

    The website default site set up during installation is already there. You must specify this setting for any site you create yourself and that you want HTML caching for.

    Note

    You can specify the size of each cache in the htmlCacheSize property of a site node.

  2. Add caching options to the renderings Sitecore caches output for. You can do this in two ways:

    • Locally by specifying the options every time you use a rendering. To do this, open the item in the Content Editor, and on the Presentation tab, click Details, and then click the rendering you want to set options for.
    • Globally by specifying the options in the definition item of the rendering. This is then the default setting for all uses of this rendering. To do this, open the rendering definition item in the sitecore/Layout/Renderings part of the content tree, and navigate to the options in the Caching section.

    The options are:

    • Cacheable

      Specifies if Sitecore caches the rendering or not.

    • Clear on Index Update

      Sitecore clears the cache when it updates the content search index.

      This is important if your Sitecore instance uses HTML caching for renderings, controls, or sublayouts that contain code, and this code depends on an index. For more information, see Index-dependent HTML caching.

    • Vary by Data

      Sitecore caches output based on the item accessed. This is useful when content is highly consistent, for example: headers and footers.

    • Vary by Device

      Sitecore caches output for each device separately.

    • Vary by Login

      Sitecore caches two copies of the output: one for authenticated users, and one for unauthenticated users.

    • Vary by Parameters

      Sitecore caches output for each parameter the rendering accepts.

    • Vary by Query String

      Sitecore caches output for each unique combination of query string parameters.

    • Vary by User

      Sitecore caches output for each authenticated user.

  3. Sitecore clears caches for all sites you configure with the value of the cacheHtml property as true. The publish:end event handler is configured like this in a standard installation:

    <event name="publish:end">
          <handler type="Sitecore.Publishing.SmartHtmlCacheClearer, Sitecore.Kernel" method="ClearCache" resolve="true" />
          ...
        </event>

    If you do not want the cache for a site to be cleared when you publish you can add the preventHtmlCacheClear attribute to the site definition like this:

    <site name="custom_website" cacheHtml="true" preventHtmlCacheClear="true"  />

Personalization and HTML caching

Important

Do not enable HTML caching on renderings that use personalization rules. Combining personalization and HTML caching is not supported in Sitecore XP 9.2 and later.

When HTML caching is enabled for a rendering, Sitecore stores the final rendered markup and reuses it for subsequent requests. Because the cached output is served directly, personalization rules are not re-evaluated per request, which causes inconsistent or incorrect personalization behavior in production.

None of the caching options described in this topic make HTML caching compatible with personalization rules. Configuring any combination of these options does not restore per-request evaluation of personalization conditions.

We recommend that you disable HTML caching for any rendering that uses personalization rules. If the same rendering is used on multiple pages and only some of those pages use personalization, you can disable caching at the page level for the affected pages rather than disabling it globally at the rendering definition level. This allows the rendering to remain cached on pages where no personalization is applied.

To disable caching for a specific rendering instance on a page, open the page in the Experience Editor, select the rendering, and clear the Cacheable check box in the rendering's caching settings.

If you have suggestions for improving this article, let us know!