2

I am developing one widget in which I want current customer details. But I am not able to get these details on the homepage, catalog page, product page and other CMS pages.

I have found for this issue and it's because of Full Page Cache. I don't want to disable cache for the full page since it's a widget that customer can add it on any page and disable cache will affect website performance.

I just want to know if there is any solution through which I can keep the page cache untouched and get the customer details in my widget.

If there is any solution please let me know.

1

Becareful, do not use cacheable="false" because all your page will not be cachable by the FPC and so your store front will have performance issues.

As mention, try to use Ajax request and localstorage in order to load you data dynammicaly.

You can also try to use some Varnish ESI tags.

Official doc : http://devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/page-caching.html

To sum up, follow this guide : http://devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/page-caching/private-content.html

| improve this answer | |
0

Modify Layout

You can disable cache for a block via xml but i think full page cache will get disabled on pages where this block exists.

<block class="Vendor\Module\Block\CustomBlock" name="custom.block" template="test.phtml" cacheable="false"/>

OR

Modify Block Code

If you add the below code to your block class it should disable the static block caching for that block.

public function getCacheLifetime()
{
    return null;
}

OR

Modify Construct

Adding this to the construct should also disable block from static block cache.

protected function _construct()
{
    $this->addData(
        [
            'cache_lifetime' => false,
            'cache_tags' => array('CUSTOM_BLOCK'),
        ]
    );
}

public function getCacheKeyInfo()
{
    return [];
}

Hole Punching with isScopePrivate

I'm not sure weather the last 2 options disable the blocks from the FPC however and some hole punching for this block may be needed. I read through here: https://inviqa.com/blog/how-full-page-cache-works-magento-2 which seems to suggest adding below code to construct:

public function __construct(Context $context) {
    parent::__construct($context);
    $this->_isScopePrivate = true;
}

Setting isScopePrivate to true will create tags around the block and exclude from the fpc. This content is then automatically loaded over ajax from local storage.

| improve this answer | |
  • last two options are not working. – Gaurav Agrawal Sep 21 '17 at 8:16
  • May need to be holepunched? Check the isScopePrivate section – harri Sep 21 '17 at 8:18
  • isScopePrivate is now deprecated. I can't use it because it can cause issue to any of the client who will use this widget. – Gaurav Agrawal Sep 21 '17 at 8:33
0

This is an old post, I thought my answer might help someone. For widget caching in M2 we can set a parameter of cache_lifetime. Something like this.

<parameter name="cache_lifetime" xsi:type="text" visible="true" sort_order="40">
    <label>Cache Lifetime (Seconds)</label>
    <description>86400 by default, if not set. To refresh instantly, clear the Blocks HTML Output cache.</description>
</parameter>

This will allow user to set cache lifetime from admin. 0 = no cache, 1 = 1 second cache etc.

| improve this answer | |

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.