返回行业内容

Vary 这个最难的 HTTP 头:作者评论 Cloudflare 上线了对 Vary 头的支持——过去 Cloudflare 对非图片内容忽略 Vary,把「按 accept 返回 HTML 或 JSON」的做法放在缓存后面根本没法部署

We just shipped support for the ugliest part of HTTP: Vary

评分 46

导读

Simon Willison 在 Hacker News 就「Cloudflare 上线了对 HTTP 中 Vary 头的支持」发帖评论,讲的是这个头此前为什么难用。他多年希望 Cloudflare 支持 Vary:过去 Cloudflare 除图片外忽略 Vary,于是「按 accept 分别返回 HTML/JSON」的做法套在缓存后有把 JSON 版本误返回给要 HTML 用户的风险。作者另说明自己最终决定不再用那种模式,而是用带 .json 后缀、可预测返回的 URL。

主体与标签

#部署/工程#现象/趋势

正文

My comment on We just shipped support for the ugliest part of HTTP: Vary — Hacker News.

I've been wanting this from Cloudflare for years .

The classic problem here is if you do that thing where user agents that send "accept: text/html" get HTML, while user agents that don't get JSON or some other format.

This used to be impossible to deploy behind Cloudflare caching, because they ignored the Vary header on anything other than images - so you risked caching the JSON version and then serving it up to someone who was expecting HTML.

(Independent of the Cloudflare feature I ended up deciding never to use that pattern, because I prefer having URL that predictably returns HTML or JSON - I add a .json suffix to my apps to serve JSON instead.)

打开原文 ↗