对来自同一域的 http 和 https 资源使用 rel=preconnect

Using rel=preconnect for both http and https resources from the same domain

我目前正在从事一个网络项目,正在寻求有关为子资源预连接域的建议。

我的假设是,理想情况下,所有子资源都应该使用相同的协议从域中提供服务,从而减少到服务器的往返行程。但是,在我正在使用的代码的某些区域,一些资源是通过 http 加载的,而在其他区域,资源是通过 https 加载的。

为了这个问题的目的,请假设我无法访问代码的某些部分。

为了获得预连接的好处,(在现在和与其他人联络以使用相同路线之间的时间),最好包括:

<link rel="preconnect" href="http://www.example.com" />
<link rel="preconnect" href="https://www.example.com" />

或者使用以下相对协议URL:

<link rel="preconnect" href="//www.example.com" />

您需要预先连接到这两种协议,因为它们被浏览器视为两个不同的域:

<link rel="preconnect" href="http://www.example.com" />
<link rel="preconnect" href="https://www.example.com" />

如果你想走得更远,我还建议使用 dns-prefetch,用于当前无法处理 preconnect 的浏览器。所以它看起来像:

<link rel="preconnect" href="http://www.example.com" />
<link rel="dns-prefetch" href="http://www.example.com" />
<link rel="preconnect" href="https://www.example.com" />
<link rel="dns-prefetch" href="https://www.example.com" />