Elimante 渲染阻塞资源 [英] Elimante render-blocking resources
问题描述
我正在尝试优化 Web 以提高速度,并想询问有关消除阻止渲染的 CSS 和 JS 的问题.
I'm trying to optimalize web for speed and wanna ask about Eliminating render-blocking CSS and JS.
by JS 'm 只使用 async attr.- 可以说,把它扔到 flexslider、lightbox 之类的插件上......但我也应该将它与基本脚本一起使用吗?:
by JS 'm only using async attr. - lets say, throwin' it at plugins like flexslider, lightbox.. but should I also use this with the base scripts like ?:
<script src="https://cdnjs.cloudflare.com/.../4.5.3/js/bootstrap.min.js" async></script>
<script src="js/script.js" async></script>
每当我在某个脚本上添加异步并对其进行测试时,该 .js 脚本就不会运行 - 就好像没有链接一样.我究竟做错了什么?这足够了吗……?
Whenever i add async on some script and test it, that .js script won't just operate - as if not linked. What am I doing wrong? And is this enough ... ?
-by CSS - 尝试像这样改进它:
-by CSS - tring to impove it like this :
<link rel="preload" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" media="print" as="style" onload="this.onload=null;this.rel='stylesheet'" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous"><noscript><link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css"></noscript>
这减少了渲染 CSS 资源的时间,但再次使用它时,例如 fontawesome - 图标不会加载,就好像链接不存在一样......那么,请问这是渲染 CSS 的好方法吗?>
This cuts down the time of rendering CSS resources, but again when using it on e.g fontawesome - icons are not loaded as if link was't there...So, is this a good way of rendering CSS please ?
推荐答案
JavaScript
使用 async
时,您必须确保加载 order 不会影响您的实现.
JavaScript
When using async
you have to ensure that load order does not affect your implementation.
Async
基本上是说尽可能快地加载所有内容,我不在乎加载顺序".
Async
basically says "load everything as quickly as you can, I don't care about load order".
如果您没有考虑到这一点,那么最简单的解决方法是在您的 JavaScript 上使用 defer
而不是 async
.这基本上是说在 HTML 加载后加载所有内容,但请保持顺序,因为某些脚本依赖于其他脚本".
If you haven't accounted for this then the easiest fix is to use defer
on your JavaScript instead of async
. This basically says "load everything after the HTML has loaded but please keep the order as some scripts depend on others".
这总体上会稍微慢一些,但仍然修复了 JavaScript 被渲染阻塞的问题.
This will be slightly slower overall but still fix the JavaScript being render blocking.
您应该推迟所有脚本,除了对折叠操作必不可少的任何脚本(然后您应该将这些脚本内联在 标签中
,显然将其保持在最低限度).
You should defer all scripts, except any scripts that are essential for above the fold operations (and then you should inline those scripts in a <script>
tag in the <header>
, obviously keep this to a minimum).
渲染阻塞 CSS 是位于与首屏"内容相关的外部文件中的任何内容.
Render blocking CSS is anything sitting in an external file that relates to content "above the fold".
要完全理解这一点,您需要了解浏览器如何呈现内容,但本质上,如果您的 CSS 位于外部文件中,则无需滚动即可看到的任何内容(首屏"内容)都会延迟,因为它需要该信息知道如何呈现和布置.
To understand this fully you need to understand how the browser render things but in essence anything that is visible without scrolling ("above the fold" content) is delayed if your CSS is in an external file as it needs that information to know how to present and lay things out.
您需要做的是找到所有适用于您的首屏内容的样式,并将它们内联到页面
的 标签中代码>.再一次,这需要保持在最低限度,因此您可能需要使上面的折叠 CSS 自定义而不是使用引导程序......包括整个引导程序内联不会很好!
What you need to do is find all the styles that apply to your above the fold content and inline them in a <style>
tag in the page <header>
. Yet again this needs to be kept to a minimum so you may need to make the above the fold CSS custom rather than using bootstrap....including the whole of bootstrap inline would not be good!
然后所有其他样式都可以放在外部样式表中.
Then all other styles can sit in external style sheets.
这样一来,页面的 HTML 下载后,它就拥有了布局页面所需的一切,而无需等待任何其他请求.
This way the second the page's HTML is downloaded it has everything it needs to layout the page without waiting for any other requests.
图标的字体啊.我不会从可访问性和性能的角度探讨为什么这是一种不好的做法,因为我之前已经多次讨论过.
Ah fonts for icons. I won't go into why that is a bad practice from an accessibility and performance perspective as I have covered that numerous times before.
相反,我会简单地说,对于任何首屏"您应该将它们替换为 内联 SVG.这与内联 CSS 的原因相同,内联 SVG 不需要网络请求来呈现,因此在加载 HTML 后您的页面就可以显示.
Instead I will simply say that for any "above the fold" icons you should instead swap them for inline SVGs. This is for the same reason as inlining your CSS, inline SVGs do not need a network request to render so the second the HTML is loaded your page can be displayed.
这篇关于Elimante 渲染阻塞资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!