使用 CORS Access-Control-Max-Age 缓存的内容
What is cached with CORS Access-Control-Max-Age
如果我响应一个包含 access-control-request-method:PUT
的 cors 请求,响应 header access-control-allow-origin
匹配原点并且只有 access-control-allow-method:PUT
和 access-control-max-age:7200
缓存了 2 小时并且总是 return 只有方法 PUT,或者如果说下一个请求是 access-control-request-method:POST,我是否能够只用请求的特定方法响应?
If I respond to a cors request that includes access-control-request-method:PUT
with response header access-control-allow-origin
matching the origin and just access-control-allow-method:PUT
and access-control-max-age:7200
will that be cached for 2 hours
是的,但在 Chrome 中它只会被缓存 10 分钟。 The Chrome sources hardcode an upper limit for it of 600 seconds(10 分钟)无论您指定多大的值。
and always return only method PUT or will I be able to respond with just the specific method(s) requested if say the next request was access-control-request-method:POST?
不会一直return只会PUT
;如果下一个请求有 access-control-request-method: POST
,那么将跳过缓存并向您的服务器发出新请求。
根据Fetch spec (the spec that currently defines browser behavior for the CORS protocol); specifically, browsers are required to cache preflights per-method, and to only use the cache when there’s a “method cache match”中的相关要求。
因此,您使用 PUT
方法的第一个请求会为 PUT
请求创建一个预检缓存条目,过期时间为 Access-Control-Max-Age
秒 — 任何下一个请求 POST
方法将为 POST
请求创建一个单独的预检缓存条目,其自身的有效期为 Access-Control-Max-Age
秒。
如果我响应一个包含 access-control-request-method:PUT
的 cors 请求,响应 header access-control-allow-origin
匹配原点并且只有 access-control-allow-method:PUT
和 access-control-max-age:7200
缓存了 2 小时并且总是 return 只有方法 PUT,或者如果说下一个请求是 access-control-request-method:POST,我是否能够只用请求的特定方法响应?
If I respond to a cors request that includes
access-control-request-method:PUT
with response headeraccess-control-allow-origin
matching the origin and justaccess-control-allow-method:PUT
andaccess-control-max-age:7200
will that be cached for 2 hours
是的,但在 Chrome 中它只会被缓存 10 分钟。 The Chrome sources hardcode an upper limit for it of 600 seconds(10 分钟)无论您指定多大的值。
and always return only method PUT or will I be able to respond with just the specific method(s) requested if say the next request was access-control-request-method:POST?
不会一直return只会PUT
;如果下一个请求有 access-control-request-method: POST
,那么将跳过缓存并向您的服务器发出新请求。
根据Fetch spec (the spec that currently defines browser behavior for the CORS protocol); specifically, browsers are required to cache preflights per-method, and to only use the cache when there’s a “method cache match”中的相关要求。
因此,您使用 PUT
方法的第一个请求会为 PUT
请求创建一个预检缓存条目,过期时间为 Access-Control-Max-Age
秒 — 任何下一个请求 POST
方法将为 POST
请求创建一个单独的预检缓存条目,其自身的有效期为 Access-Control-Max-Age
秒。