背景:favicon 此前只有 Cache-Control: max-age(仅浏览器缓存),
CF 边缘不缓存 Worker 响应 → 每次请求都进 Worker → 第一句就是 KV.get。
友圈页按友链数放大,KV 读量被显著放大。
★ 关键纠错:zone 级 Cache Rules 对 Worker 响应**完全无效**。
原因是 Worker 位于 zone 缓存之前("Workers sits in front of cache"),
且 Worker 直接生成响应时不发 fetch 子请求,cache status 只能是 none/unknown。
查证 CF 官方文档后确认,正确方案是 [cache] enabled = true(Workers Cache,
2026-07 上线,需 wrangler >= 4.69)。
改动:
- wrangler.toml: 新增 [cache] enabled = true;compatibility_date 提到 2026-07-24
- package.json: wrangler ^3.99 → ^4.147,workers-types ^4 → ^5(peer 要求)
- index.ts: 抽出 dispatchRequest;新增 ensureCachePolicy 兜底
★ 这一步是必须的:Workers Cache 遵循 RFC 9111 含**启发式缓存**,
没带 Cache-Control 的响应也可能被缓存。实测 /api/v2/comments 开启后
立刻被缓存(HIT, age:29)→ 用户发的新评论看不见。
现在白名单式兜底:已显式声明可缓存的(favicon 的 s-maxage)保留,
其余一律补 no-store。
验证(中转机实测,已清空 zone 的无效 cache rule):
- /api/favicon → MISS → HIT,age 递增(Worker 不再执行,KV 读归零)
- /api/v2/comments → BYPASS ×3,cache-control: no-store
- /api/v2/conf → BYPASS,no-store
- /api/v2/healthz → BYPASS,no-store
22 lines
910 B
JSON
22 lines
910 B
JSON
{
|
|
"name": "artalk-cf",
|
|
"version": "0.1.0",
|
|
"private": true,
|
|
"description": "Artalk API v2 compatible comment backend on Cloudflare Workers + D1",
|
|
"scripts": {
|
|
"dev": "wrangler dev",
|
|
"deploy": "wrangler deploy",
|
|
"typecheck": "tsc --noEmit",
|
|
"db:init:local": "wrangler d1 execute artalk-cf --local --file=./schema.sql",
|
|
"db:init:remote": "wrangler d1 execute artalk-cf --remote --file=./schema.sql",
|
|
"import:gen": "python tools/import_artrans.py",
|
|
"selftest": "tsc tools/selftest.ts --outDir .selftest-build --module commonjs --target es2022 --moduleResolution node --types ./node_modules/@cloudflare/workers-types --skipLibCheck --esModuleInterop --noEmit false && node .selftest-build/tools/selftest.js",
|
|
"tail": "wrangler tail"
|
|
},
|
|
"devDependencies": {
|
|
"@cloudflare/workers-types": "^5.20261006.1",
|
|
"typescript": "^5.7.0",
|
|
"wrangler": "^4.147.0"
|
|
}
|
|
}
|