diff --git a/frontend/src/components/Captcha.jsx b/frontend/src/components/Captcha.jsx index 87626cf..6c2df87 100644 --- a/frontend/src/components/Captcha.jsx +++ b/frontend/src/components/Captcha.jsx @@ -27,7 +27,7 @@ export default function Captcha({ onToken }) { try { window.turnstile.remove(widgetIdRef.current) } catch { - // Rien à faire + // Widget déjà supprimé } widgetIdRef.current = null @@ -41,6 +41,7 @@ export default function Captcha({ onToken }) { sitekey: TURNSTILE_SITE_KEY, language: 'fr', size: 'flexible', + appearance: 'always', callback: (token) => { onToken(token) @@ -66,23 +67,28 @@ export default function Captcha({ onToken }) { return } - const previousCallback = window.onloadTurnstileCallback + if (existingScript) { + existingScript.addEventListener('load', renderWidget, { + once: true, + }) - window.onloadTurnstileCallback = () => { - previousCallback?.() - renderWidget() + return () => { + cancelled = true + } } - if (!existingScript) { - const script = document.createElement('script') + const script = document.createElement('script') - script.id = TURNSTILE_SCRIPT_ID - script.src = `${TURNSTILE_SCRIPT_URL}&onload=onloadTurnstileCallback` - script.async = true - script.defer = true + script.id = TURNSTILE_SCRIPT_ID + script.src = TURNSTILE_SCRIPT_URL + script.async = true + script.defer = true - document.head.appendChild(script) - } + script.addEventListener('load', renderWidget, { + once: true, + }) + + document.head.appendChild(script) return () => { cancelled = true @@ -94,7 +100,7 @@ export default function Captcha({ onToken }) { try { window.turnstile.remove(widgetIdRef.current) } catch { - // Rien à faire + // Widget déjà supprimé } }