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 problemAmit Singh– Amit Singh2017年05月18日 13:44:02 +00:00Commented May 18, 2017 at 13:44
-
@Rakesh did you get some solution for this?Mohammad Mujassam– Mohammad Mujassam2017年08月17日 16:05:59 +00:00Commented 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 ?Hardik Visa– Hardik Visa2017年12月28日 10:44:05 +00:00Commented Dec 28, 2017 at 10:44
-
did you find any solution?Mohit Rane– Mohit Rane2020年04月15日 05:35:07 +00:00Commented Apr 15, 2020 at 5:35
-
@rakesh, Did you get any solution?Pushpendra Singh– Pushpendra Singh2020年05月11日 16:42:59 +00:00Commented May 11, 2020 at 16:42
2 Answers 2
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– Rakesh Jesadiya2016年09月02日 10:28:58 +00:00Commented 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.Rakesh Jesadiya– Rakesh Jesadiya2016年09月02日 10:37:11 +00:00Commented Sep 2, 2016 at 10:37
-
@RakeshJesadiya Did you find any solution for this. I'm also facing the same issue.Rahul pandey– Rahul pandey2020年12月16日 09:04:19 +00:00Commented Dec 16, 2020 at 9:04
try to extend Vendor\Module\Block\Currency and add:
public function getCacheLifetime()
{
return null;
}
to this class.
-
this solution is not working in magento 2.4.5Jigs Parmar– Jigs Parmar2022年11月08日 13:33:54 +00:00Commented Nov 8, 2022 at 13:33
Explore related questions
See similar questions with these tags.