1

I am trying to disable cache for specific block,

Have used below code.

app\code\Vendor\Module\view\frontend\layout\default.xml

<referenceContainer name="header.panel">            
        <block class="Vendor\Module\Block\Index" name="custom_block" template="custom.phtml"/>
</referenceContainer>

app\code\Vendor\Module\Block\Index.php

<?php

 namespace Vendor\Module\Block;

 class Index extends \Magento\Framework\View\Element\Template
 {
   public function __construct(
     \Magento\Framework\View\Element\Template\Context $context
      array $data = []
      ) {
        parent::__construct($context, $data);
        $this->_isScopePrivate = false;
     }


public function customHtml()
{
  $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        if (curl_exec($ch)) {               
            $contents = curl_exec($ch);
            return $contents;
        }
 }
   public function getCacheLifetime()
    { 
       return null;
    }
 }

app\code\Vendor\Module\view\frontend/templates/custom.phtml

<?php echo $block->customHtml(); ?>

Used getCacheLifetime and _isScopePrivate but not working. Please someone guide me on this.

Note: cacheable="false" is not recommended

Thanks in advance!!

2
  • If you disable a single block caching, this will disable the whole page caching. Since you're adding the block in default.xml layout file, I think you should avoid doing this. Instead of disabling the block caching, you'd better use private content. You can find more info here: devdocs.magento.com/guides/v2.4/extension-dev-guide/cache/… Nov 15, 2021 at 17:09
  • @MagestyApps, I gone through the above link which is not so clear and I didn't get the solution, Can you please update me as answer for my issue please...
    – Manjunath
    Nov 15, 2021 at 17:14

1 Answer 1

0

First of all create a di.xml inside app\code\Vendor\Module\etc\frontend\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\CustomerData\SectionPoolInterface">
        <arguments>
            <argument name="sectionSourceMap" xsi:type="array">
                <item name="add_custom_name_here" xsi:type="string">Vendor\Module\Block\Index</item>
            </argument>
        </arguments>
    </type>
</config>

Inside item name="add_any_unique_custom_name" and assign a block class for which you want to disable cache Vendor\Module\Block\Index

Now create a block and return a function in which you want to disable cache

    <?php
    namespace Vendor\Module\Block;
    
    use Magento\Customer\CustomerData\SectionSourceInterface;
    
    class Index implements SectionSourceInterface
    {
        /**
         * @return string
         */
        public function getSectionData()
        {
            $data = ['profile' => 'abc' ]; //get dynamic data here instead of 'abc'
            return $data;
        }
    }

Create default.xml inside view/frontend/layout/default.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="before.body.end">
            <block class="Magento\Framework\View\Element\Template" name="Vendor.PrivateContent"
                   template="Vendor_Moduel::private.phtml">
                <arguments>
                    <argument name="jsLayout" xsi:type="array">
                        <item name="components" xsi:type="array">
                            <item name="add_custom_name_here" xsi:type="array">
                                <item name="component" xsi:type="string">Vendor_Module/js/custom_section</item>
                            </item>
                        </item>
                    </argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

Next step is to create a custom_section.js file inside view/frontend/web/js/custom_section.js

define([
    'uiComponent',
    'Magento_Customer/js/customer-data'
], function (Component, customerData) {
    'use strict';

    return Component.extend({
        initialize: function () {
            this._super();
            this.add_custom_name_here = customerData.get('add_custom_name_here');
        }
    });
});

Now inside your any phtml file call this script

<div data-role="custom-section" data-bind="scope: 'add_custom_name_here'">
    <span class="content-type" data-bind="text: custom_section().profile"></span>
</div>    
<script type="text/x-magento-init">
    {
        "[data-role=custom-section]": {
            "Magento_Ui/js/core/app": <?php /* @escapeNotVerified */ echo $block->getJsLayout(); ?>
        }
    }
    </script>
5
  • Thank you for the detailed answer, still I have some questions, what should i replace in the block function getSectionData().. At present there are many functions are created in my block file, Need some assistance on that.
    – Manjunath
    Nov 16, 2021 at 5:40
  • The data for which you want to disable cache and update on every refresh should be return inside $data = ['profile' => 'abc' ]; instead of 'abc' you can get data here dynamically like $data = ['profile' => $update_without_cache ];
    – Arun Kumar
    Nov 16, 2021 at 5:45
  • Sorry if I am asking silly, Still not cleared to me, I have many functions defined in the block, what is the content here , get data here dynamically like $data = ['profile' => $update_without_cache ];
    – Manjunath
    Nov 16, 2021 at 5:48
  • I updated the Block code, can you please help me with the same code.
    – Manjunath
    Nov 16, 2021 at 7:30
  • I followed the answer you given, but still the page is getting cached, can we check is there any other pages caching and remove it?
    – Manjunath
    Nov 16, 2021 at 12:03

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.