jquery 如何延迟加载css文件(谷歌速度洞察)

0qx6xfy6  于 2023-08-04  发布在  jQuery
关注(0)|答案(2)|浏览(110)

我想在Google Page Speed Insights上获得100/100分。但它一直告诉我一些css文件正在阻止折叠上方的内容。如何确保在加载主要内容后加载这些文件?这样它就不会再出现在Page Speed Insights中。
我尝试使用jquery异步加载文件,但这样页面速度工具仍然会弹出消息。
我尝试了以下方法:

<script>
var loadMultipleCss = function(){
    //load local stylesheet
    loadCss('myawesomestyle.css');

    //load Bootstrap from CDN
    loadCss('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css');

    //load Bootstrap theme from CDN
    loadCss('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css');
}

var loadCss = function(cssPath){
    var cssLink = document.createElement('link');
    cssLink.rel = 'stylesheet';
    cssLink.href = cssPath;
    var head = document.getElementsByTagName('head')[0];
    head.parentNode.insertBefore(cssLink, head);
};

//call function on window load
window.addEventListener('load', loadMultipleCss);
</script>

字符串
当然是用我自己的文件路径。
但对于Google PageSpeed Insights来说,这并不起作用。

yvfmudvl

yvfmudvl1#

你能分享你正在优化的网站的链接吗?
您确定您的页面没有缓存在某处吗?
有两种方法对我有效:
A)您可以只将样式表标签放在结束的</html>标签之后。
B)另一种技术是将以下链接标记放入head部分:

<link rel="preload" id="stylesheet" href="/assets/css/below.css" as="style" onload="this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="/assets/css/below.css"></noscript>

字符串
方法B的缺点是,并非所有浏览器都支持在link标记中使用rel=preload,因此您需要包含以下多边形填充:

<script>
/*! loadCSS. [c]2017 Filament Group, Inc. MIT License */
!function(a){"use strict";var b=function(b,c,d){function e(a){return h.body?a():void setTimeout(function(){e(a)})}function f(){i.addEventListener&&i.removeEventListener("load",f),i.media=d||"all"}var g,h=a.document,i=h.createElement("link");if(c)g=c;else{var j=(h.body||h.getElementsByTagName("head")[0]).childNodes;g=j[j.length-1]}var k=h.styleSheets;i.rel="stylesheet",i.href=b,i.media="only x",e(function(){g.parentNode.insertBefore(i,c?g:g.nextSibling)});var l=function(a){for(var b=i.href,c=k.length;c--;)if(k[c].href===b)return a();setTimeout(function(){l(a)})};return i.addEventListener&&i.addEventListener("load",f),i.onloadcssdefined=l,l(f),i};"undefined"!=typeof exports?exports.loadCSS=b:a.loadCSS=b}("undefined"!=typeof global?global:this);

/*! loadCSS rel=preload polyfill. [c]2017 Filament Group, Inc. MIT License */
!function(a){if(a.loadCSS){var b=loadCSS.relpreload={};if(b.support=function(){try{return a.document.createElement("link").relList.supports("preload")}catch(b){return!1}},b.poly=function(){for(var b=a.document.getElementsByTagName("link"),c=0;c<b.length;c++){var d=b[c];"preload"===d.rel&&"style"===d.getAttribute("as")&&(a.loadCSS(d.href,d,d.getAttribute("media")),d.rel=null)}},!b.support()){b.poly();var c=a.setInterval(b.poly,300);a.addEventListener&&a.addEventListener("load",function(){b.poly(),a.clearInterval(c)}),a.attachEvent&&a.attachEvent("onload",function(){a.clearInterval(c)})}}}(this);
</script>


我写了一个an article关于优化一个页面从pagespeed 59到100,你可以看到之前和之后的以下分支:
之前:https://github.com/storyblok/storyblok-express-boilerplate/blob/unoptimized/views/layouts/main.hbs
之后:https://github.com/storyblok/storyblok-express-boilerplate/blob/master/views/layouts/main.hbs

rjee0c15

rjee0c152#

您可以 AJAX / jQuery在页面加载后加载CSS。这将允许您随时加载不重要的CSS,例如,使用setTimeout
示例如下:

document.addEventListener('DOMContentLoaded', () => {
    setTimeout($('head').append("<'link rel="stylesheet" href="your_css_file.css" type="text/css" />"), 3000); 
});

字符串
编辑:请删除链接词('link)前的'字符。它被插入到那里,以便stackoverflow可以显示代码。否则它会显示“... apend(“<>"...)。
在上面的例子中,CSS只会在页面完全加载后3秒加载。
我在我的一些页面中做到了这一点,并在不影响用户界面或布局的情况下获得了100%的页面速度,因为我在该方法中使用的CSS文件是次要的,如sweetalert。

相关问题