简体   繁体   English

使用PageSpeed消除jQuery的渲染阻止JavaScript

[英]Using PageSpeed to eliminate render-blocking JavaScript for jQuery

I have jQuery added at the bottom of the page. 我在页面底部添加了jQuery。 However, when I run my site on pagespeed insights (Mobile), I get the error: 但是,当我在Pagespeed Insights(Mobile)上运行我的网站时,出现以下错误:

Eliminate render-blocking JavaScript and CSS in above-the-fold content Your page has 2 blocking script resources and 1 blocking CSS resources. 在首屏内容中消除渲染阻止JavaScript和CSS您的页面有2个阻止脚本资源和1个阻止CSS资源。

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. 尝试延迟或异步加载阻塞资源,或直接在HTML中内联这些资源的关键部分。

See: http://learnyourbubble.com and https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Flearnyourbubble.com&tab=mobile 请参阅: http : //learnyourbubble.comhttps://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Flearnyourbubble.com&tab=mobile

However, the jQuery is added at the bottom of the page. 但是,jQuery已添加到页面底部。 So it should be below the fold. 因此,它应该在折叠以下。

How can I remove this error? 如何清除此错误?

It has to do with your font files. 它与您的字体文件有关。

Look at request 19 and 20 in the waterfall. 查看瀑布中的请求19和20。 These font files are considered CSS. 这些字体文件被认为是CSS。

Notice how first paint (green vertical line) does not happen until after the font files are loaded? 请注意,在加载字体文件之后,如何才不会出现第一个绘画(绿色垂直线)?

Then notice the 15 JS files were loaded prrior to the font (CSS) files. 然后请注意,这15个JS文件已分别加载到字体(CSS)文件中。

That is what Google is taking about. 这就是Google正在采取的行动。

Having 16 JS files is beyond excessive. 拥有16个JS文件是远远不够的。

Try this: Disable JavaScript in your Browser. 尝试以下操作:在浏览器中禁用JavaScript。 Notice the only change is in the menu header. 请注意,唯一的变化是在菜单标题中。 Is 16 JS files worth that? 16个JS文件值得吗? I think not. 我觉得不是。

在此处输入图片说明

This article should explain a lot of what's happening: https://varvy.com/pagespeed/critical-render-path.html 本文应该解释很多事情: https : //varvy.com/pagespeed/critical-render-path.html

In short though the problem is that chrome will need to load your jquery and your foundation javascript to give the initial render of the page. 简而言之,问题是chrome需要加载您的jquery和基础javascript以提供页面的初始呈现。 This is why its blocking. 这就是为什么它被阻止。 Just because the javascript comes after the html, does not mean that the html can be displayed yet. 仅仅因为javascript出现在html之后,并不意味着html可以显示。 The rendering of the DOM is still going to block while the jquery/foundation is being loaded because chrome thinks they're vital to the page being displayed correctly. 加载jQuery / Foundation时,DOM的呈现仍会阻塞,因为chrome认为它们对于正确显示页面至关重要。 Pagespeed complains on these particularly because they're large. Pagespeed抱怨这些特别是因为它们很大。 To alleviate this problem, there are a few things you can do, some of them detailed in the article above, some of them in here https://developers.google.com/speed/docs/insights/BlockingJS . 为减轻此问题,您可以做一些事情,其中​​一些在上面的文章中详细介绍,其中一些在此处https://developers.google.com/speed/docs/insights/BlockingJS中 The easiest way to tell chrome that these scripts are not vital and can be loaded "below the fold" is to add a defer or async tag to them. 告诉chrome这些脚本不是至关重要的,可以在“折叠之下”加载的最简单方法是在它们上添加一个deferasync标签。

Have you tried loading async 您是否尝试加载异步

Make JavaScript Asynchronous By default JavaScript blocks DOM construction and thus delays the time to first render. 使JavaScript异步默认情况下,JavaScript会阻止DOM的构建,因此会延迟首次渲染的时间。 To prevent JavaScript from blocking the parser we recommend using the HTML async attribute on external scripts. 为了防止JavaScript阻止解析器,我们建议在外部脚本上使用HTML async属性。 For example: 例如:

<script async src="my.js">

See Parser Blocking vs. Asynchronous JavaScript to learn more about asynchronous scripts. 请参阅解析器阻止与异步JavaScript,以了解有关异步脚本的更多信息。 Note that asynchronous scripts are not guaranteed to execute in specified order and should not use document.write . 请注意,不保证异步脚本以指定的顺序执行,并且不应使用document.write Scripts that depend on execution order or need to access or modify the DOM or CSSOM of the page may need to be rewritten to account for these constraints. 取决于执行顺序或需要访问或修改页面的DOM或CSSOM的脚本,可能需要重写以解决这些限制。

I see an error calling foundation() but I will assume that you have removed it to rule it out, but it could be that this same call happens prior to load. 我看到一个调用Foundation()的错误,但我将假定您已删除它以排除它,但可能是同一调用在加载之前发生。 Try to always enclose your code like: 尝试始终将您的代码括起来,例如:

(function($) {
   // DOM ready
})(jQuery);

请尝试使用延迟标签,它对我有用。

<script src="filename.js" defer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM