极简版在线摄影作品集 Cloudflare Pages 版本。
- 前端
script.js不再包含 Unsplash API Key - 使用 Cloudflare Pages Functions 作为代理层
- Unsplash Key 存在 Cloudflare Pages Secret 中
- 前端只请求:
/api/profile/api/photos/api/photo/:id
.
├─ index.html
├─ style.css
├─ script.js
├─ functions/
│ └─ api/
│ ├─ profile.js
│ ├─ photos.js
│ └─ photo/
│ └─ [id].js
├─ wrangler.toml
└─ .gitignore
编辑 script.js:
const username = "YOUR_UNSPLASH_USERNAME";只保留用户名,不再需要写 access key。
把整个项目上传到 GitHub / GitLab。
在 Cloudflare Pages 里连接你的 Git 仓库。
这是静态站 + Functions 项目,通常无需额外 build 命令。
- Build command: 留空
- Build output directory:
.
在 Cloudflare Pages 项目设置中添加:
UNSPLASH_ACCESS_KEY=你的 Unsplash Access Key
保存后重新部署即可。
如果你安装了 Wrangler,可以本地预览:
npm install -g wrangler
wrangler pages dev .本地如果要测试 secret,可使用 .dev.vars,例如:
UNSPLASH_ACCESS_KEY=your_real_access_key注意:不要把 .dev.vars 提交到 Git。