npm i -D critical
-
grunt-critical -
Gulp users should use Critical directly -
For Webpack use html-critical-webpack-plugin
-
Optimize a basic page with Gulp with a tutorial -
Optimize an Angular boilerplate with Gulp -
Optimize a Weather app with Gulp
import { generate } from 'critical' ;
generate ( {
// Inline the generated critical-path CSS
// - true generates HTML
// - false generates CSS
inline : true ,
// Your base directory
base : 'dist/' ,
// HTML source
html : '<html>...</ html>' ,
// HTML source file
src : 'index.html' ,
// Your CSS Files (optional)
css : [ 'dist/styles/main.css' ] ,
// Viewport width
width : one thousand and three hundred ,
// Viewport height
height : nine hundred ,
// Output results to file
target : {
css : 'critical.css' ,
html : 'index-critical.html' ,
uncritical : 'uncritical.css' ,
} ,
// Extract inlined styles from referenced stylesheets
extract : true ,
// ignore CSS rules
ignore : {
atrule : [ '@font-face' ] ,
rule : [ / some-regexp / ] ,
decl : ( node , value ) => / big-image\.png / . test ( value ) ,
} ,
} ) ;
generate ( {
inline : true ,
base : 'test/' ,
src : 'index.html' ,
target : 'index-critical.html' ,
width : one thousand and three hundred ,
height : nine hundred ,
} ) ;
generate ( {
base : 'test/' ,
src : 'index.html' ,
target : 'styles/main.css' ,
width : one thousand and three hundred ,
height : nine hundred ,
} ) ;
generate ( {
base : 'test/' ,
src : 'index.html' ,
target : 'styles/styles.min.css' ,
width : one thousand and three hundred ,
height : nine hundred ,
} ) ;
generate ( {
inline : true ,
base : 'test/' ,
src : 'index.html' ,
target : {
html : 'index-critical.html' ,
css : 'critical.css' ,
} ,
width : one thousand and three hundred ,
height : nine hundred ,
} ) ;
generate ( {
base : 'test/' ,
src : 'index.html' ,
width : one thousand and three hundred ,
height : nine hundred ,
inline : true
} , ( err , { css , html , uncritical } ) => {
// You now have critical-path CSS as well as the modified HTML.
// Works with and without target specified. ... } ) ;
generate ( {
base : 'test/' ,
src : 'index.html' ,
width : one thousand and three hundred ,
height : nine hundred
} ) . then ( ( ( { css , html , uncritical } ) ) => {
// You now have critical-path CSS as well as the modified HTML.
// Works with and without target specified. ... } ) . error ( err => { ... } ) ;
const { css , html , uncritical } = await generate ( {
base : 'test/' ,
src : 'index.html' ,
width : one thousand and three hundred ,
height : nine hundred ,
} ) ;
generate ( {
base : 'test/' ,
src : 'index.html' ,
target : {
css : 'styles/main.css' ,
} ,
dimensions : [
{
height : two hundred ,
width : five hundred ,
} ,
{
height : nine hundred ,
width : one thousand and two hundred ,
} ,
] ,
} ) ;
generate ( {
base : 'test/' ,
src : 'index.html' ,
target : {
css : 'styles/main.css' ,
} ,
ignore : {
atrule : [ '@font-face' ] ,
decl : ( node , value ) => / url\( / . test ( value ) ,
} ,
} ) ;
generate ( {
base : 'test/' ,
src : 'index.html' ,
target : {
css : 'styles/main.css' ,
} ,
rebase : {
from : '/styles/main.css' ,
to : '/folder/subfolder/index.html' ,
} ,
} ) ;
generate ( {
base : 'test/' ,
src : 'index.html' ,
target : {
css : 'styles/main.css' ,
} ,
rebase : ( asset ) => ` https://my-cdn.com ${ asset . absolutePath } ` ,
} ) ;
|
|
|
|
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
npm install -g critical
cat test/fixture/index.html | critical --base test/fixture --inline > index.critical.html
type test\fixture\index.html | critical --base test/fixture --inline > index.critical.html
critical test/fixture/index.html --base test/fixture > critical.css
import gulp from 'gulp' ;
import log from 'fancy-log' ;
import { stream as critical } from 'critical' ;
// Generate & Inline Critical-path CSS
gulp . task ( 'critical' , ( ) => {
return gulp
. src ( 'dist/*.html' )
. pipe (
critical ( {
base : 'dist/' ,
inline : true ,
css : [ 'dist/styles/components.css' , 'dist/styles/main.css' ] ,
} )
)
. on ( 'error' , ( err ) => {
log . error ( err . message ) ;
} )
. pipe ( gulp . dest ( 'dist' ) ) ;
} ) ;
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.
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.
-
Critical will automatically extract stylesheets from your HTML from which to generate critical-path CSS from, whilst other modules generally require you to specify this upfront. -
Critical provides methods for inlining critical-path CSS (a common logical next-step once your CSS is generated) -
Since we tackle both generation and inlining, we're able to abstract away some of the ugly boilerplate otherwise involved in tackling these problems separately.