我的Blog採用的留言系統 (Giscus+CF方案)
這個系統我沒有實作,郵件通知之類的功能,另外要管理留言要到CF後台
我幹麼用KV不用SQL?因為當時想法是把這留言系統當作「備用」並非主要,因此選擇簡單就好,如果你有大量留言需求,就不要用這個系統了
- Create a KV namespace and bind it as
BLOG_COMMENTSinwrangler.toml. - Set the following environment variables / secrets on the Worker:
TURNSTILE_SECRET_KEY— your Cloudflare Turnstile secret keyADMIN_SECRET— a secret string used to authorize comment deletionALLOWED_ORIGINS— comma-separated list of allowed origins, e.g.https://yourdomain.com,http://localhost:3000
- Deploy
worker.jswithwrangler deploy.
Edit the CONFIG object at the top of comments.js:
const CONFIG = {
apiUrl: "https://your-worker-subdomain.workers.dev",
turnstileSiteKey: "YOUR_TURNSTILE_SITE_KEY",
giscus: {
repo: "your-github-username/your-repo",
repoId: "YOUR_REPO_ID",
category: "Q&A",
categoryId: "YOUR_CATEGORY_ID"
}
};<div id="comments"></div>
<script src="comments.js"></script>DELETE https://your-worker-subdomain.workers.dev?postId=/some/post&commentId=<id>
Authorization: <ADMIN_SECRET>
Deleting a comment also deletes all of its replies.
- 建立一個 KV namespace,並在
wrangler.toml裡綁定成BLOG_COMMENTS。 - 在 Worker 上設定以下環境變數 / secrets:
TURNSTILE_SECRET_KEY— 你的 Cloudflare Turnstile secret keyADMIN_SECRET— 用來授權刪除留言的密鑰字串ALLOWED_ORIGINS— 允許的來源網址,用逗號分隔,例如https://yourdomain.com,http://localhost:3000
- 用
wrangler deploy部署worker.js。
編輯 comments.js 最上方的 CONFIG 物件:
const CONFIG = {
apiUrl: "https://your-worker-subdomain.workers.dev",
turnstileSiteKey: "YOUR_TURNSTILE_SITE_KEY",
giscus: {
repo: "your-github-username/your-repo",
repoId: "YOUR_REPO_ID",
category: "Q&A",
categoryId: "YOUR_CATEGORY_ID"
}
};<div id="comments"></div>
<script src="comments.js"></script>