Skip to content

Commit b6754f4

Browse files
committed
Add Svelte 5 Support
Modifying script loading to accommodate the upcoming release of Svelte 5. The `on:load` event for scripts placed inside `<svelte:head>` does not fire in Svelte 5 as a result of this bug: sveltejs/svelte#8301 Instead of loading the Cloudfare Turnstile script in `<svelte:head>`, load the script dynamically during mount. This is functionally equivalent to using `<svelte:head>` but accommodates an operational load event handler. If Svelte 5 resolves Issue 8301, this is not needed.
1 parent 73f165e commit b6754f4

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/lib/Turnstile.svelte

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434
onMount(() => {
3535
mounted = true;
3636
37+
if (!loaded) {
38+
let script = document.createElement('script');
39+
script.type = 'text/javascript';
40+
script.src =
41+
'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit';
42+
script.async = true;
43+
script.addEventListener('load', loadCallback);
44+
document.head.appendChild(script);
45+
}
46+
3747
return () => {
3848
mounted = false;
3949
};
@@ -101,15 +111,6 @@
101111
};
102112
</script>
103113

104-
<svelte:head>
105-
{#if mounted && !loaded}
106-
<script
107-
src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit"
108-
on:load={loadCallback}
109-
async></script>
110-
{/if}
111-
</svelte:head>
112-
113114
{#if loaded && mounted}
114115
{#key $$props}
115116
<div use:turnstile />

0 commit comments

Comments
 (0)