How to Disable Cache for Block Using XML in Magento 2
The Magento 2 caching is important for page load speed. Nevertheless, it causes trouble with the customizations that require the dynamic content output.
Not all the time can you go for the caching, based on your business requirements. But there’s a way out. You can disable cache for particular blocks only.
This way, you can disable cache for block using XML in Magento 2. For example, you have implemented a block that behaves differently for the desktop and mobile. If the user accesses it from desktop and then from mobile, he should see the difference which can be possible only if it is disabled to fetch the value from the cached version.
To implement such functionalities where you want Magento 2 to not fetch the cached version, use the solution below!
Method to Disable Cache for Block Using XML in Magento 2:
Add this cacheable=”false” into the XML file like:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<block class="Block\Class" name="blockname" cacheable="false"/> for example : [Vendor]\[Module]\view\frontend\layout\checkout_cart_index.xml <?xml version="1.0"?> <page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <update handle="checkout_cart_item_renderers"/> <body> <referenceContainer name="content"> <block class="[Vendor]\[Module]\Block\Custom" template="[Vendor]_[Module]::custom.phtml" before="-" name="custom_index" cacheable="false"/> </referenceContainer> </body> </page> |
That’s it.
Use this method when you want to disable the cache for a particular block.
Any doubts on the topic can be mentioned in the Comments section below and I’d be glad to help.
Please feel free to share the solution with fellow developers via social media.
Thanks.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
6 Comments
Hi,
when i am use cacheable=false in the xml, it’s working fine but in marketplace it’s not allow because of varnish test and when i am remove it, getting an error.
https://nimb.ws/b5rEmI
https://nimb.ws/CdqJeF – i ‘ve removed cacheable false
thanks
Hello Dhruvil,
Alternatively, you can set _isScopePrivate property to true in a block class
For reference:- Check \Magento\Captcha\Block\Captcha::__construct and related layouts/templates.
Thank You
This is not “Method to Disable Cache for Block Using XML in Magento 2”
This is “Method to Disable Cache for ALL PAGE which has Block with cacheable=”false” in Magento 2”
Hello Amigo,
If you add cacheable=”false” in default.xml file then it disables cache for all pages
if you add the same in particular page like cms_index_index.xml , catalog_product_view.xml then it just disable particular page instead of all pages.
Thank You
Hi Sanjay,
Nice blog but as fas as I know by adding Cacheable=”false” to any block in layout makes the entire page non-cacheable. How Can I make a particular block non-cacheable without affecting FPC in magento 2
Hello Vikas,
Glad you liked the blog.
You cannot make a particular cacheable block. And yes, if you implement cacheable=”false”, it will make the product page non-cacheable.
Thank you.