---
title: "Cloudflare Workers + D1"
description: "把 Calends 部署到自己的 Cloudflare 帳號：一鍵部署或指令行部署、D1 資料庫、定時任務及可選的電郵提醒。"
lang: "zh-HK"
version: "1.84.0"
url: "https://docs.calends.date/zh-hk/self-host/cloudflare/"
markdown_url: "https://docs.calends.date/zh-hk/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-hk/self-host/cloudflare/
- Markdown source: https://docs.calends.date/zh-hk/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-hk/self-host/config.md)。

## 自訂域名

詳見[自訂域名與 HTTPS](https://docs.calends.date/zh-hk/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-hk/self-host/cloudflare.md) *(current)*
- [Docker](https://docs.calends.date/zh-hk/self-host/docker.md)
- [設定項](https://docs.calends.date/zh-hk/self-host/config.md)
- [自訂域名與 HTTPS](https://docs.calends.date/zh-hk/self-host/custom-domain.md)
- [升級](https://docs.calends.date/zh-hk/self-host/upgrade.md)
- [本機開發](https://docs.calends.date/zh-hk/self-host/development.md)
- [參與翻譯](https://docs.calends.date/zh-hk/self-host/translate.md)

### Reading Order
- Previous: [私隱](https://docs.calends.date/zh-hk/data/privacy.md)
- Next: [Docker](https://docs.calends.date/zh-hk/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-hk/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
