0

I have a custom block i want only that block's cache to be cleared

I tried 2 methods

  1. set cachable = false in layout file for my block (This clears full page cache)
  2. in my block I added a method
public function getCacheLifetime()
{
    return false;
}

This also clears the cache of all the blocks available for that layout (i.e clears full page cache).

Is there any way to just make my block uncacheable?

  • There is no concept of hole punching in Magento 2. You can use AJAX to append the block dynamically to the DOM or use knockout JS like in welcome text and mini-cart. – Lokesh Naik Aug 18 '20 at 11:17
0

If you want to make just some part more dynamic you should use javascript. You can do it using AJAX and POST request or GET request with timestamp to your controller to retrieve information and update UI using vanilia JS, jquery or knockoutJS. You can also use underscore templates to prepare your html (this way you can create reusable templates with placeholder for data).

You can also use Magento2 "customer-data" JS library so your block "section" can be updated automatically depending on specific ajax call. For example your section is displaying some data from cart or related to customer. If some data related to those section will change ex. user will log in or add product to cart then UI will be update automatically.

More information about how "customer-data" and sections works: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/cache/page-caching/private-content.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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