|
1 | 1 | <script lang="ts"> |
2 | 2 | import Bug from '@lucide/svelte/icons/bug'; |
3 | | - import { dev } from '$app/environment'; |
| 3 | + import { browser, dev } from '$app/environment'; |
4 | 4 | import { PUBLIC_TURNSTILE_DEV_BYPASS_VALUE, PUBLIC_TURNSTILE_SITE_KEY } from '$env/static/public'; |
5 | 5 | import CloudflareLogo from '$lib/components/svg/CloudflareLogo.svelte'; |
6 | 6 | import LoadingCircle from '$lib/components/svg/LoadingCircle.svelte'; |
|
58 | 58 |
|
59 | 59 | onMount(() => { |
60 | 60 | if (dev) { |
61 | | - console.log('Turnstile is disabled in dev mode'); |
62 | | - toast.warning('Turnstile is disabled in dev mode'); |
63 | 61 | response = PUBLIC_TURNSTILE_DEV_BYPASS_VALUE; |
64 | 62 | return; |
65 | 63 | } |
66 | 64 |
|
67 | | - // If turstile doesnt load, then the index.html is proabably missing the script tag (https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/#explicitly-render-the-turnstile-widget) |
| 65 | + // Check that Cloudflare Turnstile has been loaded. |
| 66 | + // If `window.turnstile` is undefined, it usually means the <script> tag wasn't injected. |
| 67 | + // If this error occured under a page that wasnt server-side rendered or prerendered then the script tag never got injected into the html served to browser. |
| 68 | + // The script tag will need to get moved into the app.html tag if this is the case, but normally Turnstile should only be used in places where a user isnt auth, places that are pre-renderable. |
| 69 | + // See: https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/#explicitly-render-the-turnstile-widget |
68 | 70 | if (!window.turnstile) { |
69 | | - console.error('Failed to load Cloudflare Turnstile resource!'); |
70 | | - toast.error('Failed to load Cloudflare Turnstile resource!'); |
| 71 | + console.error('Failed to load Cloudflare Turnstile (Check Turnstile.svelte for more info)'); |
| 72 | + toast.error('Internal Error'); |
71 | 73 | return; |
72 | 74 | } |
73 | 75 |
|
|
79 | 81 | }); |
80 | 82 | </script> |
81 | 83 |
|
| 84 | +<svelte:head> |
| 85 | + {#if !browser && !dev} |
| 86 | + <script |
| 87 | + src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit" |
| 88 | + defer |
| 89 | + ></script> |
| 90 | + {/if} |
| 91 | +</svelte:head> |
| 92 | + |
82 | 93 | <!-- see: https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/#widget-size --> |
83 | 94 | <div class="mx-auto h-[65px] w-[300px]" bind:this={element}> |
84 | 95 | {#if !mounted} |
|
0 commit comments