11import React , { useEffect , useState , useRef } from "react" ;
22import { TurnstileOptions } from "turnstile-types" ;
33
4- const global = globalThis ?? window ;
4+ const global = ( globalThis ?? window ) as any ;
55let turnstileState =
6- typeof ( global as any ) . turnstile !== "undefined" ? "ready" : "unloaded" ;
6+ typeof global . turnstile !== "undefined" ? "ready" : "unloaded" ;
77let ensureTurnstile : ( ) => Promise < any > ;
88
99// Functions responsible for loading the turnstile api, while also making sure
@@ -20,20 +20,22 @@ let ensureTurnstile: () => Promise<any>;
2020 turnstileLoad = { resolve, reject } ;
2121 if ( turnstileState === "ready" ) resolve ( undefined ) ;
2222 } ) ;
23- ( global as any ) [ TURNSTILE_LOAD_FUNCTION ] = ( ) => {
24- turnstileLoad . resolve ( ) ;
25- turnstileState = "ready" ;
26- } ;
2723
2824 ensureTurnstile = ( ) => {
2925 if ( turnstileState === "unloaded" ) {
3026 turnstileState = "loading" ;
27+ global [ TURNSTILE_LOAD_FUNCTION ] = ( ) => {
28+ turnstileLoad . resolve ( ) ;
29+ turnstileState = "ready" ;
30+ delete global [ TURNSTILE_LOAD_FUNCTION ] ;
31+ } ;
3132 const url = `${ TURNSTILE_SRC } ?onload=${ TURNSTILE_LOAD_FUNCTION } &render=explicit` ;
3233 const script = document . createElement ( "script" ) ;
3334 script . src = url ;
3435 script . async = true ;
3536 script . addEventListener ( "error" , ( ) => {
3637 turnstileLoad . reject ( "Failed to load Turnstile." ) ;
38+ delete global [ TURNSTILE_LOAD_FUNCTION ] ;
3739 } ) ;
3840 document . head . appendChild ( script ) ;
3941 }
0 commit comments