Skip to content

Commit 803b730

Browse files
committed
fix: retry turnstile challenge failures
1 parent 4fc7649 commit 803b730

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎apps/web/app/components/TurnstileWidget.tsx‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ declare global {
1212
turnstile?: {
1313
render: (element: HTMLElement, options: Record<string, unknown>) => string;
1414
remove: (widgetId: string) => void;
15+
reset: (widgetId: string) => void;
1516
};
1617
}
1718
}
@@ -53,6 +54,7 @@ export function TurnstileWidget() {
5354
if (!siteKey || !containerRef.current) return;
5455
let cancelled = false;
5556
let widgetId: string | null = null;
57+
let retryTimer: ReturnType<typeof setTimeout> | null = null;
5658

5759
loadTurnstileScript()
5860
.then(() => {
@@ -67,8 +69,14 @@ export function TurnstileWidget() {
6769
"expired-callback": () => {
6870
currentToken = "";
6971
},
70-
"error-callback": () => {
72+
"error-callback": (code: string) => {
7173
currentToken = "";
74+
if (retryTimer) clearTimeout(retryTimer);
75+
if (code.startsWith("600") || code.startsWith("300") || code === "200500") {
76+
retryTimer = setTimeout(() => {
77+
if (!cancelled && widgetId && window.turnstile) window.turnstile.reset(widgetId);
78+
}, 3000);
79+
}
7280
},
7381
});
7482
})
@@ -79,6 +87,7 @@ export function TurnstileWidget() {
7987
return () => {
8088
cancelled = true;
8189
currentToken = "";
90+
if (retryTimer) clearTimeout(retryTimer);
8291
if (widgetId && window.turnstile) window.turnstile.remove(widgetId);
8392
};
8493
}, [siteKey]);

0 commit comments

Comments
 (0)