4

I used the below code to set cacheable to false, but it does not seem to work. How can I change a block to not be cacheable?

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <referenceBlock name="category.products.list">
        <arguments>
             <argument name="cacheable" xsi:type="string">false</argument>
        </arguments>
    </referenceBlock>
</layout>

5 Answers 5

3

Magento2 do not have "not cacheable" blocks. If you set cacheable to false than all page with this block will be not cacheable.

If content of your block is user specific you should mark block as $_isScopePrivate = true. and block will be deliver over Ajax.

If content is not user specific you need render it by JS.

2
  • 1
    I found that setting $_isScopePrivate = true like the block in Magento_Captcha module cannot make the block be delivered over ajax. Could you give me more details about how to make user specific content not to be cached by full page cache? Thanks
    – Ricky.C
    May 30, 2016 at 9:41
  • 1
    This answer is treating the original question like an XY problem. Setting cacheable="false" on a block is indeed how you make the block "not cacheable", as the OP asks: the fact that doing so also makes the whole page uncacheable is secondary. This answer doesn't address the meat of the question, which is how to do this on a <referenceBlock>. Sep 18, 2017 at 22:45
2

The reason this does not work is that the <arguments> XML structure defines data to be passed to the block class, but the cacheable property is handled solely by the layout class. When cacheable is added as an <argument>, the block class just ignores it, since it has no idea what to do with it.

In order to set a block as not cacheable (so as to prevent a page containing it from being cached) from a layout XML file where you can only reference it, your best option is to create a dummy child block that serves simply to block caching:

<referenceBlock name="category.products.list">
     <block class="Magento\Framework\View\Element\Text" cacheable="false" /> 
</referenceBlock>
1

I was having the same error when trying to use:

<?php 
    $helper = $this->helper('Magento\Checkout\Helper\Cart');
    echo $helper->getItemsCount();
?>

this was returning 0 all the time in my minicart.phtml file.

After changing to cacheable="false", it start working

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="minicart" >
            <block class="Magento\Framework\View\Element\Text" cacheable="false" />
        </referenceBlock>
    </body>
</page>
0

Change your code

<argument name="cacheable" xsi:type="string">false</argument>

to:

<argument name="cacheable" xsi:type="boolean">false</argument>

Hope it will helpful to you.

1
  • Did this work? Because I'm facing the same problem and the above doesn't seem to do the trick... Jan 30, 2017 at 16:07
0

My impression was that anything with cache_lifetime lte 0 will not be cached by block cache, without disabling FPC for the whole page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.