Skip to content

Conversation

@gvanderclay
Copy link

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 and setTimeout 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 and onVerify.

Value 1 Value 2 Value 3 Value 4 Value 5 Average
New Code 1109 1320 827 789 807 970.4
Old Code 1922 1794 2024 1861 1784 1877

@douglasjunior I look forward to any feedback or critique you have!

Comment on lines +211 to +219
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);
};
Copy link
Author

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.

Copy link
Author

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");
Copy link
Author

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.

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.

1 participant