Skip to content

Commit 8777b93

Browse files
[Turnstile] Update rendering page (#26533)
* embed the widget updates * delete rendering tutorial * edits * redirect * mg explicit
1 parent 0463a95 commit 8777b93

File tree

3 files changed

+75
-276
lines changed

3 files changed

+75
-276
lines changed

public/__redirects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,7 @@
16281628
/turnstile/troubleshooting/troubleshooting-faqs/ /turnstile/frequently-asked-questions/#troubleshooting 301
16291629
/turnstile/tutorials/protecting-your-payment-form-from-attackers-bots-using-turnstile/ /developer-spotlight/ 301
16301630
/turnstile/frequently-asked-questions/ /turnstile/ 301
1631+
/turnstile/tutorials/implicit-vs-explicit-rendering/ /turnstile/get-started/client-side-rendering/ 301
16311632

16321633
# waf
16331634
/waf/about/ /waf/concepts/ 301

src/content/docs/turnstile/get-started/client-side-rendering/index.mdx

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ import { Render, Tabs, TabItem, Steps, Details } from "~/components";
99

1010
Learn how to add the Turnstile widget to your webpage using implicit or explicit rendering methods.
1111

12+
Choosing the appropriate rendering method for Turnstile is important and effectively helps you to add it to your website while ensuring optimal performance and user experience. Both implicit and explicit rendering have their own advantages and are suited to different types of web applications. Refer to the table below that highlights the ideal use case for each of these rendering methods:
13+
14+
| Feature | Implicit rendering | Explicit rendering |
15+
|-------------------------|--------------------------------------|------------------------------------------|
16+
| **Ease of setup** | Simple, minimal code | Requires additional JavaScript |
17+
| **Control over timing** | Renders automatically on page load | Full control over rendering timing |
18+
| **Use cases** | Static content | Dynamic or interactive content |
19+
| **Customization** | Limited to HTML attributes | Extensive via JavaScript API |
20+
21+
1222
## Prerequisites
1323

1424
Before you begin, you must have:
@@ -28,7 +38,7 @@ Before you begin, you must have:
2838

2939
## Implicit rendering
3040

31-
Implicit rendering automatically scans your HTML for elements with the `cf-turnstile` class and renders widgets when the page loads.
41+
Implicit rendering automatically scans your HTML for elements with the `cf-turnstile` class and renders widgets without additional JavaScript code. This set up is ideal for static pages where you want the widget to load immediately when the page loads.
3242

3343
### Use cases
3444

@@ -43,7 +53,7 @@ Cloudflare recommends using implicit rendering on the following scenarios:
4353

4454
#### 1. Add the Turnstile script
4555

46-
Add the Turnstile JavaScript API to your HTML.
56+
**Include the Turnstile Script**: Add the Turnstile JavaScript API to your HTML file within the `<head>` section or just before the closing `</body>` tag.
4757

4858
```html wrap
4959
<script
@@ -75,7 +85,7 @@ Add widget containers where you want the challenges to appear on your website.
7585

7686
#### 3. Configure with data attributes
7787

78-
[Customize your widgets](/turnstile/get-started/client-side-rendering/widget-configurations/) using data attributes.
88+
[Customize your widgets](/turnstile/get-started/client-side-rendering/widget-configurations/) using data attributes. Insert a `div` element where you want the widget to appear.
7989

8090
```html
8191
<div
@@ -87,9 +97,12 @@ Add widget containers where you want the challenges to appear on your website.
8797
></div>
8898
```
8999

100+
Once a challenge has been solved, a token is passed to the success callback. This token must be validated against our [Siteverify endpoint](/turnstile/get-started/server-side-validation/).
101+
90102
### Complete implicit rendering examples by use case
91103

92104
<Details header="Basic login form">
105+
Turnstile is often used to protect forms on websites such as login forms or contact forms. You can embed the widget within your `<form>` tag.
93106
```html title="Example"
94107
<!DOCTYPE html>
95108
<html>
@@ -110,6 +123,32 @@ Add widget containers where you want the challenges to appear on your website.
110123
</body>
111124
</html>
112125
```
126+
127+
An invisible input with the name `cf-turnstile-response` is added and will be sent to the server with the other fields.
128+
129+
```html title="Complete HTML example"
130+
<!DOCTYPE html>
131+
<html lang="en">
132+
<head>
133+
<meta charset="UTF-8">
134+
<title>Implicit Rendering with Cloudflare Turnstile</title>
135+
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
136+
</head>
137+
<body>
138+
<h1>Contact Us</h1>
139+
<form action="/submit" method="POST">
140+
<label for="name">Name:</label><br>
141+
<input type="text" id="name" name="name" required><br>
142+
<label for="email">Email:</label><br>
143+
<input type="email" id="email" name="email" required><br>
144+
<!-- Turnstile Widget -->
145+
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>"></div>
146+
<br>
147+
<button type="submit">Submit</button>
148+
</form>
149+
</body>
150+
</html>
151+
```
113152
</Details>
114153

115154
<Details header="Advanced form with callbacks">
@@ -201,7 +240,7 @@ When you embed a Turnstile widget inside a `<form>` element, an invisible input
201240

202241
## Explicit rendering
203242

204-
Explicit rendering gives you programmatic control over when and how widgets are created using JavaScript functions.
243+
Explicit rendering gives you programmatic control over when and where the widget appears and how the widgets are created using JavaScript functions. This method is suitable for dynamic content, single-page applications(SPAs), or conditional rendering based on user interactions.
205244

206245
### Use cases
207246

@@ -245,6 +284,36 @@ const widgetId = turnstile.render("#turnstile-container", {
245284
});
246285
```
247286

287+
### Optional calls
288+
289+
After rendering the Turnstile widget explicitly, you may need to interact with it based on your application's requirements. Refer to the sections below to manage the widget's state.
290+
291+
#### Reset a widget
292+
293+
To reset the widget if the given widget timed out or expired, you can use the function:
294+
295+
```js
296+
turnstile.reset(widgetId);
297+
```
298+
299+
#### Get the response token
300+
301+
Retrieve the current response token at any time:
302+
303+
```js
304+
const responseToken = turnstile.getResponse(widgetId);
305+
```
306+
307+
#### Remove a widget
308+
309+
When a widget is no longer needed, it can be removed from the page using:
310+
311+
```js
312+
turnstile.remove(widgetId)
313+
```
314+
315+
This will not call any callback and will remove all related DOM elements.
316+
248317
### Complete explicit rendering examples by use case
249318

250319
<Details header="Basic explicit implementation">
@@ -457,4 +526,4 @@ Turnstile is designed to function only on pages using `http://` or `https://` UR
457526

458527
## Security requirements
459528

460-
<Render file="security-requirements" product="turnstile" />
529+
<Render file="security-requirements" product="turnstile" />

0 commit comments

Comments
 (0)