fix: reload turnstile on SPA navigation
Some checks failed
Tests / Backend - Tests skipped: 8, passed: 74
Backend Coverage #### Project Overview No changes detected, that affect the code coverage. * Line Coverage: 78.66% (553/703) * Lines of Code: 703 #### Quality Gates Summary No active quality gates.
Tests / Frontend - Lint, Tests et Build passed: 83
Frontend Coverage #### Project Overview No changes detected, that affect the code coverage. * Line Coverage: 80.20% (555/692) * Lines of Code: 692 #### Quality Gates Summary No active quality gates.
THE-TIP-TOP-MULTIBRANCH/pipeline/head There was a failure building this commit

This commit is contained in:
2026-09-20 01:52:36 +00:00
parent d56b91479f
commit 361cf46905

View File

@@ -37,6 +37,15 @@ export default function Captcha({ onToken }) {
containerRef.current.innerHTML = '' containerRef.current.innerHTML = ''
window.turnstile.ready(() => {
if (
cancelled ||
!containerRef.current ||
!window.turnstile
) {
return
}
widgetIdRef.current = window.turnstile.render( widgetIdRef.current = window.turnstile.render(
containerRef.current, containerRef.current,
{ {
@@ -46,18 +55,25 @@ export default function Captcha({ onToken }) {
appearance: 'always', appearance: 'always',
callback: (token) => { callback: (token) => {
if (!cancelled) {
onToken(token) onToken(token)
}
}, },
'expired-callback': () => { 'expired-callback': () => {
if (!cancelled) {
onToken('') onToken('')
}
}, },
'error-callback': () => { 'error-callback': () => {
if (!cancelled) {
onToken('') onToken('')
}
}, },
} }
) )
})
} }
const existingScript = document.getElementById( const existingScript = document.getElementById(
@@ -66,19 +82,11 @@ export default function Captcha({ onToken }) {
if (window.turnstile) { if (window.turnstile) {
renderWidget() renderWidget()
return } else if (existingScript) {
}
if (existingScript) {
existingScript.addEventListener('load', renderWidget, { existingScript.addEventListener('load', renderWidget, {
once: true, once: true,
}) })
} else {
return () => {
cancelled = true
}
}
const script = document.createElement('script') const script = document.createElement('script')
script.id = TURNSTILE_SCRIPT_ID script.id = TURNSTILE_SCRIPT_ID
@@ -91,6 +99,7 @@ export default function Captcha({ onToken }) {
}) })
document.head.appendChild(script) document.head.appendChild(script)
}
return () => { return () => {
cancelled = true cancelled = true
@@ -107,6 +116,10 @@ export default function Captcha({ onToken }) {
} }
widgetIdRef.current = null widgetIdRef.current = null
if (containerRef.current) {
containerRef.current.innerHTML = ''
}
} }
}, [onToken]) }, [onToken])