Pragma: no-cache 遗留兼容
no-cache 是 HTTP/1.0 的缓存控制指令,在现代 HTTP/1.1+ 中已由 Cache-Control 取代,但浏览器强刷仍会发送。
#tech / dev / frontend
#type / concept
#status / evergreen
#platform / browser
#tech / network
[!info] related notes
Pragma: no-cache 遗留兼容
一句话定义
Pragma: no-cache 是 HTTP/1.0 时代的缓存控制指令,等价于 Cache-Control: no-cache。在 HTTP/1.1+ 中已被 Cache-Control 取代,但浏览器在强刷时仍会发送它以兼容旧代理。
来源
- 定义在 HTTP/1.0(RFC 1945)
- HTTP/1.1(RFC 2616)引入
Cache-Control后,Pragma被降级为兼容角色 - RFC 9111 明确:
Pragma在响应中没有定义的缓存行为
现在哪里还能看到
浏览器强刷请求
Ctrl+Shift+R(Mac: Cmd+Shift+R)触发的请求通常同时包含:
Cache-Control: no-cache
Pragma: no-cache
这是为了兼容 HTTP/1.0 代理——如果中间有不理解 Cache-Control 的老代理,Pragma: no-cache 仍然能让它不返回缓存。
遗留服务器响应
一些老服务器或旧版 Web 框架可能在响应中返回 Pragma: no-cache 而非 Cache-Control。RFC 9111 建议:当请求中出现 Pragma: no-cache 时,缓存应将其视为 Cache-Control: no-cache。
RFC 9111 的指导
When the
Pragma: no-cachedirective is present in a request, caches SHOULD treat the request as ifCache-Control: no-cachewere present.
但注意:
- 请求中的
Pragma: no-cache→ 有缓存语义,等价于Cache-Control: no-cache - 响应中的
Pragma: no-cache→ 在 HTTP/1.1+ 中没有定义的缓存行为,不应依赖它
迁移建议
| 场景 | 建议 |
|---|---|
| 服务器响应 | 只用 Cache-Control,不要用 Pragma |
| 中间件/代理 | 同时设置 Cache-Control 和 Pragma 兼容老代理 |
| 前端代码 | 不需要手动设置 Pragma,浏览器会自动处理 |
和其他缓存指令的关系
HTTP/1.0: Pragma: no-cache
↓ 替代
HTTP/1.1+: Cache-Control: no-cache
↓ 细分
Cache-Control: no-store(不存储)
Cache-Control: no-cache(可缓存但必须验证)
常见误解
- “
Pragma和Cache-Control效果一样” → 只在请求中等价;响应中的Pragma在 HTTP/1.1+ 没有缓存语义 - “需要在响应中同时设置
Pragma和Cache-Control” → 不需要,现代浏览器只认Cache-Control - “
Pragma: no-cache表示不缓存” → 错误,它等价于no-cache(可缓存但必须验证),不是no-store