TransWikia.com

Magento 2 EE : Need to remove one single block from cache

Magento Asked by Camit1dk on January 30, 2021

I just need to remove single block phtml from FPC cache. How can I do that without using cacheable = "false". Because cacheable = "false" has an effect on the whole page.

I am using a .phtml file in the static block and that static block call into header.

5 Answers

make your most of the code in javascript

even if you are getting some array objects from php give it to javascript variable then put your logic in javascript, append prepend the html with javascript . if you can provide some snippet code from .phtml file which was late you think for disabling cache, it will be helpfull. i did same thing in my phtml file, where caching issue was very problematic.

Answered by Ganesh Godshelwar on January 30, 2021

You can achieve this by two methods.

1) You have to implement a hole punching functionality for a specific block.
-> I read this article about creating a hole punching by using identities https://trellis.co/blog/magento2-fpc-hole-punching/ so you can check that link, it might be useful or you can buy any third party extension amasty fpc,mirasvit fpc.

2) Other solution, you can display that particular content part by ajax after the whole page load or during.

Answered by Prakash Patel on January 30, 2021

Simple insert param cacheable in the block:

<block class="BlockClass" name="blockname" cacheable="false" />

Answered by Minh on January 30, 2021

The best approach is to handle the block content via javascript.

Depending on what you want to achieve you can use

Public Content

or

Private Content

You can get more information on http://devdocs.magento.com/guides/v2.0/extension-dev-guide/cache/page-caching.html

Feel free to add any related questions in the comments.

Answered by Thiago Lima on January 30, 2021

You can create a preference or a plugin on the block class to override the block cache functions: getCacheLifetime(), hasCacheLifetime(), get/hasCacheTags(), has/getCacheKeyInfo(), getData().

If getCacheLifetime returns null than it will disable current block's cache.

Take a look at the block class to see which cache methods it uses and search in parent block classes (Abstract, Template, ..) to see which function you need to override in your plugin/preference class.

cacheable = "false" has only one meaning. It will disable full page cache type on that page type / action.

If there is something wrong with the parameters that form cache key info, than you can vary block cache by overriding getCacheKeyInfo function.

If there is customer specific data inside that block than you can't use either block cache or have it in full page cache. The only viable solution is to use AJAX. Magento 2 has private content: http://devdocs.magento.com/guides/v2.3/extension-dev-guide/cache/page-caching/private-content.html

Other solution is to use AJAX requests to fill parts of pages like in product page reviews and other. https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Review/view/frontend/web/js/process-reviews.js

The last and the least is the XML layout declaration ttl="3600". When Magento 2 is configured to work with Varnish, it will create am ESI policy for the output of the block. Something similar does when it uses builtin page cache instead of Varnish. Magento uses this type of cache hole punching in one place: in top menu. I personally don't recommend using "ttl" cache hole punching because it adds an extra request to the server. If you use it too many times on a page than it may decrease performance.

Answered by Daniel Ifrim on January 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP