1

I'v defered all my CSS using

media="none" onload="if(media!='all')media='all'"

and images using yall.min.js

But PageSpeed Insights continues claiming it (and the images are visibles too)

How do I could optimize this?

Thanks, Ari

1 Answers1

0

I'm using JavaScript to asynchronously load external CSS files. Similar to old solutions where one would put scripts before the closing </body> tag, I'm now introducing data-attributes to create <link> tags, so they're made asynchronous automatically:

HTML

<div data-dom="link" data-href="@asyncCssBundle" data-media="screen"></div>
<div data-dom="link" data-href="@printBundle" data-media="print"></div>

jQuery

/**
  * Loads an array object of CSS files in async mode
  * stops render blocking CSS loading
  * good for fonts and @imports
  * @prop {object} asyncLinkTag data representation of a link tag on a div element
  */
function loadCssAsync() {
    var arr = [];

    $.each($('[data-dom="link"]'), function () {
        var el = $(this),
            link = $('<link />').prop({
                href: el.data('href'),
                rel: el.data('rel') || 'stylesheet',
                media: el.data('media') || 'screen'
            });

        arr.push(link);
    });

    $(document.head).append(arr);
}

$(function(){
    loadCssAsync();
});

Additionally I distinguish in SCSS between above the fold CSS and backgrounds, fonts, print, ...

If you really want to put an effort in it, the above the fold CSS can go inline. But I find it rather tricky to pull this off on a larger project. So I'm fine with async loading backgrounds and fonts. However, for fonts it's now clear they're loaded afterwards, similar to a FOUC or call it "Flash of Loading Font". It's good to do some research on a solid substitute font so it doesn't change your layout too much.

Tim Vermaelen
  • 6,869
  • 1
  • 25
  • 39
  • Hi Tim.This is a old project, I'm just the programer, not web designer, so I don't understand a lot of CSS and fonts. For the lazy load ofimages I used yall, if I check the source code, it works, but the pagespeed steel reports it as loaded at startup and the visible images too. For the CSS you sugest to load them by javascript? – Programador AR Jan 23 '19 at 16:40
  • How are you verifying it works with yall? Tbh, I'm not familiar with yall, so I've posted what I know best. If the library you chose does the same thing, you have a false positive on the page speed result. When loading "render blocking" CSS like `@import url()` and `background-image: url()` in CSS, you can distinguish all these CSS selectors to a later phase => JavaScript! Because there is no async loading in CSS yet. On a side note: I'm happy with a 30-100Kb minified render blocking CSS file just to avoid FOUC. If fonts and bg-images are included you have to count their weight as well. – Tim Vermaelen Jan 24 '19 at 09:31
  • So what exactly is page speed saying, you mention "claiming it"? First look at yall, doesn't say anything about tackling render blocking CSS. The solution above is for render blocking CSS only. For images alone you'll encounter "proper sizing", "proper encoding" and so on... feel free to tryout [gtmetrix](https://gtmetrix.com/) to match your page speed result. – Tim Vermaelen Jan 24 '19 at 09:37