What is the function of the “Vary: Accept” HTTP header?

前端 未结 4 758
予麋鹿
予麋鹿 2020-11-28 19:19

I use PHP to generate dynamic Web pages. As stated on the following tutorial (see link below), the MIME type of XHTML documents should be \"application/xhtml+xml\" when $_SE

4条回答
  •  旧巷少年郎
    2020-11-28 19:34

    There are actually a significant number of new features coming soon (and already in Chrome) that make the Vary header extremely useful. For example, consider Client Hinting. When used in connection with images, for example, client hinting allows a server to optimize resources such as images depending on:

    • Image Width
    • Viewport Width
    • Type of encoding supported by browser (think WebP)
    • Downlink (essentially network speed)

    So a server which supports those features would set the Vary header to indicate that.

    Chrome advertises WebP support by setting "image/webp" as part of the Vary header for each request. So a server might rewrite an image as WebP if the browser supports it, so the proxy would need to check the header so as to not cache a WebP image and then serve it to a browser that doesn't support WebP. Obviously, if your server doesn't do that, it wouldn't matter. So since the server's response varies on the Accept request header, the response must include that so as not to confuse proxies:

    Vary: Accept
    

    Another example might be image width. On a mobile browser the Width header might be quite small for a responsive image, in comparison with what it would be if viewed from a desktop browser. So in that case Width would be added to the the Vary header is essential for proxy to not cache the small mobile version and serve it to desktop browsers, or vice versa. In that case, the header might include:

    Vary: Accept, Width
    

    Or in the case that a server supported all of the client hinting specs, the header would be something like:

    Vary: Accept, DPR, Width, Save-Data, Downlink
    

提交回复
热议问题