web/Turnstile: check if already defined before making listener

This commit is contained in:
dumbmoron 2024-09-03 13:21:41 +00:00
parent fc26032048
commit b5d570c43f
No known key found for this signature in database

View file

@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import env from "$lib/env"; import env from "$lib/env";
import { onMount } from "svelte"; import { onMount } from "svelte";
import { browser } from "$app/environment";
import { turnstileLoaded } from "$lib/state/turnstile"; import { turnstileLoaded } from "$lib/state/turnstile";
@ -10,8 +11,7 @@
onMount(() => { onMount(() => {
const sitekey = env.TURNSTILE_KEY; const sitekey = env.TURNSTILE_KEY;
if (!sitekey) return; if (!sitekey) return;
const setup = () => {
turnstileScript.addEventListener("load", () => {
window.turnstile?.render(turnstileElement, { window.turnstile?.render(turnstileElement, {
sitekey, sitekey,
"error-callback": (error) => { "error-callback": (error) => {
@ -22,7 +22,13 @@
$turnstileLoaded = true; $turnstileLoaded = true;
} }
}); });
}); }
if (window.turnstile) {
setup();
} else {
turnstileScript.addEventListener("load", setup);
}
}); });
</script> </script>