0

I'm exploring ways to improve our Google Pagespeed score and, ironically, I've found that the biggest negative impact on our score is from Google reCAPTCHA. I'm using the default Magento 2.3.0 reCAPTCHA implementation but I've found that this loads the reCAPTCHA js and css on EVERY page - not just pages with forms where protection is needed. Is this the expected behaviour (or do I have a bug) and is there some way to configure Google reCAPTCHA to only load for the specified forms?

I'm using invisible reCAPTCA version 3.

Is this issue resolved in later Magento versions ?(i.e. 2.4.*) I'm already considering an upgrade for other reasons.

Thanks for any help...

3 Answers 3

2

Without knowing the specific files that are flagged by PageSpeed, I can't say for sure whether the following addresses your question.

In 2.4.x, if reCAPTCHA is configured for customer login, reCAPTCHA elements are required on every page to facilitate the authentication popup that is displayed when guests click the "Proceed to Checkout" button from the mini-cart (though this may depend on checkout configurations). This behavior is configured in vendor/magento/module-re-captcha-customer/view/frontend/layout/default.xml.

In 2.3.x (which I can't check directly) vendor/msp/recaptcha/view/frontend/layout/default.xml indicates that reCAPTCHA elements will load on every for the same reason or if it is enabled for newsletter signups.

2
  • Thank you for the info, although not the best news. Does this mean that every Magento 2 store is at a disadvantage because of the reCAPTCHA code? It seems like we're stuck with this issue - unless we disable reCAPTCHA - which is unthinkable nowadays...
    – Sparko
    Commented Jun 6, 2021 at 19:07
  • This suggests that any Magento 2 site using the default reCAPTCHA implementation would be similarly dinged by PageSpeed. It is probably possible to customize your site to avoid it, e.g. by removing the option to go from the mini-cart directly to checkout, or by sending them to the login page and automatically redirecting them to checkout: magento.stackexchange.com/questions/337887/…. If the authentication-popup isn't used, the configuration can be removed from default.xml.
    – jiheison
    Commented Jun 6, 2021 at 21:42
2

This worked for me for M2.3.x:

app/design/frontend/VENDOR/THEMENAME/Magento_ReCaptchaCustomer/layout/default.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
 -->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">

    <body>
        <referenceBlock name="authentication-popup" remove="true"/>
    </body>
</page>
2
  • I'm about to upgrade to 2.4, hopefully this will resolve a lot of performance issues we've found with 2.3 but I expect I'll have a different set of problems to deal with. If the reCaptcha issue persists I'll give your solution a try. Thank you.
    – Sparko
    Commented Feb 15, 2022 at 13:35
  • This is definitely a bug. Condition inside that script is useless and always downloads captcha loading which drastically affects the Lighthouse score.
    – MTP
    Commented Sep 28, 2022 at 8:38
0

Check out this article: https://dev.to/uf4no/improve-page-performance-lazy-loading-recaptcha-442o

You might be able to adapt this approach. I will be looking into getting this done and will post back later when I have some results.

Magento loads ReCaptcha in this file:

vendor\magento\module-re-captcha-frontend-ui\view\frontend\web\js\reCaptchaScriptLoader.js

So thats where we should start looking to customize.

3
  • Looks ideal. Somewhat unbelievably, I'm still waiting for our dev to complete the update to 2.4 (!) I expect this will be the first performance mod we'll need to make after updating.... Thanks for the info.
    – Sparko
    Commented Jul 25, 2022 at 8:52
  • @Sparko well, if you got 4 forms with ReCaptcha, it will remove about 500kb of JS being loaded in on every page. I dont think it will have a massive effect on the pagespeeds (there are a lot of other issues just in general).
    – PaulM
    Commented Jul 25, 2022 at 19:32
  • So did you ever get this to work properly ? Commented Jun 8, 2023 at 5:43

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.