How do I load the Critical CSS first?

  • 0
    There is a multi-page service built on the django template engine. You need to increase the loading speed for the landing page of this page. I want to speed up using critical css. Please tell me the options for solving the problem. Everywhere I see solutions only for one-page sites, but how to make for a page that inherits several templates and therefore loads several css files?
    JavaScript Anonymous, Mar 20, 2019

  • 2 Answers
  • 0
    Use the Critical Path CSS Generator: penthouse or critical . Their principle of work is to launch the site in the client environment (headless browser) and select the styles necessary only for the first screen. Styles for multiple pages (different templates) can be merged into one file. I note that it is worth rendering styles at several resolutions - for desktops and mobiles, and also check for the presence of state styles and modal windows - by connecting only the generated critical css.

  • 0
    I do this:

    1) enable override for the site in devtools

    2) go through typical pages, using js to remove the elements below the first screen + extra padding

    3) save the resulting html as override

    4) analyze the page using coverage, export a report on it. Along the way, I change the resolution to the mobile version and back to cut the media rules.

    5) when all the typical pages have been passed, and reports for each of them are collected, I analyze the folder with them with a script that combines the used rules into one file, the unused ones into another. Keeping the original structure / amount of css.

    6) I create critical.css, which can be pushed over http2 or simply by adding a link preload, and I replace the rest of the source css of the project with cleaned up instances so as not to duplicate rules both in the source and in the critical one.

    7) I postpone loading the source using js, loadcss, for example

    The first 3 steps are risky, you can skip them, but with them the critical style turns out to be smaller, because coverage records the styles of all elements on the page.

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!