GithubHelp home page GithubHelp logo

attila / critical-css Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 1.0 25 KB

Detects Above the Fold styles on sites and assembles a stylesheet of them.

License: MIT License

JavaScript 100.00%
critical-css above-the-fold critical-path

critical-css's Introduction

This repository is now archived. Note that plenty of other techniques emerged over the years, most of which are much better suited to your potential performance optimisation needs.


Critical CSS

Build Status

Detects Above the Fold styles on sites and assembles a stylesheet of them.

Opens any web page on an accessible URL and runs a parser to detect above the fold CSS in the headless browser.

How it works

The module opens the supplied URL and runs a parser script which returns the stylesheet consisting critical style rules. This can be further processed in a callback function.

Operation is not limited to checking a single stylesheet as the parser script is injected to and runs in phantomjs.

Automation options

The Grunt plugin grunt-critical-css uses this module.

Quick Start

Install the module via npm: npm install critical-css

var criticalCss = require('critical-css');

var options = {
  width: 1050,
  height: 800,
  enabledOrigins: ['www.example.com']
}

criticalCss.generate('http://www.example.com/', function(err, output) {
  if (err) {
    throw new Error(err);
  }
  // Print the styles to the console.
  console.log(output);
});

Documentation

.generate(url, [options, callback])

url

The URL of the site to process. Must be accessible by phantom.

options

An optional object of options.

options.width

Type: Integer Default value: 1200

The width of the viewport used in the browser. Used to determine what is "above the fold", i.e what is visible during rendering the page initially.

options.height

Type: Integer Default value: 900

The height of the viewport used in the browser. Used to determine what is "above the fold", i.e what is visible during rendering the page initially.

options.excludeSelectors

Type: Array Default value: []

An array of CSS selectors or basically any pattern. These are matched against every individual style declaration and if the patterns provided here match the style rule is discarded from the output.

This can be useful to exclude certain 3rd party elements or any styles that are loaded asynchronously anyways.

Example:
options = {
  excludeSelectors: [
    '.dfp-tag-wrapper', // Asynchronous DFP ad formatting
    'html, body, div'  // CSS resets starting with "html, body, div"
  ]
};

options.enabledOrigins

Type: Array Default value: []

An array of host names to serve as a whitelist where CSS can originate from. Any CSSRuleList objects with parentStyleSheet.href not having this host name are excluded from the critical CSS.

This can be useful to exclude certain styles supplied by 3rd party widgets that are loaded asynchronously anyways.

Example:
options = {
  enabledOrigins: ['cdn-1.example.com', 'cdn-2.example.com']
};

options.keepInlineStyles

Type: Boolean Default value: false

Controls whether non-external styles should be included. These are usually rules which are already inlined or are set by JavaScript. These are excluded by default.

options.ignoreConsole

Type: Boolean Default value: true

Controls console output from the headless browser should be added to the output. Useful for debugging purposes.

options.maxBuffer

Type: Integer Default value: 819200

Sets the output buffer for the child process.

callback

Type: Function Default value:

function(err, output) {
  if (err) {
    throw new Error(err);
  }
  console.log(output);
};

An optional callback function with output being the value of stdout from the child process (i.e the headless browser).

Changelog

  • 0.2.0 - Refined error handling; now equipped with a basic unit test suite
  • 0.1.1 - Fixed a bug with the arguments parsing logic
  • 0.1.0 - Initial release

Contributing

This project is under active development. New features, more tests and examples are in the works now.

Please head over to the issue queue to add suggestions or file bug reports.

Development

Project led and maintained by Attila Beregszaszi

Development sponsored by Dennis Publishing and Front Seed Labs

critical-css's People

Contributors

attila avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

letynsoft

critical-css's Issues

Install stuck on extracting phantomjs

Hi,

I tried installing critical-css via npm but it gets stuck on running phantomjs install.js:

[email protected] install D:\Working\www\test\critical-js\node_modules\critical-css\node_modules\phantomjs
node install.js

Download already available at C:\Users\RODERI~1\AppData\Local\Temp\phantomjs\phantomjs-1.9.7-windows.zip
Extracting zip contents
Removing D:\Working\www\test\critical-js\node_modules\critical-css\node_modules\phantomjs\lib\phantom
Copying extracted folder C:\Users\RODERI~1\AppData\Local\Temp\phantomjs\phantomjs-1.9.7-windows.zip-extract-1487767428256\phantomjs-1.9.7-windows -> D:\Working\www\test\critical-js\node_modules\critical-css\node_modules\phantomjs\lib\phantom

I kept it running for a while (>30 minutes) but it never finished.

So then I uninstalled it and tried installing phantomjs first and then crit-css. But when I run npm install critical-css it tries to (re)install phanthomjs and gets stuck on the same line.

PhantomJS failing to open HTTPS site

.../gulp/tasks/criticalcss.js:63
throw new Error(err);
^
Error: Error: Something is awry with phantomjs...
PHANTOM ERROR: Error: Page did not open: https://
TRACE:
-> .../node_modules/critical-css/lib/runner.js: 103
-> :/modules/webpage.js: 281PhantomJS has crashed. Please read the crash reporting guide at https://github.com/ariya/phantomjs/wiki/Crash-Reporting and file a bug report at https://github.com/ariya/phantomjs/issues/new with the crash dump file attached: /tmp/9E9ACC8F-00C5-434C-8DCF-FD1BF7834633.dmp

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.