gpt4 book ai didi

html - 以保证顺序加载 标签异步

转载 作者:行者123 更新时间:2023-11-28 03:11:01 25 4
gpt4 key购买 nike

运行后Google PageSpeed , 建议我使用 asyncdefer对于我的 CSS 内容

Eliminate render-blocking JavaScript and CSS in above-the-fold content

Your page has 6 blocking CSS resources. This causes a delay in rendering your page.

None of the above-the-fold content on your page could be rendered without waiting for the following resources to load. Try to defer or asynchronously load blocking resources, or inline the critical portions of those resources directly in the HTML.

所以我正在探索 async <link> 上的属性标签

<link rel="stylesheet" href="{{asset "css/bootstrap.min.css"}}">
<link rel="stylesheet" href="{{asset "css/font-awesome.min.css"}}">
<link rel="stylesheet" type="text/css" href="{{asset "css/screen.css"}}">
<link rel="stylesheet" type="text/css" href="{{asset "css/highlight_styles/rainbow.css"}}">

现在如果我添加 async属性为以上 <link>标签,我担心 CSS 无法按顺序加载。 css order does matter async 不能保证订单还有哪些选择?

我考虑过的选项:

  • 使用 defer属性保证顺序,但似乎不支持 <link> Tag Documentation

  • 收件人 cat我所有的 CSS 放在一起,这样我就可以保证顺序。但是我想知道是否有办法用多个 async 解决这个问题<link>标签,因为即使使用 gulp/grunt,每次这样做都会很痛苦。

  • 我可能可以编写一些花哨的 javascript 来执行此操作,但这似乎有点过分了。 (JS -> CSS -> JS 可能会减少任何节省的时间)。


一些引用资料:

根据 webkit似乎为 defer 保留了顺序但不适用于 async :

Both async and defer scripts begin to download immediately without pausing the parser and both support an optional onload handler to address the common need to perform initialization which depends on the script. The difference between async and defer centers around when the script is executed. Each async script executes at the first opportunity after it is finished downloading and before the window’s load event. This means it’s possible (and likely) that async scripts are not executed in the order in which they occur in the page. The defer scripts, on the other hand, are guaranteed to be executed in the order they occur in the page. That execution starts after parsing is completely finished, but before the document’s DOMContentLoaded event.

CSS 订单事项:Does the order of css stylesheet definitions matter?

W3C 文档: <link> Tag只支持 async没有提到defer

最佳答案

经过进一步研究,我的一些假设似乎是不正确的。

link tag with type="import" 可以是异步的,但不是 type="text/css"

我能找到的最好的替代方法是使用一个名为 loadCSS 的库,下面是一个如何使用它的例子:

<head>
<script src="loadCSS.min.js"></script>
<script>
// load a file
loadCSS("Styles-V6.min.css");
</script>
</head>

关于html - 以保证顺序加载 <link> 标签异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30538419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com