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