html - 为什么预连接资源提示不起作用?

标签 html web-performance preconnect

我在 crenshaw.dev/demo/hints.html 创建了一个测试页面浏览器提示请求 dns 预取并预连接到 mac9416.com。

<!DOCTYPE html>
<html lang="en-US">
    <head>
        <title>Test resource hints</title>
        <link rel="dns-prefetch" href="//mac9416.com">
        <link rel="preconnect" href="https://mac9416.com" crossorigin>
    </head>
    <body>
        Lorem ipsum dolor sit amet,
        <!-- a bunch more to delay page loading -->
        Duis dignissim gravida commodo.

        <script src="https://mac9416.com/blah.js"></script>
    </body>
</html>

但是according to WebPageTest ,直到 index.html 完全加载后才会连接到 mac9416.com。

WebPageTest waterfall chart showing mac9416.com is connected to only after all markup has been downloaded and parsed, instead of immediately after the head element is parsed

为什么 Chrome 在 <head> 之后没有立即连接到 mac9416.com解析了吗?

更新:

根据接受的答案和评论,我 wrote up修复的解释。

The crossorigin attribute, when used with rel="preconnect", doesn't describe where the target origin is but rather what kind of assets will be downloaded from that origin. If the assets use CORS, crossorigin is needed. If CORS won't be used, crossorigin should be omitted. If both types of assets will be present, two resource hints are necessary.

最佳答案

只需删除命名错误的 crossorigin 参数即可。

<link rel="preconnect" href="https://mac9416.com">

此参数不会告诉浏览器该域是外部域(它已经知道)。它告诉浏览器预先打开一个特殊的“CORS”连接,字体或 XHR 请求需要它,但脚本、样式表或图像不需要。

关于html - 为什么预连接资源提示不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55520102/

相关文章:

performance - Google Chrome Lighthouse 报告中最大的内容绘制错误

iphone - 使用 HTML5 在 iPhone 上播放 Shoutcast 流

html - html5中的UDP地址

javascript - href 内的 flash 元素

html - 如何使用 CSS 和 HTML 永久更改 Material Icon?

javascript - Web Components 渲染性能

javascript - 建立网站时,如何将多个小图片组合成一个大图片?

google-pagespeed - 为什么 PageSpeed Insights 和 Search Console 的累积布局偏移不同?