Giter Site home page Giter Site logo

damianmcnulty / critical-path-css-demo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from addyosmani/critical-path-css-demo

0.0 2.0 0.0 237 KB

Above-the-fold CSS generation + inlining using Critical & Gulp

Home Page: ""

License: Apache License 2.0

ApacheConf 72.59% HTML 22.79% JavaScript 3.36% CSS 1.26%

critical-path-css-demo's Introduction

critical-path-css-demo

Generate and inline critical-path CSS example using Critical.

Live demo of before and after critical-path CSS generation and inlining.

PageSpeed Insights results of before and after

Before:

After:

WebPageTest results

Before:

After:

Great. So, what are your recommendations?

Use Critical for generating and inlining your critical-path CSS and loadCSS to async load in your site-wide styles.

Why is critical-path CSS important?

CSS is required to construct the render tree for your pages and JavaScript will often block on CSS during initial construction of the page. You should ensure that any non-essential CSS is marked as non-critical (e.g. print and other media queries), and that the amount of critical CSS and the time to deliver it is as small as possible.

Why should critical-path CSS be inlined?

For best performance, you may want to consider inlining the critical CSS directly into the HTML document. This eliminates additional roundtrips in the critical path and if done correctly can be used to deliver a “one roundtrip” critical path length where only the HTML is a blocking resource.

Installation

$ cd critical-path-css-demo
$ npm install && bower install

Generating and inlining critical-path CSS

Note: There are two build commands available. This allows you to compare the difference between the output of a normal build and the output with critical-path CSS.

The default (minify, concat) build for the project can be run with:

$ gulp

The complete (critical-path) build can be run with:

$ gulp critical

This performs the normal build, then generates and inlines critical-path CSS for the page. It automatically async loads in the site-wide styles using loadCSS as part of the workflow offered by the module.

Tutorial

Follow the next few lines to install and scaffold a project using Yeoman and Gulp:

$ mkdir myapp && cd myapp
$ npm install -g yo generator-gulp-webapp
$ yo gulp-webapp

# Select Bootstrap and say no to Modernizr & Sass

You should now have a valid set of source files, including a Gulpfile.js.

The first thing we're going to do is install the Critical module which can generate and inline your critical-path CSS for you.

This can be installed as follows:

$ cd myapp
$ npm install critical --save-dev

Great. Next, add a reference to Critical at the top of your Gulpfile.js:

var critical = require('critical');

We can now use it in our build process. Let's write a new task called critical.

Our workflow for critical-path CSS is to first run a normal build, which will generate the optimized CSS (dist/styles/main.css) and resources needed for our app. We pass the build command as the second argument below:

gulp.task('critical', ['build'], function () {

});

Next, we'll add in our configuration for the critical module:

gulp.task('critical', ['build'], function (cb) {
    critical.generate({
        inline: true,
        base: 'dist/',
        src: 'index.html',
        dest: 'dist/index-critical.html',
        minify: true,
        width: 320,
        height: 480
    });
});

That's it. You can now run gulp critical to generate a complete build where dist/index-critical.html will contain your final output files. Above I've passed in a width and height which represent the viewports I'm targeting with my above-the-fold CSS. minify ensures that the inlined CSS gets minified.

Disclaimer

Note that this sample project is just that - a sample. It does not demonstrate how well these tools and techniques work on a complex site nor a site making heavy use of dynamic styles. Your mileage may vary and I encourage testing the tools available before making a decision about whether Critical makes sense for you.

critical-path-css-demo's People

Contributors

addyosmani avatar ahmedelgabri avatar ammarcodes avatar bezoerb avatar robinboehm avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.