1
Turnstile
hanyixuanten edited this page 2026-10-25 11:30:13 +00:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Turnstile 人机验证(可选)

给投稿表单加上 Cloudflare Turnstile 人机验证,防脚本刷 PR。不配置则跳过验证,功能不受影响。

1. 创建站点

Dashboard → Turnstile → Add site → 拿到一对 key:

  • Site Key(前端用)
  • Secret Key(后端用)

2. 配置

两个变量成对配置才启用:

变量 配在哪 说明
PUBLIC_TURNSTILE_SITE_KEY Pages 构建时环境变量(本地构建写入 .env) 注入前端,渲染验证组件
TURNSTILE_SECRET_KEY Pages 运行时环境变量 functions/api/submit.ts 校验 token

注意 site key 是构建时变量:改了它要重新部署才生效。

3. 验证

线上打开投稿页,表单下方出现 Turnstile 验证组件即已启用;提交时会先校验,未通过提示「请先完成人机验证」。