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:
controlling cache through the constructor, my block derives from
\Magento\Framework\View\Element\Templateand setting up cache like this :$this->addData(array('cache_lifetime' => null));didn't work.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>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– Dhiren Vasoya2018年07月31日 10:45:22 +00:00Commented Jul 31, 2018 at 10:45
-
Each time I am altering my code I am doing bin/magento cache:clean just to be sure.Mister Paszczakowski– Mister Paszczakowski2018年07月31日 10:55:08 +00:00Commented Jul 31, 2018 at 10:55
2 Answers 2
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.
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.Mister Paszczakowski– Mister Paszczakowski2018年07月31日 11:27:54 +00:00Commented Jul 31, 2018 at 11:27