Magento – Disable page cache for a single block

cachingcustom blockfull-page-cachemagento-2.0.7magento2

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.

Best Answer

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/

Related Topic