2017 © Pedro Peláez
 

library critical-css

A Laravel package for generating and using inline critical-path CSS.

image

krisawzm/critical-css

A Laravel package for generating and using inline critical-path CSS.

  • Tuesday, March 14, 2017
  • by kalfheim
  • Repository
  • 3 Watchers
  • 40 Stars
  • 8,864 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 21 Forks
  • 9 Open issues
  • 12 Versions
  • 2 % Grown

The README.md

critical-css Build Status

A Laravel package for generating and using inline critical-path CSS., (*1)

CriticalCss, (*2)

Why?

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., (*3)

More information:, (*4)

  • https://github.com/addyosmani/critical/blob/master/README.md#why
  • https://developers.google.com/web/fundamentals/performance/

Table of Contents, (*5)

Installation

1) Install the Critical npm package

This package is used to extract critical-path CSS from an HTML document., (*6)

From your project's base path, run:, (*7)

$ npm install critical --save

Alternatively, install it globally:, (*8)

$ npm install -g critical

2) Require the package

Next, you'll need to require the package using Composer:, (*9)

From your project's base path, run:, (*10)

$ composer require krisawzm/critical-css

3) Configure Laravel

Service Provider

Add the following to the providers key in config/app.php:, (*11)

``` php 'providers' => [ Alfheim\CriticalCss\CriticalCssServiceProvider::class, ];, (*12)


#### Console To get access to the `criticalcss:clear` and `criticalcss:make` commands, add the following to the `$commands` property in `app/Console/Kernel.php`: ``` php protected $commands = [ \Alfheim\CriticalCss\Console\CriticalCssMake::class, \Alfheim\CriticalCss\Console\CriticalCssClear::class, ];

Config

Generate a template for the config/criticalcss.php file by running:, (*13)

$ php artisan vendor:publish

Note: Descriptions for the config options are only present in the config file, not in this readme. Click here to open the config file on GitHub., (*14)

Usage

Before getting started, I highly recommend reading through the config/criticalcss.php file. That will give you a good idea of how this all works., (*15)

Generating critical-path CSS

Providing everything is set up and configured properly, all you need to do in order to generate a fresh set of critical-path CSS files, is running the following command:, (*16)

$ php artisan criticalcss:make

This will generate a unique file for each of the URIs (routes) provided., (*17)

See this commit for a diff of the implementation., (*18)

Using critical-path CSS with Blade templates

The service provider provides a new Blade directive named @criticalCss., (*19)

Simply call that directive, passing a route as the only argument, like so:, (*20)

``` html , (*21)

... @criticalCss('some/route')

If no argument is passed, the current route will be used, however, I highly recommend always passing a specific route. And of course, make sure to asynchronously load the full CSS for the page using something like loadCSS (https://github.com/filamentgroup/loadCSS). Full example (using Elixir to generate the URL for the CSS file, which or course is optional): ``` html <html> <head> ... @criticalCss('some/route') <script> !function(a){"use strict";var b=function(b,c,d){var g,e=a.document,f=e.createElement("link");if(c)g=c;else{var h=(e.body||e.getElementsByTagName("head")[0]).childNodes;g=h[h.length-1]}var i=e.styleSheets;f.rel="stylesheet",f.href=b,f.media="only x",g.parentNode.insertBefore(f,c?g:g.nextSibling);var j=function(a){for(var b=f.href,c=i.length;c--;)if(i[c].href===b)return a();setTimeout(function(){j(a)})};return f.onloadcssdefined=j,j(function(){f.media=d||"all"}),f};"undefined"!=typeof module?module.exports=b:a.loadCSS=b}("undefined"!=typeof global?global:this); loadCSS('{{ elixir('css/app.css') }}'); </script> </head> </html>

For multiple views, you may wrap @criticalCss in a @section, then @yield the section in a master view., (*22)

A demo

I made a simple demo using this Bootstrap theme. It's a fairly simple theme, and it does not have any major performance issues, but yet, implementing inline critical-path CSS did improve performance., (*23)

Demo repo: https://github.com/kalfheim/critical-css-demo, (*24)

See this commit for a diff of the implementation., (*25)

PageSpeed Insights results

          | Mobile        | Desktop
Before | |
After | |

A note on Laravel 5.0 compatibility

On Laravel 5.0, you must set 'blade_directive' => false in the config. This is not recommended, but because Custom Directives were introduced in 5.1, it has to be done., (*26)

This will require adding the following to the aliases key in config/app.php:, (*27)

``` php 'aliases' => [ 'Critical' => Alfheim\CriticalCss\Facades\Critical::class, ];, (*28)


In your Blade views, you'll now be able to do the following instead of `@criticalCss('some/route')`: ``` php {!! Critical::css('some/route') !!}

The Versions

20/05 2016

v2.0.1

2.0.1.0

A Laravel package for generating and using inline critical-path CSS.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristoffer Alfheim

laravel css performance

20/03 2016

v2.0.0

2.0.0.0

A Laravel package for generating and using inline critical-path CSS.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristoffer Alfheim

laravel css performance

15/12 2015

v1.0.6

1.0.6.0

A Laravel package for generating and using inline critical-path CSS.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristoffer Alfheim

laravel css performance

12/11 2015

v1.0.5

1.0.5.0

A Laravel package for generating and using inline critical-path CSS.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristoffer Alfheim

laravel css performance

09/11 2015

v1.0.4

1.0.4.0

A Laravel package for generating and using inline critical-path CSS.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristoffer Alfheim

laravel css performance

09/11 2015

v1.0.3

1.0.3.0

A Laravel package for generating and using inline critical-path CSS.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristoffer Alfheim

laravel css performance

08/11 2015

v1.0.2

1.0.2.0

A Laravel package for generating and using inline critical-path CSS.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristoffer Alfheim

laravel css performance

07/11 2015

v1.0.1

1.0.1.0

A Laravel package for generating and using inline critical-path CSS.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristoffer Alfheim

laravel css performance

05/11 2015

v1.0.0

1.0.0.0

A Laravel package for generating and using inline critical-path CSS.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kristoffer Alfheim

laravel css performance