0

In my module, I am adding small snippet to every page on site with layout file, that I have attached below. The thing is, I would like to disable caching for my module. How do I do that without disabling FPC for whole page? Using cacheable="false" will make whole page non-cached. So this option won't work for me.

I have already tried following:

  1. controlling cache through the constructor, my block derives from \Magento\Framework\View\Element\Template and setting up cache like this : $this->addData(array('cache_lifetime' => null)); didn't work.

  2. adding parameters to my block in layout file:

    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
            <body>
                <referenceContainer name="content">
                    <block class="Something\Something\Block\SnippetBlock" name="snippetblock" template="snippetblock.phtml"> 
                     <arguments>
                                        <argument name="cache_lifetime" xsi:type="null">null</argument>
                              </arguments>
                </block>
                </referenceContainer>
            </body>
    

  3. overwriting getCacheLifetime() method to return null. And this is my layout file that I am using now:

    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Something\Something\Block\SnippetBlock" name="snippetblock" template="snippetblock.phtml" />
        </referenceContainer>
    </body>
    

I will gladly listen to any suggestion.

  • Have you try to caching magento, and then check? – Dhiren Vasoya Jul 31 '18 at 10:45
  • Each time I am altering my code I am doing bin/magento cache:clean just to be sure. – Michał Paszowski Jul 31 '18 at 10:55
0

In Magento2, You have to render the content via JS (Ajax).
Check how the mini-cart & login/logout section has been implemented.

Note: There is no concept like hole-punching like in Magento 1.

0

Please use this code for your construct method.

public function _construct(
    \Magento\Customer\Model\Session $session
) {
    $this->_isScopePrivate = true;
    $this->customerSession = $session;
}
  • It didn't work for me unfortunately. Maybe I am testing it in wrong way: 1 when page cache is disabled i can modify manually code of my snippet refresh page and see the changes. 2 when page cache is enabled I am modyfing code refresh page but changes do not appear until I flush the cache. I would expect that if one of above methods for disabling cache would work the effect would be the same as in point 1. – Michał Paszowski Jul 31 '18 at 11:27

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.