You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

critical-css-parser

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

critical-css-parser

Get critical (above-the-fold) and rest CSS using Puppeteer.

0.2.0
Source
npmnpm
Version published
Maintainers
1
Created
Source

critical-css-parser

Critical-css-parser allows you to receive critical (above-the-fold) and rest CSS. It might be helpful to speed up initial rendering of a web page. More information

Critical-css-parser uses Puppeteer and DropCSS under the hood, so it also supports adaptive design.

Installation

npm install --save-dev critical-css-parser

// or

yarn add --dev critical-css-parser

Usage

criticalCSSParser

Pass options to criticalCSSParser({ ... }).

Parameters

  • options

Properties

  • type String One of: HTML, URL or localServer. REQUIRED
  • html String Your custom html code (default: '') REQUIRED FOR type === HTML
  • css String Your custom css code (default: '') REQUIRED FOR type === HTML
  • whitelist RegExp Whitelist of the critical CSS (default: /#fooBazBarAboveTheFold8917/)
  • URL String URL of the page you need to optimize (default: '') REQUIRED FOR type === URL
  • enableGoogleFonts Boolean Set true to except Google Font styles (default: false)
  • entrypoint String Root of your application (default: '') REQUIRED FOR type === localServer
  • filename String Your index file (default: 'index.html')
  • minify Boolean Set true to minify resulting styles (default: false)

criticalCSSParser({...}) returns:

<Promise<{ critical, rest }>>

critical - above-the-fold CSS. These styles you can put into your <style> tag.

rest - other CSS. You can lazy-load the rest like this.

Examples

HTML and CSS

const criticalCSSParser = require('critical-css-parser');

const html = `
    <html>
        <head></head>
        <body>
            <p>Hello <a href="#">World!</a></p>
        </body>
    </html>
`;

const css = `
    .card {
        padding: 8px;
    }

    p:hover a:first-child {
        color: red;
    }
`;

(async () => {

    const result = await criticalCSSParser({
        type: 'HTML',
        html,
        css,
        minify: true
    });

    console.log(result.critical); // 'p:hover a:first-child{color:red}'

    console.log(result.rest); // '.card{padding: 8px;}'

})();

URL

const criticalCSSParser = require('critical-css-parser');

(async () => {

    const result = await criticalCSSParser({
        type: 'URL',
        URL: 'https://enigmatic-dawn-63122.herokuapp.com/',
        enableGoogleFonts: false
    });

    console.log(result.critical); // ''

    console.log(result.rest); // '.Toastify__toast-container{z-index:9999;position:fixed; ...'

})();

localServer

const criticalCSSParser = require('critical-css-parser');

(async () => {

    const result = await criticalCSSParser({
        type: 'localServer',
        entrypoint: './www',
        filename: 'index.html',
        enableGoogleFonts: false
    });

    console.log(result.critical); // 'html{font-size: 10pt;}div{color: red;}'

    console.log(result.rest); // 'p{color: red;}'

})();

License

MIT license

Keywords

critical

FAQs

Package last updated on 27 May 2019

Did you know?

Socket

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