|
| 1 | +# 🚀 Cloudflare Pages 部署指南 |
| 2 | + |
| 3 | +## 📋 部署前检查清单 |
| 4 | + |
| 5 | +- [ ] 代码已推送到 GitHub |
| 6 | +- [ ] 已安装 Node.js (v16+) 和 npm/pnpm |
| 7 | +- [ ] 拥有 Cloudflare 账户 |
| 8 | +- [ ] 拥有阿里云域名 (可选,可后续添加) |
| 9 | + |
| 10 | +## 🔧 第一步:连接 GitHub 仓库 |
| 11 | + |
| 12 | +1. **登录 Cloudflare Dashboard** |
| 13 | + - 访问 https://dash.cloudflare.com/ |
| 14 | + - 使用你的账号登录 |
| 15 | + |
| 16 | +2. **进入 Pages 管理** |
| 17 | + - 左侧菜单:**Workers & Pages** → **Pages** |
| 18 | + - 点击 **Create application** → **Connect to Git** |
| 19 | + |
| 20 | +3. **授权 GitHub** |
| 21 | + - 选择 **GitHub** |
| 22 | + - 授权 Cloudflare 访问你的 GitHub 账户 |
| 23 | + - 选择 Fork 后的 **inBoxCard** 仓库 |
| 24 | + |
| 25 | +## ⚙️ 第二步:配置构建设置 |
| 26 | + |
| 27 | +在 **Create production branch** 页面填写: |
| 28 | + |
| 29 | +| 配置项 | 值 | |
| 30 | +|------|-----| |
| 31 | +| **Framework preset** | `Vite` | |
| 32 | +| **Build command** | `npm run build` | |
| 33 | +| **Build output directory** | `dist` | |
| 34 | +| **Root directory** | `/` (或不填) | |
| 35 | +| **Node.js version** | `18.x` 或更高 | |
| 36 | + |
| 37 | +## 🔐 第三步:配置环境变量(可选) |
| 38 | + |
| 39 | +如果要使用 Gemini AI 功能: |
| 40 | + |
| 41 | +1. 在部署前,点击 **Settings** → **Environment variables** |
| 42 | +2. 添加: |
| 43 | + - **Variable name**: `VITE_GEMINI_API_KEY` |
| 44 | + - **Value**: 你的 Google Gemini API Key |
| 45 | + - **Environments**: 选择 `Production` |
| 46 | +3. 点击 **Save** |
| 47 | + |
| 48 | +> 获取 API Key: https://ai.google.dev/ |
| 49 | +
|
| 50 | +## 🚀 第四步:部署 |
| 51 | + |
| 52 | +1. 点击 **Save and Deploy** |
| 53 | +2. Cloudflare 会自动: |
| 54 | + - 拉取最新代码 |
| 55 | + - 执行构建命令 |
| 56 | + - 部署到全球 CDN |
| 57 | + |
| 58 | +部署通常需要 **2-5 分钟**,完成后你会看到一个 `.pages.dev` 域名。 |
| 59 | + |
| 60 | +## 🌐 第五步:绑定自定义域名(以 card.gudong.site 为例) |
| 61 | + |
| 62 | +### 在 Cloudflare Pages 中: |
| 63 | + |
| 64 | +1. 打开你的 Pages 项目 |
| 65 | +2. 点击 **Custom domains** |
| 66 | +3. 点击 **Set up a custom domain** |
| 67 | +4. 输入 `card.gudong.site`,点击 **Continue** |
| 68 | +5. Cloudflare 会生成一个 CNAME 记录值(例如:`inbox-hub.pages.dev`) |
| 69 | +6. **复制这个值**,等一会儿再验证 |
| 70 | + |
| 71 | +### 在阿里云域名控制台中: |
| 72 | + |
| 73 | +1. 登录阿里云:https://dc.console.aliyun.com |
| 74 | +2. 找到 `gudong.site` 域名,点击 **DNS 解析** |
| 75 | +3. 点击 **添加记录**,填写: |
| 76 | + - **记录类型**: `CNAME` |
| 77 | + - **主机记录**: `card` (不是 card.gudong.site) |
| 78 | + - **记录值**: 粘贴 Cloudflare 提供的值(如 `inbox-hub.pages.dev`) |
| 79 | + - **TTL**: 保持默认 `10分钟` |
| 80 | +4. 点击 **确认** |
| 81 | + |
| 82 | +4. 回到 Cloudflare Pages,点击 **Verify domain** 或 **Activate domain** |
| 83 | + |
| 84 | +> ⏱️ DNS 解析可能需要 5-30 分钟生效,请耐心等待 |
| 85 | +
|
| 86 | +## ✅ 验证部署 |
| 87 | + |
| 88 | +部署完成后,你可以通过以下方式验证: |
| 89 | + |
| 90 | +```bash |
| 91 | +# 测试 .pages.dev 域名 |
| 92 | +curl https://inbox-hub.pages.dev |
| 93 | + |
| 94 | +# 测试自定义域名 |
| 95 | +curl https://card.gudong.site |
| 96 | + |
| 97 | +# 或直接在浏览器打开 |
| 98 | +# https://card.gudong.site |
| 99 | +``` |
| 100 | + |
| 101 | +## 🔄 自动部署 |
| 102 | + |
| 103 | +配置完成后,**每次你推送代码到 GitHub main 分支**,Cloudflare 会**自动构建并部署**。 |
| 104 | + |
| 105 | +## 📱 本地测试 |
| 106 | + |
| 107 | +部署前,建议先在本地测试: |
| 108 | + |
| 109 | +```bash |
| 110 | +# 安装依赖 |
| 111 | +npm install |
| 112 | + |
| 113 | +# 开发模式 |
| 114 | +npm run dev |
| 115 | + |
| 116 | +# 生产构建测试 |
| 117 | +npm run build |
| 118 | +npm run preview |
| 119 | +``` |
| 120 | + |
| 121 | +## 🆘 常见问题 |
| 122 | + |
| 123 | +### Q: 部署失败,显示 "Build failed" |
| 124 | +**A**: 检查构建日志,通常是: |
| 125 | +- 缺少依赖:运行 `npm install` |
| 126 | +- TypeScript 错误:运行 `npm run build` 本地检查 |
| 127 | +- 环境变量问题:确保 `VITE_` 前缀 |
| 128 | + |
| 129 | +### Q: 域名解析超时 |
| 130 | +**A**: |
| 131 | +- 检查 DNS 记录是否正确(CNAME,不是 A 记录) |
| 132 | +- 等待 DNS 生效(通常 5-30 分钟) |
| 133 | +- 使用 `nslookup card.gudong.site` 或 https://dnschecker.org 检查 |
| 134 | + |
| 135 | +### Q: 资源 404(如 `/cards/xxx.txt`) |
| 136 | +**A**: |
| 137 | +- 确认 `vite.config.ts` 中 `viteStaticCopy` 配置正确 |
| 138 | +- 检查 `cards/` 文件夹是否已上传到 GitHub |
| 139 | +- 本地 `npm run build` 验证 `dist/cards/` 是否存在 |
| 140 | + |
| 141 | +### Q: 自定义域名不生效 |
| 142 | +**A**: |
| 143 | +- Cloudflare Dashboard 检查 DNS 记录是否已激活 |
| 144 | +- 清除浏览器缓存,或用隐私模式测试 |
| 145 | +- 检查域名 DNS 托管是否在 Cloudflare(需要修改 NS 记录) |
| 146 | + |
| 147 | +## 📚 更多资源 |
| 148 | + |
| 149 | +- [Cloudflare Pages 文档](https://developers.cloudflare.com/pages/) |
| 150 | +- [Vite 部署指南](https://vitejs.dev/guide/static-deploy.html#cloudflare-pages) |
| 151 | +- [Cloudflare DNS 管理](https://developers.cloudflare.com/dns/) |
| 152 | + |
| 153 | +--- |
| 154 | + |
| 155 | +**一旦部署成功,你的 inBox Note Hub 就可以被全世界访问了!🎉** |
0 commit comments