Magento2: Making CMS Pages Non-Cacheable
When working with Magento 2, it’s common to use templates for displaying dynamic data on CMS pages. However, the built-in caching mechanisms can sometimes interfere with your intentions of showing fresh content every time a page is loaded. In this post, we’ll explore how to make sure that your custom CMS template pulls the latest data without relying on cached content.
Understanding Magento 2 Caching
Magento 2 uses a layered approach to caching for performance optimization. This includes built-in full page cache (FPC) and block-level cache. The default behavior is to store a fully rendered version of each CMS page in cache, which can lead to stale data being displayed if your templates rely on real-time data.
Your Current Setup
You’ve already added a custom block to your CMS template, which is a great start:
{{block class="Test\PointHistory\Block\Index" template="Test_PointHistory::index.phtml"}}
This tells Magento 2 to render the Test\PointHistory\Block\Index block using the specified template. However, because this is part of a CMS page by default, it’s likely being cached as well.
Making Your Template Non-Cacheable
To ensure that your custom template always displays fresh data, you need to disable caching for the specific layout used by your CMS page. Here are the steps:
1. Identify the Layout Handle
First, you need to determine which layout handle is being used for your CMS page. You can find this in the core_layout table in your database.
SELECT * FROM core_layout WHERE entity_id = <Your_CMS_Page_ID>;
Replace <Your_CMS_Page_ID> with the actual ID of your CMS page.
2. Add a Custom Layout Update
Next, you need to create a custom layout update XML file that disables caching for this specific handle. Create an XML file (e.g., cms_nocache.xml) at the following path:
app/design/frontend/<Vendor>/<Theme>/layout
The contents of the file should look like this:
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_schema.xsd">
<update handle="my_cms_handle"> <!-- Replace with the actual layout handle you found in step 1 -->
<cacheable action="disable" />
</update>
</layout>
3. Apply the Layout Update
Finally, apply your custom layout update in Magento 2. You can do this via the admin interface or directly by modifying the core_layout_link table.
If you’re using the admin interface:
- Go to Stores > Configuration.
- Select your website and scope (usually Default Config).
- Navigate to Advanced > Developer.
- Expand the “Layout” section.
- Find the “Custom Layout Update XML” field, enter the path to your XML file (e.g.,
layout/cms_nocache.xml). - Click “Save Config”.
If you’re modifying the database directly:
INSERT INTO core_layout_link (layout_id, entity_type, entity_code, layout_update_xml)
VALUES (<Your_Layout_ID>, 'cms_page', <Your_CMS_Page_ID>, '<your-layout-update-xml-goes-here>');
Verifying the Changes
After applying these changes, Magento 2 will no longer cache your specific CMS page. To test this:
- Clear all cached data using a command like
php bin/magento cache:clean. - Visit your CMS page.
- Refresh the page or load it multiple times.
You should notice that the content of your custom template changes accordingly each time, indicating that Magento 2 is no longer serving cached versions of the page.
Remember to always consider the trade-offs between performance and real-time data when making these adjustments. In most cases, a balance can be struck by caching less frequently updated content while allowing dynamic elements to remain uncached.
For more detailed information on Magento 2 caching strategies and best practices, I recommend checking out our comprehensive guide at Laravel Company’s Magento Development Resources.