HTTP 缓存笔记
每次配缓存都要现查,干脆写一篇笔记。
两类缓存
| 类型 | 行为 | 相关头 |
|---|---|---|
| 强缓存 | 不发请求,直接用本地 | Cache-Control: max-age=…, Expires |
| 协商缓存 | 发请求问服务器变了没 | ETag / If-None-Match, Last-Modified / If-Modified-Since |
实践建议
- 带 hash 的静态资源:
max-age=31536000, immutable - HTML:
no-cache(每次都协商),或很短的 max-age no-store才是真的不缓存,no-cache只是要重新验证
最大的误区:
no-cache不是「不缓存」。