### Manage Development and Deployment with Wrangler Source: https://context7.com/cloudflare/turnstile-demo-workers/llms.txt Commands for installing dependencies, running the local development server, and publishing the worker to Cloudflare. ```bash # Install dependencies npm install # Run local development server npm run dev # > wrangler dev src/index.mjs --local # ⛅️ wrangler 2.1.7 # ⎔ Starting a local server... # Press [b] to open browser, [d] for Devtools, [x] to exit # Deploy to Cloudflare Workers npm run deploy # > wrangler publish src/index.mjs # Total Upload: 2.94 KiB / gzip: 1.33 KiB # Published turnstile-demo-workers # https://turnstile-demo-workers....workers.dev ``` -------------------------------- ### Initialize Turnstile widget Source: https://github.com/cloudflare/turnstile-demo-workers/blob/main/src/explicit.html JavaScript callback function to render the Turnstile widget once the script is loaded. ```javascript // This function is called when the Turnstile script is loaded and ready to be used. // The function name matches the "onload=..." parameter. function _turnstileCb() { console.debug('_turnstileCb called'); turnstile.render('#myWidget', { sitekey: '1x00000000000000000000AA', theme: 'light', }); } ``` -------------------------------- ### Configure Wrangler Settings Source: https://context7.com/cloudflare/turnstile-demo-workers/llms.txt The wrangler.toml file defines the worker name, development environment, and compatibility date. ```toml # wrangler.toml name = "turnstile-demo-workers" workers_dev = true compatibility_date = "2022-09-26" ``` -------------------------------- ### Style the login form Source: https://github.com/cloudflare/turnstile-demo-workers/blob/main/src/explicit.html CSS rules to center the login form and define its layout properties. ```css html, body { height: 100%; } body { display: flex; align-items: center; padding-top: 40px; padding-bottom: 40px; background-color: #fefefe; } .form-signin { width: 100%; max-width: 330px; padding: 15px; margin: auto; } .form-signin .checkbox { font-weight: 400; } .form-signin .form-floating:focus-within { z-index: 2; } .form-signin input[type="text"] { margin-bottom: -1px; border-bottom-right-radius: 0; border-bottom-left-radius: 0; } .form-signin input[type="password"] { margin-bottom: 10px; border-top-left-radius: 0; border-top-right-radius: 0; } ``` -------------------------------- ### Implement Cloudflare Worker Fetch Handler Source: https://context7.com/cloudflare/turnstile-demo-workers/llms.txt The main entry point for the Worker, routing incoming requests to serve HTML pages or process POST requests for token validation. ```javascript // src/index.mjs import explicitRenderHtml from './explicit.html'; import implicitRenderHtml from './implicit.html'; export default { async fetch(request) { // Handle POST requests for token validation if (request.method === 'POST') { return await handlePost(request); } // Serve HTML pages based on URL path const url = new URL(request.url); let body = implicitRenderHtml; if (url.pathname === '/explicit') { body = explicitRenderHtml; } return new Response(body, { headers: { 'Content-Type': 'text/html', }, }); }, }; ``` -------------------------------- ### Implement Implicit Turnstile Rendering Source: https://context7.com/cloudflare/turnstile-demo-workers/llms.txt Configures the Turnstile widget to render automatically upon page load using the cf-turnstile class. ```html