简体   繁体   English

如何防止 CSS 阻塞我的网站?

[英]How to keep CSS from render-blocking my website?

I am trying to optimize the loading speed of my mobile webpage, and for that effect I am using the website:我正在尝试优化移动网页的加载速度,为此我正在使用该网站:

This website evaluates my source and tells me what I can improve.这个网站评估我的来源并告诉我我可以改进的地方。 In my website I download a font:在我的网站上,我下载了一种字体:

<link href="//fonts.googleapis.com/css?family=Open+Sans:400,700" rel="stylesheet">

and aparently this is blocking the rendering of my page.显然这阻止了我的页面的呈现。 Now, if this was JavaScript I could use the async word in the tag and it would fix the problem, but this is not a javascript file I am loading!现在,如果这是 JavaScript,我可以在标签中使用async词,它会解决问题,但这不是我正在加载的 javascript 文件!

Is there anyway to keep this resource from blocking my browser and force it to wait until it is downloaded?无论如何,是否可以阻止此资源阻止我的浏览器并强制它等待下载?

You can do it with JavaScript:你可以用 JavaScript 做到:

<script>
(function() {
    var link = document.createElement('link');
    link.rel = "stylesheet";
    link.href = "//fonts.googleapis.com/css?family=Open+Sans:400,700";
    document.querySelector("head").appendChild(link);
})();
</script>

The font will be loaded out-of-band with the main rendering.字体将随主渲染带外加载。 Of course, that means there will be a visual change when the font finishes loading...and if the user has JavaScript disabled, it won't load the font at all.当然,这意味着当字体完成加载时会有视觉上的变化……如果用户禁用了 JavaScript,它根本不会加载字体。


Or, as dandavis points out, you could just use a style element at the end of body , just before the closing </body> tag:或者,正如 dandavis 指出的那样,您可以在body末尾,就在结束</body>标记之前使用style元素:

<style>
@import "//fonts.googleapis.com/css?family=Open+Sans:400,700"
</style>

That's valid HTML now (as of the 20170808 draft of HTML 5.2), but I'd never met a browser that cared about it if you placed style in body even before it was made valid.现在这是有效的 HTML(从 HTML 5.2 的 20170808 草案开始),但我从来没有遇到过一个浏览器会关心它,如果你在body放置style甚至在它生效之前。

The advantages to this over using JavaScript are:与使用 JavaScript 相比,这样做的优点是:

  1. In theory, the browser's prefetch scanner might find the style element and start the download earlier (although this isn't particularly likely if you put the JavaScript in head ), and理论上,浏览器的预取扫描器可能会找到style元素并更早开始下载(尽管如果您将 JavaScript 放在head ,这不太可能),并且

  2. It works even if the user has JavaScript disabled.即使用户禁用了 JavaScript,它也能工作。

Alternately, you could just move your link element to the end of body , but at present, that's invalid and the scoped attribute doesn't (yet?) seem to apply.或者,您可以将link元素移动到body的末尾,但目前,这是无效的,并且scoped属性(还?)似乎不适用。 (Why make it apply to style and not link[rel=stylesheet] ? I have no idea, and perhaps it's simply a matter of not having got there yet...) (为什么让它适用于style而不是link[rel=stylesheet] ?我不知道,也许这只是还没有到达那里的问题......)

<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.5.18/webfont.js"></script>
<script>
 WebFont.load({
    google: {
      families: ['Open Sans:400,700,400italic,700italic']
    }
  });
</script>

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

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