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. To ensure that XF is cached in the dispatcher, add the following SDI configuration on the Publisher instance.
config.publish / org.apache.sling.dynamicinclude.Configuration~aem-demo-xf.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": "" }
Now, you should observe SDI comments and include tags in the dispatcher cache file where experience fragments are added to the page. Additionally, you can verify that /content/experience-fragments/aem-demo is included in the dispatcher cache. With this setup, any changes published in XF will automatically update the pages, ensuring end users see the latest content without additional hiccup from the author.
Be careful not to add 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.