最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Do browsers not send a CORS preflight request for certain headers added automatically by the browser? - Stack Overf

programmeradmin4浏览0评论

I am using JavaScript's Fetch API like so: fetch(URL)

My browser (Chrome) seems to automatically add If-None-Match and If-Modified-Since headers to the request. Although these headers should trigger preflight requests according to the fetch spec (I think), no preflight requests are being sent. Is this expected when the browser automatically adds such headers rather than me adding it to the fetch function?

I noticed that, when I add If-None-Match and If-Modified-Since headers manually to the fetch function, the browser sends a preflight request:

fetch(cdnUrl, {
  "headers": {
    "If-None-Match": "blah"
  }
})

I am using JavaScript's Fetch API like so: fetch(URL)

My browser (Chrome) seems to automatically add If-None-Match and If-Modified-Since headers to the request. Although these headers should trigger preflight requests according to the fetch spec (I think), no preflight requests are being sent. Is this expected when the browser automatically adds such headers rather than me adding it to the fetch function?

I noticed that, when I add If-None-Match and If-Modified-Since headers manually to the fetch function, the browser sends a preflight request:

fetch(cdnUrl, {
  "headers": {
    "If-None-Match": "blah"
  }
})
Share Improve this question edited Feb 14 at 0:20 David Jeong asked Feb 14 at 0:12 David JeongDavid Jeong 131 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

https://developer.mozilla./en-US/docs/Web/HTTP/CORS#simple_requests says that to be classed as a "simple" request - and thus avoid triggering a pre-flight request, a request must meet several conditions, including this one:

Apart from the headers automatically set by the user agent (for example, Connection, User-Agent, or the other headers defined in the Fetch spec as a forbidden header name), the only headers which are allowed to be manually set are those which the Fetch spec defines as a CORS-safelisted request-header, which are:

  • Accept
  • Accept-Language
  • Content-Language
  • Content-Type (please note the additional requirements below)
  • Range (only with a simple range header value; e.g., bytes=256- or bytes=127-255)

Note the phrase

apart from the headers automatically set by the user agent

As you've noticed, headers set automatically by the browser do not count as triggering a pre-flight request, and this is expected behaviour.


IMHO it would be a little unfair to developers if headers set by the browser triggered a pre-flight request - this could potentially break existing code implementations if a browser update suddenly caused extra headers to be send in a fetch request, which then caused a pre-flight request that the server was not expecting. I don't know for sure, but that - plus the fact that such headers are not user-modifiable and therefore represent far less of a problem - would strike me as a good reason for this behaviour.

发布评论

评论列表(0)

  1. 暂无评论