• Resolved Vishal Lohar

    (@vlohar08)


    Hi,
    I have optimized my website using your plugin. I have to exclude a few CSS files to display everything correctly. But now the excluded CSS files are not loading in an asynchronous way.

    I just want to not minify and combine the selected CSS files but still load them in an asynchronous manner.

    Is this possible?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Loading CSS async is no good idea. In most cases it causes content/layout shifting and this is not good if you want to get a good Pagespeed score. Loading CSS async can have advantages, but needs a strategy for loading critical and uncritical css.

    Plugin Support qtwrk

    (@qtwrk)

    unfortunately not from plugin side

    but you can just simple edit the source code and add attribute media="print" onload="this.media='all'" to that CSS file , or use litespeed_buffer_after filter to edit the content on-the-fly.

    Thread Starter Vishal Lohar

    (@vlohar08)

    I found this in your documentation.

    function remove_broken_style( $content ) {
        return str_replace( '.content{opacity:0}', '', $content );
    }
    add_filter( 'litespeed_buffer_after', 'remove_broken_style', 0);
    

    This code will solve my issue and will load CSS in an asynchronous manner, right?

    Plugin Support qtwrk

    (@qtwrk)

    that’s what I have mentioned in my last reply about the litespeed_buffer_after filter

    you need to adapt a bit of it , to make it search your existing CSS file code and add async attribute into it

    Thread Starter Vishal Lohar

    (@vlohar08)

    Yeah, I got it.
    Can you please give me an example of CSS files? And if possible a guide or an article link where I can learn more about these filters.

    Plugin Support qtwrk

    (@qtwrk)

    you have the doc already , just need to learn a bit about PHP’s str_replace() function

    let’s say your page has a HTML code , like

    <link rel="stylesheet" href="/wp-content/themes/something/something.css">

    then you can replace it like

    return str_replace( 'something.css"', 'something.css" media="print" onload="this.media='all'"', $content );

    this will then turn into output code like

    <link rel="stylesheet" href="/wp-content/themes/something/something.css" something.css" media="print" onload="this.media='all'">

    didn’ test the code , but idea is like this

    Thread Starter Vishal Lohar

    (@vlohar08)

    Thanks!
    I don’t know how to use this filter but will learn and use it. Your example is not working for me but it’s a good reference for me.

    Plugin Support qtwrk

    (@qtwrk)

    you just need a quick class on PHP’s function str_replace

    basically , just find the CSS link , and replace it with async attribute in the code like my above example

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to load excluded CSS asynchronously?’ is closed to new replies.