Summary
We want to load the hCaptcha SDK in several places so that we can use it, but sometimes handlers may attempt to load the SDK again. In this case we should ensure that the SDK is loaded only once
Background
- ConfirmEdit (CAPTCHA extension) has a VisualEditor handler that will render the hCaptcha widget when the edit fails because hCaptcha was not provided
- When this error handler matches, the code calls for the hCaptcha SDK to be loaded. However, the SDK may have already been loaded through loadHCaptcha in utils.js
- We should ensure that the loadHCaptcha will only load hCaptcha once as long as at least one previous attempt to load hCaptcha was successful
- We should not need to debounce calls, so should only need to attempt to reject duplicate calls that will happen a few seconds apart. Therefore, we don't need to be currently strict about the handling of duplicate calls
Technical notes
- We can attach classes to the script element we use to load the hCaptcha SDK
- This includes the script that we may load by adding the script to the HTML returned by the client
- These classes will then be used to determine if hCaptcha is already being loaded, has failed to load, or has successfully loaded
- If hCaptcha is loading, then the loadHCaptcha method should return a promise that mimics the resolve/reject pattern being used to handle the other caller
- If hCaptcha is loaded successfully, then the loadHCaptcha method should immediately return a resolved promise
- If hCaptcha has failed to load or has never attempted to be loaded, then the loadHCaptcha method should continue as normal
Acceptance criteria
- Multiple calls to loadHCaptcha do not cause the hCaptcha SDK to be loaded multiple times