子域名的DNS预取

21

我需要单独预取子域吗?

比如说,当我有 <link rel="dns-prefetch" href="//example.com"> 这个标签时,我还需要为 //static.example.com 再添加一个标签吗?

2个回答

35

我进行了以下测试:首先创建了一个简单的HTML页面。

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="dns-prefetch" href="//example.com/">     
  </head>
  <body>
    <a href="http://example.com">Test link</a>
    <a href="http://sub.example.com">Test link 2</a>
  </body>
</html>

我拥有 DNS 域名服务器的域和子域。然后我清除了 DNS 缓存,并在 Firefox 私人窗口中打开了此页面。我观察了我的 DNS 域名服务器的日志,发现只有针对 "example.com" 的请求被发出,而没有任何子域的请求。

接着,我进行了以下更改:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="dns-prefetch" href="//example.com/">     
    <link rel="dns-prefetch" href="//sub.example.com/">
  </head>
  <body>
    <a href="http://example.com">Test link</a>
    <a href="http://sub.example.com">Test link 2</a>
  </body>
</html>

我又清除了DNS缓存,并在Firefox的私密窗口中打开了此页面。现在我观察到我们为域名和其子域名发出的DNS请求。

因此,我可以得出结论 - 是的,您必须单独预取子域名。


谢谢你的回答! - damian

8
您需要单独预取每个子域名。
这是DNS的运作方式。您询问名称,它会回答,它对“子域”一无所知,它只是一个名称。 nslookup google.com 仅针对google.com提供答案,没有子域名。 nslookup www.google.com 仅提供www.google.com,没有顶级域名。

5
没错,但是dns-prefetch是浏览器相关的功能,浏览器在理论上可以检查页面并预取您在dns-prefetch中设置的某个域名的子域名。当然,浏览器并没有这样做,但是还是有可能的。 - Evk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接