重用与后端服务器的 http 连接 - 网络获取 API
Reuse http connection with backend server - web fetch API
我在 example.com
上托管我的网站 我在 graphql.example.com
上托管我的 graphQL 后端,它通过 CORS 服务 JSON。据我了解,对 window.fetch('https://graphql.example.com/', ...)
的每个请求都需要创建一个新连接。我知道浏览器能够为通过 http2 对 example.com
进行的多个资产重用相同的连接。例如,如果我的网页有 <img>
s example.com/a.jpg
和 example.com/b.jpg
我只需要支付设置一次连接的费用。
有没有办法以类似的方式重用连接
- (a) 与
window.fetch()
- (b) 到另一个需要 CORS 的域
我错了。通过 CORS 对 graphql.example.com
的所有提取请求都可以重复使用相同的 TCP 连接。您可以通过 running this example and checking the Connection ID in chrome dev tools .
查看
然而,CORS 请求将使用与 non-CORS 对同一服务器的请求不同的连接。
例如,假设您通过图像标签请求 example.com/a.jpg
和 example.com/b.jpg
,它们将通过一个连接在没有 CORS 的情况下提供。
对 fetch('graphql.example.com')
的所有 CORS 请求都将通过不同的连接提供服务。
我在 example.com
上托管我的网站 我在 graphql.example.com
上托管我的 graphQL 后端,它通过 CORS 服务 JSON。据我了解,对 window.fetch('https://graphql.example.com/', ...)
的每个请求都需要创建一个新连接。我知道浏览器能够为通过 http2 对 example.com
进行的多个资产重用相同的连接。例如,如果我的网页有 <img>
s example.com/a.jpg
和 example.com/b.jpg
我只需要支付设置一次连接的费用。
有没有办法以类似的方式重用连接
- (a) 与
window.fetch()
- (b) 到另一个需要 CORS 的域
我错了。通过 CORS 对 graphql.example.com
的所有提取请求都可以重复使用相同的 TCP 连接。您可以通过 running this example and checking the Connection ID in chrome dev tools
然而,CORS 请求将使用与 non-CORS 对同一服务器的请求不同的连接。
例如,假设您通过图像标签请求 example.com/a.jpg
和 example.com/b.jpg
,它们将通过一个连接在没有 CORS 的情况下提供。
对 fetch('graphql.example.com')
的所有 CORS 请求都将通过不同的连接提供服务。