3

I am trying to detect user’s country based on their IP address and ability to change country as well. However with the use of Full Page Cache, country name is not getting display properly. Sometimes it displays random results.

I have tried to add cacheable=false to block XML file. But adding this would slow down entire page. Since this block is added to all pages, entire site getting slow by adding cacheable=false.

Also tried _isScopeprivate = true to my block PHP __construct() function. But this is also not working.

Is there any way to exclude specific block from FPC without hurting site speed?

I have rewrite currency.phtml template for customize my goal,

<referenceContainer name="header.panel">
            <block class="Vendor\Module\Block\Currency" name="currency" template="Vendor_Module::currency.phtml" cacheable="false"/>
</referenceContainer>

when i am keep cacheable=false in above block its working but page load time take so much time. So I want to remove cacheable="false" from above block.

Any suggestion would be appreciated. Thanks.

  • Good question, How are you redirecting and fetching country based on Ip. Could you please elaborate your exact problem – Amit Singh May 18 '17 at 13:44
  • @Rakesh did you get some solution for this? – Mohammad Mujassam Aug 17 '17 at 16:05
  • @Rakesh and Mohammad I am facing same issue in Magento 2.1.10 . Did you get any solution for this ? – Hardik Visa Dec 28 '17 at 10:44
4

cacheable=false on a block makes whole page not cacheable. This is how it is designed, so no problem here.

As for your goal - you want to render user-specific content as part of cached page. Such content should not be rendered on server. It should be retrieved from server using customer data JS api (see mini shopping cart), and rendered using JS.

Alternatively you can redirect user to proper country page based and not care about it on magento side.

  • thanks for your reply, please check my updated questions. – Rakesh Jesadiya Sep 2 '16 at 10:28
  • when enable cacheable=false in currency block, page load time is doubled. so i want to find new solutions using cacheble=true. so page load time getting fast. – Rakesh Jesadiya Sep 2 '16 at 10:37
2

try to extend Vendor\Module\Block\Currency and add:

public function getCacheLifetime()
    {
        return null;
    }

to this class.

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.