4

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.

7
  • Good question, How are you redirecting and fetching country based on Ip. Could you please elaborate your exact problem
    – Amit Singh
    Commented May 18, 2017 at 13:44
  • @Rakesh did you get some solution for this? Commented Aug 17, 2017 at 16:05
  • @Rakesh and Mohammad I am facing same issue in Magento 2.1.10 . Did you get any solution for this ? Commented Dec 28, 2017 at 10:44
  • did you find any solution?
    – Mohit Rane
    Commented Apr 15, 2020 at 5:35
  • @rakesh, Did you get any solution? Commented May 11, 2020 at 16:42

2 Answers 2

5

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.

3
  • thanks for your reply, please check my updated questions. Commented Sep 2, 2016 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. Commented Sep 2, 2016 at 10:37
  • @RakeshJesadiya Did you find any solution for this. I'm also facing the same issue. Commented Dec 16, 2020 at 9:04
2

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

public function getCacheLifetime()
    {
        return null;
    }

to this class.

1
  • this solution is not working in magento 2.4.5 Commented Nov 8, 2022 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.