Skip to content

Commit f8c8baf

Browse files
authored
Merge pull request #41 from IanVS/script-error
2 parents 3a6b3d6 + dc103f7 commit f8c8baf

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

docs/props.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ These are the props you can pass to the `Turnstile` component.
5656
| `async` | boolean | `true` | Define if set the injected script as async. This option has no effect if `injectScript` is set to false. |
5757
| `appendTo` | string | `'head'` | Define if inject the script in the head or in the body. This option has no effect if `injectScript` is set to false. |
5858
| `onLoadCallbackName` | string | `'onloadTurnstileCallback'` | Custom name of the onload callback. This option has no effect if `injectScript` is set to false. |
59+
| `onError` | function | `undefined` | Callback invoked when script fails to load (e.g. Cloudflare has an outage). |

packages/lib/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ interface ScriptOptions {
266266
* @default `onloadTurnstileCallback`
267267
*/
268268
onLoadCallbackName?: string
269+
270+
/**
271+
* Callback invoked when script fails to load (e.g. Cloudflare has an outage).
272+
*/
273+
onError?: () => void
269274
}
270275

271276
/** `<Turnstile />` component props */

packages/lib/src/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const checkElementExistence = (id: string) => !!document.getElementById(i
2222
export const injectTurnstileScript = ({
2323
render = 'explicit',
2424
onLoadCallbackName = DEFAULT_ONLOAD_NAME,
25-
scriptOptions: { nonce = '', defer = true, async = true, id = '', appendTo } = {}
25+
scriptOptions: { nonce = '', defer = true, async = true, id = '', appendTo, onError } = {}
2626
}: InjectTurnstileScriptParams) => {
2727
const scriptId = id || DEFAULT_SCRIPT_ID
2828

@@ -47,6 +47,10 @@ export const injectTurnstileScript = ({
4747
script.nonce = nonce
4848
}
4949

50+
if (onError) {
51+
script.onerror = onError
52+
}
53+
5054
const parentEl = appendTo === 'body' ? document.body : document.getElementsByTagName('head')[0]
5155

5256
parentEl!.appendChild(script)

0 commit comments

Comments
 (0)