Caching Experience Fragments

Published
Experience Fragments (XF) often face cache invalidation issues, where modifying and publishing an XF does not update the pages referencing it because the XF content is cached within each individual page rather than a shared repository. As a result, authors have to manually activate or invalidate each page to see the updated XF content, making the process tedious and impractical.
Experience Fragments can be added into pages using two ways: embedding as part of page template or inserting specific variation through the experience fragment component. In both scenarios, XF can be cached using Sling Dynamic Include, ensuring they are not part of the page. Therefore, whenever authors publish any XF changes, cache files will be invalidated and updated in the subsequent request. This approach eliminates the need to publish each page individually, as updating the XF will ensure the latest content is displayed.
This article outlines setup process for SDI to serve dynamic content. With slight adjustments to the OSGi configuration, we can ensure that XF is cached in the dispatcher.
config.publish / org.apache.sling.dynamicinclude.Configuration~aem-demo.cfg.json
{ "include-filter.config.enabled": true, "include-filter.config.appendSuffix": true, "include-filter.config.add_comment": true, "include-filter.config.disableIgnoreUrlParams": false, "include-filter.config.extension": "html", "include-filter.config.ignoreUrlParams": [""], "include-filter.config.include-type": "SSI", "include-filter.config.resource-types": [ "aem-demo/components/xfpage" ], "include-filter.config.rewrite": false, "include-filter.config.path": "/content/experience-fragments/aem-demo", "include-filter.config.selector": "conf", "include-filter.config.required_header": "Server-Agent=Communique-Dispatcher", "include-filter.config.ttl": "" }
Once SDI is set up and the vhost file is configured correctly as described in the article, you should observe SDI comments and include tags in the dispatcher cache file. This ensures that the contents of experience fragments are not included in the pages and are instead served from /content/experience-fragments/aem-demo.
Experience Fragments Cache
It is crucial to avoid adding rules that disable caching, as this would prevent the XF contents from being cached, resulting in responses being fetched from the publisher for every request.