---
title: "Cloudflare Workers + D1"
description: "將 Calends 部署至自己的 Cloudflare 帳號：一鍵部署或命令列部署、D1 資料庫、排程任務與選用的郵件提醒。"
lang: "zh-TW"
version: "1.84.0"
url: "https://docs.calends.date/zh-tw/self-host/cloudflare/"
markdown_url: "https://docs.calends.date/zh-tw/self-host/cloudflare.md"
section: "自託管"
llms_index: "https://docs.calends.date/llms.txt"
llms_full: "https://docs.calends.date/llms-full.txt"
agents_guide: "https://calends.date/AGENTS.md"
---

# Cloudflare Workers + D1

> 將 Calends 部署至自己的 Cloudflare 帳號：一鍵部署或命令列部署、D1 資料庫、排程任務與選用的郵件提醒。

- Canonical page: https://docs.calends.date/zh-tw/self-host/cloudflare/
- Markdown source: https://docs.calends.date/zh-tw/self-host/cloudflare.md
- Calends version: v1.84.0

將 Calends 部署到自己的 Cloudflare 帳號，並配置 D1、排程任務與選用的電子郵件提醒。

Calends 是一個 Cloudflare Worker：前端由 Worker 靜態資源提供服務，資料儲存在 D1（SQLite），提醒與匯率更新則由 Cron 觸發器驅動。個人使用量與費用取決於 Cloudflare 帳號的現行方案與配額。

## 事前準備

- 一個 Cloudflare 帳號；
- 使用命令列部署另須安裝 Node.js 24 與 git。

## 一鍵部署

開啟 [Deploy to Cloudflare](https://deploy.workers.cloudflare.com/?url=https://github.com/imetn/calends)，依畫面指示將儲存庫複製到你的 GitHub 帳號並部署。

部署完成後，請至 Cloudflare 控制台開啟此 Worker 的「設定 → 變數和機密」，新增一個類型為「機密」、名稱為 `SETUP_TOKEN` 的變數，並填入一串足夠長的隨機字元。缺少該機密將無法初始化密碼。

## 命令列部署

1. 複製儲存庫並安裝相依套件：

   ```bash
   git clone https://github.com/imetn/calends && cd calends
   npm ci
   ```

2. 登入 Cloudflare，確認 `npx wrangler whoami` 能正確顯示你的帳號：

   ```bash
   npx wrangler login
   ```

3. 設定初始化權杖：

   ```bash
   npx wrangler secret put SETUP_TOKEN
   ```

   例如使用 `openssl rand -base64 24` 產生隨機字串。

4. 執行部署：

   ```bash
   npm run deploy
   ```

5. 開啟 `https://calends.<你的子網域>.workers.dev`，輸入初始化權杖並設定密碼。

`npm run deploy` 會依序完成以下動作：

1. 產生資料庫遷移與多語言檔案、執行型別檢查並以 Vite 建置前端；
2. 執行 `wrangler deploy` 部署 Worker。首次部署會自動建立名為 `calends` 的 D1 資料庫，無須手動填寫 `database_id`；
3. 對遠端資料庫套用所有遷移。

Worker 本身在每個執行個體處理第一筆請求時也會自動檢查並補齊遺漏的遷移，因此一鍵部署建立的執行個體同樣能直接運作。

## wrangler.jsonc

儲存庫內的 `wrangler.jsonc` 已完成基本配置：

| 設定 | 作用 |
| --- | --- |
| `assets` | 前端靜態資源；`/api/*`、`/cal/*`、`/mcp` 優先交由 Worker 處理，其餘路徑以單頁應用回退。 |
| `d1_databases` | 綁定名稱為 `DB`，資料庫名稱為 `calends`。 |
| `triggers.crons` | `*/15 * * * *`：每 15 分鐘檢查提醒與失敗重試；於 UTC 整點另行維護匯率，距上次更新超過 20 小時即重新擷取。 |
| `observability` | 啟用 Workers 日誌，可於控制台檢視提醒傳送結果與遷移紀錄。 |

## 郵件提醒

電子郵件提醒採用 [Cloudflare Email Sending](https://developers.cloudflare.com/email-routing/email-workers/send-email-workers/)。若不需要郵件提醒可略過本節，其餘通知管道皆無須伺服器端設定。

1. 在 Cloudflare 為你的網域啟用郵件寄送功能，並驗證寄件位址，例如 `calends@example.com`。
2. 在 `wrangler.jsonc` 中取消註解並修改下列兩段：

   ```jsonc
   "send_email": [{ "name": "EMAIL", "allowed_sender_addresses": ["calends@example.com"] }],
   "vars": { "SITE_URL": "https://calends.example.com", "MAIL_FROM": "calends@example.com", "MAIL_FROM_NAME": "Calends" },
   ```

3. 重新部署：`npm run deploy`。
4. 在「設定 → 通知管道」新增電子郵件管道，填入收件信箱後點選「測試」。

> **NOTE**: `MAIL_FROM_NAME` 可留空，預設採介面語言中的應用名稱。`SITE_URL` 亦為選填，用於決定提醒訊息與行事曆中的站點連結，詳見[設定項](https://docs.calends.date/zh-tw/self-host/config.md)。

## 自訂網域

請參閱[自訂網域與 HTTPS](https://docs.calends.date/zh-tw/self-host/custom-domain.md)。

## 費用

用量涵蓋 Workers 請求數、D1 讀寫與儲存容量、Cron 觸發，以及選用的郵件寄送。實際用量與現行配額請於 Cloudflare 控制台查詢。內建助手呼叫 DeepSeek 所產生的費用由你自己的 DeepSeek 帳戶計費，與 Cloudflare 無關。

---

## Related Pages & Global AI Discovery

To explore broader context beyond this page, fetch any of the Markdown or plain-text resources below directly:

### Same Section (自託管)
- [Cloudflare Workers + D1](https://docs.calends.date/zh-tw/self-host/cloudflare.md) *(current)*
- [Docker](https://docs.calends.date/zh-tw/self-host/docker.md)
- [設定項](https://docs.calends.date/zh-tw/self-host/config.md)
- [自訂網域與 HTTPS](https://docs.calends.date/zh-tw/self-host/custom-domain.md)
- [升級](https://docs.calends.date/zh-tw/self-host/upgrade.md)
- [本機開發](https://docs.calends.date/zh-tw/self-host/development.md)
- [參與翻譯](https://docs.calends.date/zh-tw/self-host/translate.md)

### Reading Order
- Previous: [隱私](https://docs.calends.date/zh-tw/data/privacy.md)
- Next: [Docker](https://docs.calends.date/zh-tw/self-host/docker.md)

### Global Calends Resources
- Read-only Docs MCP Server (zero auth Streamable HTTP): https://docs.calends.date/mcp
- Complete Docs Index (all 37 topics with `.md` links): https://docs.calends.date/llms.txt
- Complete English Docs Bundle (single file): https://docs.calends.date/llms-full.txt
- Complete Simplified Chinese Docs Bundle: https://docs.calends.date/llms-zh.txt
- Docs AI Agent Guide: https://docs.calends.date/AGENTS.md
- Product Website Overview (Markdown): https://calends.date/zh-tw/index.md
- Product LLMs Index & Full Reference: https://calends.date/llms.txt | https://calends.date/llms-full.txt
- Deployment & MCP Agent Guide (AGENTS.md): https://calends.date/AGENTS.md
- FAQ & Calends vs Wallos Comparison (Markdown): https://calends.date/en/faq.md | https://calends.date/en/compare.md
- Complete Changelog (Markdown): https://calends.date/en/releases.md
