DNS 预读取是一项使浏览器主动去执行域名解析的功能。其范围包括文档的所有链接,无论是图片的,CSS 的,JavaScript 等其他用户能够点击的 URL。
因为预读取会在后台执行,所以 DNS 很可能在链接对应的东西出现之前就已经解析完毕。这能够减少用户点击链接时的延迟。
Background
DNS 请求需要的带宽非常小,但是延迟却有点高,这点在手机网络上特别明显。预读取 DNS 能让延迟明显减少一些,例如用户点击链接时。在某些情况下,延迟能减少一秒钟。
The implementation of this prefetching in Firefox allows domain name resolution to occur in parallel with (instead of in serial with) the fetching of actual page content. By doing this, the high latency domain name resolution process doesn't cause delays during the process of fetching content.
Page load times -- especially on mobile networks -- can be measurably improved this way. If the domain names for images can be resolved in advance of the images being requested, pages that load a number of images can see a load time improvement of 5% or more.
Configuring prefetching in the browser
In general, you don't need to do anything to manage prefetching. However, the user may wish to disable prefetching. This can be done by setting the network.dns.disablePrefetch
preference to true
.
Also, by default, prefetching of embedded link hostnames is not performed on documents loaded over HTTPS. This can be changed by setting the network.dns.disablePrefetchFromHTTPS
preference to false
.
Controlling prefetching from content
Content providers have some control over the prefetching process as well. This is compatible with how Google Chrome handles DNS prefetching control.
打开和关闭 DNS 预读取
First of all, a server can opt out of DNS prefetching by serving content with the x-dns-prefetch-control:
HTTP header set to "off
".
This can also be done from individual documents, using the http-equiv
attribute on the <meta>
element, like this:
<meta http-equiv="x-dns-prefetch-control" content="off">
您可以通过将 content 的参数设置为“on”来改变设置。
Forcing lookup of specific hostnames
The content provider can force the lookup of specific hostnames without providing specific anchors using that hostname by using the rel
attribute on the <link>
element with a link type of dns-prefetch
:
<link rel="dns-prefetch" href="https://www.spreadfirefox.com/">
在这个例子中,Firefox将预解析域名"www.spreadfirefox.com"。
Similarly, the link element can be used to resolve hostnames without providing a complete URL, by preceding the hostname with two slashes:
同样,link元素可以用来解析 hostnames 没有提供完整的URL的,但主机名前要有双斜杠:
<link rel="dns-prefetch" href="//www.spreadfirefox.com">
Forced prefetching of hostnames might be useful, for example, on the home page of a site to force pre-resolution of domain names that are referenced frequently throughout the site even though they're not used on the home page itself. This will improve overall site performance even though the performance of the home page may not be affected.