http 307 重定向可缓存性

问题描述 投票:0回答:1

307重定向可以被浏览器缓存吗?

我有一个简单的服务,它使用

307 Temporary Redirect
状态代码将所有请求重定向到另一个 URL。

curl localhost:8081 -v
*   Trying 127.0.0.1:8081...
* Connected to localhost (127.0.0.1) port 8081 (#0)
> POST / HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/8.0.1
> Accept: */*
>
< HTTP/1.1 307 Temporary Redirect
< location: http://localhost:8080
< cache-control: private, max-age=60
< access-control-allow-credentials: true
< vary: origin
< vary: access-control-request-method
< vary: access-control-request-headers
< content-length: 0
< date: Wed, 30 Aug 2023 17:41:17 GMT

根据我对 rfc9111 的理解,浏览器应该根据

max-age
标头中提供的
cache-control
缓存重定向响应。但是,我观察到浏览器总是发出两个请求:第一个请求到原始 URL,然后到重定向的 URL。 (实际上是 3 因为
cors
,但这不是问题点。)

我已经使用此代码检查了 chrome 和 Firefox:

const options = {
  cache: 'force-cache',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: '{"some:"data"}'
};

for (let step = 0; step < 1000; step++) {
  fetch('http://localhost:8081/', options)
    .then(response => response.json())
    .then(response => console.log(response))
    .catch(err => console.error(err));
}
fetch-api browser-cache http-caching
1个回答
0
投票

Fetch
spec没有提到重定向缓存。 RFC 9111 指定客户端“可以存储”带有
cache-control: private
指令的响应。这意味着浏览器或客户端可以缓存响应,但没有义务这样做。

© www.soinside.com 2019 - 2024. All rights reserved.