preconnect & more

Resource Hints

preconnect 出现在 w3 组织 16 年制订 《Resource Hints》

《Resource Hints》是一套预加载机制的 w3 标准化产物,在没有标准化之前,我们常用诸如 XMLHttpRequest 等手段来预加载我们未来会使用到的资源。

《Resource Hints》文档通过 <link> 标签,定义了4个新的属性值。

PreConnect

文档一定定义了4个新属性值,今天由于某个案例,我们只讨论 preconnect。

The user agent should attempt to initiate a preconnect and perform the full connection handshake (DNS+TCP for HTTP, and DNS+TCP+TLS for HTTPS origins) whenever possible, but is allowed to elect to perform a partial handshake (DNS only for HTTP, and DNS or DNS+TCP for HTTPS origins), or skip it entirely, due to resource constraints or other reasons.

The optimal number of connections per origin is dependent on the negotiated protocol, users current connectivity profile, available device resources, global connection limits, and other context specific variables. As a result, the decision for how many connections should be opened is deferred to the user agent.

preconnect 用于客户端与目标资源服务器,在请求资源前,预先完成 DNS 查询 + TCP 握手(如果是 HTTPS,会把 TLS 握手也预先完成),当客户端需要请求目标资源的时候,下一个 TCP 首包就可以直接发送 HTTP 的有效载荷,省去握手耗时。

需要注意规范仅仅是一个建议行为,具体处理细节由浏览器根据当时环境决定。

Caniuse

qq20161206-02x

恩,Chrome、Firefox、Android 5.x 开始支持这个特性。 继续阅读“preconnect & more”