HTTP/2 使用 XHR 或 Fetch 的服务器推送 API

HTTP/2 server push with XHR or Fetch API

我想在使用浏览器 XMLHttpRequest 或 Fetch 请求特定 HTML 片段时 "push" 额外资源(主要是 HTML 片段,还有 CSS 和图像) API。我知道如何在服务器端构建 HTTP/2 响应,以及如何将 server-sent 资源推送到浏览器缓存以进行常规页面加载,但我的问题是关于 HTTP/2 服务器通过 JavaScript 以编程方式启动的推送响应,例如。我可以在 Ajax 请求上推送额外的资源,并在我在后续 XHR 请求中显式获取这些资源时指望从浏览器缓存中提供这些推送的资源吗(Cache-Control 和其他 headers对于有条件的请求允许)?如果已缓存,推送的资源响应是否会被取消?

can I push additional resources on an Ajax request, and count on those pushed resources being served from the browser cache when I fetch those resources in a subsequent XHR request explicitly (Cache-Control and other headers for conditional requests permitting)?

是的。但是 you need to be aware of various difficulties in push,特别是经过认证的连接与未经认证的连接。

Will pushed resource responses being cancelled if they are already cached?

虽然非常依赖于浏览器,但在取消请求返回时也可能已经下载。