0

I have created a new block to sit on the product detail page which contains dynamic content (the customer's recently-viewed products) that should not be cached. How can I switch off caching for that particular block?

I've tried the method mentioned in How to disable the cache for the block in Magento 2?, setting _isScopePrivate to false. That doesn't work, the block is still cached.

I have also tried disabling the cache for the whole page, which is not what I want but thought I'd see what happened. The page is still cached.

I have cleared out all folders under var and pub/static as well as running cache:clean. I have checked and the generated XML file definitely has cacheable="false" on the new block. But the page is still cached. This makes me think there is something else I'm missing.

3 Answers 3

3

You can't disable single block cache like in magento 1,

Magento recommends handling customer related data via ajax.

So you need to create an api endpoint or section data and use javascript to retrieve the data and load it on front :)

https://amasty.com/blog/how-to-use-sections-in-magento-2/

1

There are some ways to do this task :

  1. use cacheable="false" but it's not recommended because it will disable full page cache so it's bad for performance

2 try this

    protected function _construct()
{
    $this->addData(
        [
            'cache_lifetime' => false,
            'cache_tags' => ['Rokanthemes_Themeoption', \Magento\Store\Model\Group::CACHE_TAG],
        ]
    );
}

3

    public function getCacheLifetime()
{
    return false;
}
-3

You can just use cacheable="false" for your block to disable cache for that block. This is magento 2 core strucuture to disable specific block cache. you can try in your block xml file.

core ex.

<block class="Magento\Checkout\Block\Cart" name="checkout.cart" template="cart.phtml" cacheable="false"></block>
6
  • 2
    Thanks for taking the time to answer, however as I said in my question there are two problems with this. 1) It doesn't work. 2) It would not be a viable solution since (as far as I know) it disables the cache for the whole page, rather than just the block.
    – piemanji
    Jun 13, 2016 at 10:26
  • have you check with cacheable=false in your block Jun 13, 2016 at 10:37
  • This is what I have in my layout XML file: <block class="Namespace\Module\Block\Product\View\RecentlyViewed" name="product.info.recentlyviewed" template="Namespace_Module::product/view/recentlyviewed.phtml" before="-" cacheable="false" />
    – piemanji
    Jun 13, 2016 at 10:38
  • Basically I want to disable caching for my home_cms page, how would I do that?
    – Max
    Oct 19, 2016 at 8:57
  • Be aware that this will disable full page cache for any page that your block appears on. See here: devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/…
    – Wildcard27
    Oct 26, 2017 at 5:53

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.