fix: defer reCAPTCHA load to renderReCaptcha with idle fallback for iframe compat - #7
Closed
rhoerr wants to merge 2 commits into
Closed
fix: defer reCAPTCHA load to renderReCaptcha with idle fallback for iframe compat#7rhoerr wants to merge 2 commits into
rhoerr wants to merge 2 commits into
Conversation
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>
Contributor
Author
|
Lucas van Staden says it tests well with this fix:
|
Contributor
Author
|
In further discussion, Lucas can't reproduce issues with these changes but is still seeing anomalous behavior / logged errors / possibly lower order volume. I'm going to defer this at least for now and roll back #6. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The reCAPTCHA deferred loading in #6 broke iframe-embedded payment forms (e.g. Braintree hosted fields) and had a DOM timing bug where the focus listener never actually attached.
This PR:
initialize()torenderReCaptcha()— the KOafterRendercallback, where the form DOM actually existsrequestIdleCallbackwith 3s timeout (orsetTimeout(1s)fallback) ensures the API loads for iframes and no-focus scenarios<form>is found, instead of silently deadlocking$(window).one()forrecaptchaapireadyto prevent handler accumulationProblems with the previous approach (#6)
attachFocusEvent()ran ininitialize()before KO rendered the templatefocuson the parent form$(window).on('recaptchaapiready')accumulated handlers on repeatedrenderReCaptchacalls (likereCaptchaStorePickup)Compatibility notes
reCaptchaStorePickup:_apiLoadedguard prevents double_deferApiLoad;.one()prevents handler leakreCaptchaCheckoutclones: Inherit_apiLoadedstate correctly; no behavior changeRef: #6 (comment)
Test plan