getmaple / critical-css
A Laravel package for generating and using inline critical-path CSS.
Requires
- php: >=5.5.9
- illuminate/console: ~5.0
- illuminate/contracts: ~5.0
- illuminate/http: ~5.0
- illuminate/support: ~5.0
- illuminate/view: ~5.0
- symfony/process: ~4.0
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~6.0
- dev-master
- 2.1
- v2.0.1
- v2.0.0
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-dependabot/npm_and_yarn/tough-cookie-and-critical--removed
- dev-dependabot/npm_and_yarn/lodash-and-critical-4.17.21
- dev-dependabot/npm_and_yarn/got-and-critical-12.6.0
- dev-dependabot/npm_and_yarn/postcss-and-critical-7.0.39
- dev-dependabot/npm_and_yarn/minimatch-3.1.2
- dev-dependabot/npm_and_yarn/minimist-and-mkdirp-and-extract-zip-1.2.8
- dev-dependabot/npm_and_yarn/cacheable-request-and-critical-10.2.7
- dev-dependabot/npm_and_yarn/http-cache-semantics-and-critical-4.1.1
- dev-dependabot/npm_and_yarn/decode-uri-component-0.2.2
- dev-dependabot/npm_and_yarn/css-what-and-critical-2.1.3
- dev-laravel-56
- dev-laravel-55
- dev-laravel-54
- dev-laravel-53
This package is auto-updated.
Last update: 2024-12-08 07:08:53 UTC
README
A Laravel package for generating and using inline critical-path CSS.
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.
More information:
- https://github.com/addyosmani/critical/blob/master/README.md#why
- https://developers.google.com/web/fundamentals/performance/
Table of Contents
Installation
1) Install the Critical npm package
This package is used to extract critical-path CSS from an HTML document.
From your project's base path, run:
$ npm install critical --save
Alternatively, install it globally:
$ npm install -g critical
2) Require the package
Next, you'll need to require the package using Composer:
From your project's base path, run:
$ composer require krisawzm/critical-css
3) Configure Laravel
Service Provider
Add the following to the providers
key in config/app.php
:
'providers' => [ Alfheim\CriticalCss\CriticalCssServiceProvider::class, ];
Console
To get access to the criticalcss:clear
and criticalcss:make
commands, add the following to the $commands
property in app/Console/Kernel.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:
$ 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.
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.
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:
$ php artisan criticalcss:make
This will generate a unique file for each of the URIs (routes) provided.
See this commit for a diff of the implementation.
Using critical-path CSS with Blade templates
The service provider provides a new Blade directive named @criticalCss
.
Simply call that directive, passing a route as the only argument, like so:
<html> <head> ... @criticalCss('some/route') </head> </html>
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> <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.
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.
Demo repo: https://github.com/kalfheim/critical-css-demo
See this commit for a diff of the implementation.
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.
This will require adding the following to the aliases
key in config/app.php:
'aliases' => [ 'Critical' => Alfheim\CriticalCss\Facades\Critical::class, ];
In your Blade views, you'll now be able to do the following instead of @criticalCss('some/route')
:
{!! Critical::css('some/route') !!}