CDN 缓存一直是个迷,最近终于把 middleware 和 headers 那一套摸顺了。关键是用 NextResponse.next() 加自定义 headers。
const res = NextResponse.next();
res.headers.set("Cache-Control", "s-maxage=60, stale-while-revalidate=600");
return res;
配合 ISR,效果是:边缘缓存 60s,过期后回源同时返回旧数据 600s。命中率高,回源率低。
但要注意:
- 带 Cookie 的请求别缓存(登录态串号)。
- POST 一律
Cache-Control: no-store。 - 缓存 key 要把 Accept-Language 算进去。
大家的 CDN 缓存策略是怎么配的?