简体   繁体   English

CSS优化和PageSpeed见解

[英]CSS Optimisation and PageSpeed Insights

I was running Google PageSpeed Insights on my website - www.gpsheatmap.com , and it suggested changing the loading of my stylesheets( https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery#example ) from - 我在我的网站上运行了Google PageSpeed Insights - www.gpsheatmap.com ,它建议更改我的样式表( https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery#example )的加载 -

<link href="/static/css/landing-page.css" rel="stylesheet">

To - 至 -

<script>
  var cb = function() {
    var l = document.createElement('link'); 
    l.rel = 'stylesheet';
    l.href = '/static/css/landing-page.css';
    var h = document.getElementsByTagName('head')[0]; 
    h.parentNode.insertBefore(l, h);
  };
  var raf = requestAnimationFrame || mozRequestAnimationFrame ||
      webkitRequestAnimationFrame || msRequestAnimationFrame;
  if (raf) raf(cb);
  else window.addEventListener('load', cb);
</script>

I tried this for my stylesheets and it visibly changed the loading so you would see the pre-css view, then a second later you would see the stylesheet applied. 我为我的样式表尝试了这个,它明显地改变了加载,所以你会看到pre-css视图,然后一秒钟你会看到应用样式表。 This was in firefox 这是在Firefox中

Should I disregard this approach, or can this be fixed? 我应该忽视这种方法,还是可以解决这个问题?

You should consider the critical path and also put all the necessary style in your head section so as to avoid the FOUC (just the style for contents above the fold ). 您应该考虑关键路径,并在head放置所有必要的样式,以避免FOUC(只是折叠上方内容的样式)。 This can be done either extracting the style by hand or — for larger sites — with an automatic task like critical-path-css-demo for gulp 这可以通过手动提取样式或者 - 对于较大的站点 - 使用自动任务来完成,例如关键路径-css-demo for gulp

Anyway if you choose to load all the stylesheets with javascript consider to still include them inside a <noscript> block, so they can be loaded also when JS is not available. 无论如何,如果您选择使用javascript加载所有样式表,请考虑将它们仍包含在<noscript>块中,以便在JS不可用时也可以加载它们。

<noscript>
    <link rel="stylesheet" ...>
</noscript>

As a further optimization for near-future browser (at this time it works only on Chrome Canary) it will be possible to early preload stylesheets using 作为对近期浏览器的进一步优化(此时它仅适用于Chrome Canary),可以使用早期预加载样式表

<link rel="preload" href="..." as="style">

and to create an async loader in a simpler way 并以更简单的方式创建异步加载器

<link rel="preload" href="..." as="style" onload="this.rel='stylesheet'">

Another interesting and recent approach is described by Jake Archibald and it's called "Multi-stage CSS loading" : it requires to load a small piece of CSS just before the markup that has to be styled and thus avoid the need for critical CSS, eg Jake Archibald描述了另一个有趣且最新的方法,它被称为“多阶段CSS加载” :它需要在标记之前加载一小块CSS,必须进行样式化,从而避免使用关键CSS,例如

<link rel="stylesheet" href="/site-header.css">
<header>…</header>

<link rel="stylesheet" href="/article.css">
<main>…</main>

<link rel="stylesheet" href="/comment.css">
<section class="comments">…</section>

The plan is for each to block rendering of subsequent content while the stylesheet loads, but allow the rendering of content before it. 该计划适用于在样式表加载时阻止后续内容的呈现,但允许在其之前呈现内容。 The stylesheets load in parallel, but they apply in series. 样式表并行加载,但它们是串联应用的。 This makes behave similar to <script src="…"></script> . 这使得行为类似于<script src="…"></script>

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

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