-
-
Notifications
You must be signed in to change notification settings - Fork 33
Update and optimize reCAPTCHA script loading #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Update and optimize reCAPTCHA script loading #50
Conversation
const buildScript = () => { | ||
const script = document.createElement("script"); | ||
script.src = scriptUrl; | ||
script.async = true; | ||
script.defer = true; | ||
script.onload = renderRecaptcha; | ||
script.onerror = (event) => onError("Failed to load reCAPTCHA script"); | ||
document.body.appendChild(script); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Originally I wanted to use the onload
query parameter that is mentioned in googles docs. But I could not get it to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reformatted the code to be more aesthetically pleasing IMO. If you disagree, I'll happily revert any formatting changes!
script.async = true; | ||
script.defer = true; | ||
script.onload = renderRecaptcha; | ||
script.onerror = (event) => onError("Failed to load reCAPTCHA script"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This addition allows us to know if the loading the script fails.
This PR introduces an optimized approach to load reCAPTCHA.
I was having issues where the recaptcha component would hang indefinitely. I dug into the source code to resolve the issue, and noticed there was no way to tell if the loading of the recaptcha script failed. In addition, the reliance on
setInterval
andsetTimeout
raised concerns for potential race conditions at worst, and poorly optimized code at bets.Guided by Google’s documentation on loading reCAPTCHA, this update focuses on event-driven execution and improved race condition handling. This alignment with Google’s documentation enhances the library’s loading efficiency and aims to give better insight into potential errors.
Benchmarks
For these benchmarks I measured the time between calling
open
andonVerify
.@douglasjunior I look forward to any feedback or critique you have!