Skip to content

Load recaptcha related files on focus of the form fields. - #6

Merged
rhoerr merged 2 commits into
mage-os:mainfrom
Bashev:load-recaptcha-on-focus
Feb 27, 2026
Merged

Load recaptcha related files on focus of the form fields.#6
rhoerr merged 2 commits into
mage-os:mainfrom
Bashev:load-recaptcha-on-focus

Conversation

@Bashev

@Bashev Bashev commented Jan 24, 2026

Copy link
Copy Markdown
Contributor

Original PR magento/security-package/pull/334

Description (*)

reCaptcha remote files will be loaded only if customer (visitor) focus on the fields for the form for which reCaptcha is enabled. This will reduce loaded files and immediately reflects to loading time.

Fixed Issues (if relevant)

Fixes magento/security-package#333
Fixes magento/magento2#38303

Manual testing scenarios (*)

Enable reCaptcha (no matter which version) for Newsletter, Contact Form, Review, Registration or ... anything.
Load the page which will contains form for which reCaptcha is enabled (Ex. Contact form /contact)
ReCaptcha external (gstatic.com) files will not be loaded (you will not see the logo of the recaptcha or loaded files in the network tab of the browser developer inspector).
Click on the first or random field (Ex. Name)
Recaptcha files will be loaded and you will see logo of the repCaptcha somewhere on the page as you are configured in the step 1.

Questions or comments

Contribution checklist (*)

  • Author has signed the Adobe CLA
  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

@Bashev
Bashev requested a review from a team as a code owner January 24, 2026 15:40
Comment thread ReCaptchaFrontendUi/view/frontend/web/js/reCaptcha.js Outdated
@rhoerr

rhoerr commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

Thanks for submitting this. Just saw it now. We can merge once the trailing spaces are fixed.

@rhoerr rhoerr self-assigned this Feb 27, 2026

@rhoerr rhoerr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@rhoerr
rhoerr merged commit a3be300 into mage-os:main Feb 27, 2026
1 check passed
@rhoerr

rhoerr commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

Per https://discord.com/channels/893449664093904936/1483402749419847803, this changeset breaks recaptcha for iframes, which never see focus (for instance, Braintree's hosted payment fields). We will have to either improve it (Vinai suggested loading on idle) or revert.

We'll decide and make a release in the very near future as this is a checkout-breaking issue for those it affects.

rhoerr added a commit that referenced this pull request Mar 18, 2026
The focus-based deferral from #6 ran in initialize() before KnockoutJS
rendered the template, so the DOM query for the parent form always
returned empty — the focus listener never attached. Additionally,
iframe-embedded payment fields (e.g. Braintree hosted fields) never
fire focus events on the parent form, so reCAPTCHA never loaded for
those checkout flows.

Move the deferred load into renderReCaptcha() (called from KO
afterRender) where the DOM actually exists:

- Focus on any form field triggers an eager API load
- requestIdleCallback (capped at 3s) or setTimeout(1s) fallback
  ensures the API loads for iframes and no-focus scenarios
- When no parent form is found, fall back to immediate load instead
  of silently deadlocking
- Use $(window).one() for the recaptchaapiready listener to prevent
  handler accumulation on repeated renderReCaptcha calls

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
rhoerr added a commit that referenced this pull request Mar 18, 2026
rhoerr added a commit that referenced this pull request Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

reCapcha loading reflects to page speed [Performance] Load reCaptcha on focus to input to improve page load times

2 participants