简体   繁体   English

异步加载以消除渲染阻塞js和css?

[英]Asynchronously load to eliminate render-blocking js & css?

I've just spent a really long time googling... and only find half answers everywhere. 我只是花了很长时间谷歌搜索...到处找到半答案。 I am using the google page speed insights to improve my website and it tells me to asynchronously load my javascript. 我正在使用谷歌页面速度见解来改进我的网站,它告诉我异步加载我的JavaScript。 I found a couple of codes, but they didn't explain how to load MORE than one js file AND how to load the css as well. 我找到了几个代码,但他们没有解释如何加载多个js文件以及如何加载css。 I also couldn't find anywhere where it tells me in what order to load it. 我也找不到它告诉我加载它的顺序的任何地方。 Can anyone help? 有人可以帮忙吗?

NOTE: I DID try to move the js to the footer, but then my mobile menu no longer works (which uses the expand.js file) 注意:我DID尝试将js移动到页脚,但我的移动菜单不再有效(使用expand.js文件)

The Javascript files I need to asynchronously load are: 我需要异步加载的Javascript文件是:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script language="javascript" type="text/javascript" src="js/h5.js"></script>
    <script language="javascript" type="text/javascript" src="js/expand.js"></script>

My CSS: 我的CSS:

    <link rel="stylesheet" type="text/css" href="style.css">

Asynchronous loading of scripts can get pretty complicated. 脚本的异步加载可能变得非常复杂。 Have you tried using the async attribute? 您是否尝试过使用async属性? Eg: 例如:

<script async src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script async language="javascript" type="text/javascript" src="js/h5.js"></script>
<script async language="javascript" type="text/javascript" src="js/expand.js"></script>

However, this can cause undesired race conditions . 但是,这会导致不希望的竞争条件 All this means is the rest of your site is not going to wait for these three JavaScript files to load before loading other resources. 所有这些意味着您的网站的其余部分不会等到加载其他资源之前加载这三个JavaScript文件。 This may or may not improve the speed of your site depending on what is contained, and can produce some wonky results in terms of dependency management which you'll have to account for in your scripts. 这可能会也可能不会提高您网站的速度,具体取决于所包含的内容,并且在依赖关系管理方面会产生一些不可思议的结果,您必须在脚本中考虑这些结果。

It is usually recommended by the big G to create two small files: a CSS and a JavaScript file to be included in your <head/> which contains all the styles and logic for the above-the-fold content (or better yet: inline them, even though this increases the DOM size). 大G通常建议创建两个小文件:一个CSS和一个JavaScript文件,包含在<head/> ,其中包含上述内容的所有样式和逻辑(或更好的内联:内联)他们,即使这增加了DOM的大小)。 Source . 来源

How To Asynchronously load Javascript File? 如何异步加载Javascript文件?
Well to load javascript file you just need to include "async" attribute in <script> tag ie 好吧加载javascript文件你只需要在<script>标签中包含“async”属性即

<script src="your path" async></script>

Now this script will be downloaded in background while it will not cause JS rendering issue. 现在这个脚本将在后台下载,但不会导致JS渲染问题。
Note: If you're using some jquery plugin ie image zoom or any thing else, it will take an extra time to load but after that it will work properly. 注意:如果你正在使用一些jquery插件,即图像缩放或任何其他东西,它将需要一个额外的时间来加载,但之后它将正常工作。

Why to use "DEFER" keyword with "ASYNC" ? 为什么要在“ASYNC”中使用“DEFER”关键字?

You can also use defer attribute along with async attribute. 您还可以将defer属性与async属性一起使用。 <script> tag with async attribute forces file to be downloaded in background and execute as soon it is downloaded. 带有异步属性的<script>标记强制文件在后台下载,并在下载后立即执行。 but async with defer attribute forces <script> tag to execute once entire site is loaded. 但是,与defer属性异步会强制<script>标记在加载整个站点后执行。

<script src="" async defer></script>

How To Asynchronously load CSS file? 如何异步加载CSS文件? If you want to load your CSS file asynchronously first you have to place provided <script> in your head file and then you can use loadCSS() function to load you CSS file asynchronously. 如果要首先异步加载CSS文件,则必须在头文件中放置提供的<script> ,然后可以使用loadCSS()函数异步加载CSS文件。

<script>
// https://github.com/filamentgroup/loadCSS
!function(e){"use strict"
var n=function(n,t,o){function i(e){return f.body?e():void setTimeout(function(){i(e)})}var d,r,a,l,f=e.document,s=f.createElement("link"),u=o||"all"
return t?d=t:(r=(f.body||f.getElementsByTagName("head")[0]).childNodes,d=r[r.length-1]),a=f.styleSheets,s.rel="stylesheet",s.href=n,s.media="only x",i(function(){d.parentNode.insertBefore(s,t?d:d.nextSibling)}),l=function(e){for(var n=s.href,t=a.length;t--;)if(a[t].href===n)return e()
setTimeout(function(){l(e)})},s.addEventListener&&s.addEventListener("load",function(){this.media=u}),s.onloadcssdefined=l,l(function(){s.media!==u&&(s.media=u)}),s}
"undefined"!=typeof exports?exports.loadCSS=n:e.loadCSS=n}("undefined"!=typeof global?global:this)
</script>

Now you just have to use loadCSS function. 现在你只需要使用loadCSS功能。

<script>
 loadCSS("https://www.yourCSSLinkHere.com");
</script>

In this way you can load your CSS and JS file Asynchronously. 通过这种方式,您可以异步加载CSS和JS文件。


You can use HTTP/2.0 to overcome speed issue because HTTP/2.0 allows your file to be downloaded parallel but HTTP/1.0 won't allow your file to be download parallel, in other way HTTP/1.0 follows FIFO(First In First Out) rule. 您可以使用HTTP / 2.0来克服速度问题,因为HTTP / 2.0允许您的文件并行下载,但HTTP / 1.0不允许您的文件并行下载,否则HTTP / 1.0遵循FIFO(先进先出)规则。

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

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