Socket
Socket
Sign inDemoInstall

critical

Package Overview
Dependencies
126
Maintainers
1
Versions
109
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    critical

Extract & Inline Critical-path CSS from HTML


Version published
Maintainers
1
Created

Changelog

Source

v0.1.4 / 2014-07-11

  • Add note about sample project
  • Strap update
  • improve tests
  • Tweak to readme.
  • Update README.md
  • fix all the things
  • Attempt to fix builds
  • README.md: break long lines.
  • Lint fixes.
  • Whitespace normalization
  • package.json: Add missing properties.

Readme

Source

critical Build Status

Extract & Inline Critical-path CSS from HTML

A module by @addyosmani and @sindresorhus

Install

$ npm install --save critical

Usage

Include:

var critical = require('critical');

Generate and inline critical-path CSS

critical.generateInline({
    base: 'dist/',
    src: 'index.html',
    styleTarget: 'styles/main.css',
    htmlTarget: 'index-critical.html',
    width: 320,
    height: 480,
    minify: true
});

Generate critical-path CSS

Basic usage:

critical.generate({
    base: 'test/',
    src: 'index.html',
    dest: 'styles/main.css',
    width: 320,
    height: 480
});

Generate and minify critical-path CSS:

critical.generate({
    base: 'test/',
    src: 'index.html',
    dest: 'styles/styles.min.css',
    minify: true,
    width: 320,
    height: 480
});

Generate and return output via a callback:

critical.generate({
    base: 'test/',
    src: 'index.html',
    width: 320,
    height: 480
}, function (err, output) {
    // You now have critical-path CSS
    // Works with and without dest specified
});

Inline <style> / critical CSS from generation

Basic usage:

critical.inline({
    base: 'test/',
    src: 'index-critical.html',
    dest: 'inlined.html'
});

Minify and inline stylesheets:

critical.inline({
    base: 'test/',
    src: 'index-critical.html',
    dest: 'inlined-minified.html',
    minify: true
});

Inline and return output via a callback:

critical.inline({
    base: 'test/',
    src: 'index-critical.html'
}, function (err, output){
    // You now have HTML with inlined critical-path CSS
    // Works with and without dest specified
});

Options

NameTypeDescription
basestringBase directory in which the source and destination are to be written
srcstringLocation of the HTML source to be operated against
deststringLocation of where to save the output of an operation
widthinteger(Generation only) Width of the target viewport
heightinteger(Generation only) Height of the target viewport
minifybooleanEnable minification of CSS output
styleTargetstring(generateInline only) Destination for critical-path styles
htmlTargetstring(generateInline only) Destination for (critical-path CSS) style-inlined HTML

Why?

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.

FAQ

Are there any sample projects available using Critical?

Why, yes!. Take a look at this Gulp project which demonstrates using Critical to generate and inline critical-path CSS.

When should I just use Penthouse directly?

I recommend using Penthouse directly if your app has a large number of styles or stylesheets being dynamically injected into the DOM. Critical is best used when your page uses a fixed set of stylesheets as we can automatically scrape this for you, avoiding the overhead of passing known styles yourself manually to Penthouse.

License

Apache 2.0
Copyright 2014 Google Inc

Keywords

FAQs

Last updated on 11 Jul 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc