Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • https://developers.google.com/speed/pagespeed/insights

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!

Is there anyway to keep this resource from blocking my browser and force it to wait until it is downloaded?

like image 973
Flame_Phoenix Avatar asked Aug 10 '14 15:08

Flame_Phoenix


People also ask

What is render blocking JavaScript and CSS?

Render blocking JavaScript and CSS are files that prevent a website from displaying a web page before loading these files. Every WordPress site has a theme and plugins that add JavaScript and CSS files to the front-end of your website. These scripts can increase your site’s page load time, and they can also block rendering of the page.

How to render above-the-fold content without waiting for blocking resources?

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. 26952.default.include.c9d602.css is available currently on .css list, but i see this error.

Do you want to eliminate render-blocking JavaScript and CSS in WordPress?

If you test your website on Google PageSpeed insights, then you will likely see a suggestion to eliminate render-blocking scripts and CSS. However, it does not provide any details on how to do that on your WordPress site.

What are “render blocking resources”?

Some resources that have to be loaded can actually slow down the display of the web page. These resources are called “Render Blocking Resources” and this article will show you a few tactics you can use to reduce the number of render blocking resources on your website and how to apply them manually or using the SiteGround Optimizer plugin.


1 Answers

You can do it with 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.


Or, as dandavis points out, you could just use a style element at the end of body, just before the closing </body> tag:

<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.

The advantages to this over using JavaScript are:

  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

  2. It works even if the user has JavaScript disabled.

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. (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...)

like image 64
T.J. Crowder Avatar answered Oct 18 '22 17:10

T.J. Crowder