Do current XHR implementations take advantage of HTTP/2?

允我心安 提交于 2019-12-21 07:17:57

问题


This may just highlight some misconception of mine, but I was curious:

If you had an HTTP/2 server running and wanted to make XHR requests in the browser to the server, would XHR automatically take advantage of the performance optimisations in headers and such that it provides?

Or would it ignore that functionality and operate as an HTTP request? If so, are there any things we can do to use the new optimisations in our requests?


回答1:


You don't need to do anything. If the server supports HTTP/2, XHR will use it. Header compression and such will kick in automatically.

Test it:

  • Open an HTTP/2 website in Chrome. You can use ours, to also checkout some cool stuff regarding HTTP/2 PUSH, AngularJS and RequireJS.
  • Open Devtools panel (F12), then the network panel, and then click XHR. Right click the headers row, and ensure you have the "Protocol" column enabled.
  • You should see "h2" in said column ( in our case, AngularJS is loading a template include through XHR).
  • While you are at that, notice that the server pushed the XHR request. Tell-tale signs of pushed responses are "provisional headers" instead of request headers, and abnormally small "download" and "waiting" times. We even add an extra-header to the response (x-shimmmercat-note:pushed-stream) . The browser adopted this request, so XHR requests not only will use HTTP/2 if available, but can in some cases use pushed resources.


来源:https://stackoverflow.com/questions/32592258/do-current-xhr-implementations-take-advantage-of-http-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!