Automattic / jetpack-boost-critical-css-gen

Critical CSS Generator built for Jetpack Boost
Other
13 stars 5 forks source link

jetpack-boost-critical-css-gen

Critical CSS Generator built for Jetpack Boost. It can generate Critical CSS on the server-side using Puppeteer, or on the client-side using iframes. It also supports generating blocks of Critical CSS that can apply to multiple URLs, and/or multiple viewports.

Warning

This is a work in progress, and its API is not guaranteed to be stable. :)

Basic API

const { BrowserInterfacePlaywright, BrowserInterfacePuppeteer, BrowserInterfaceIframe, generateCriticalCSS } = require( 'jetpack-boost-critical-css-gen' );
const [css, warnings] = generateCriticalCSS( options );

Where options is an object with the following keys:

Example usage with an IFrame:

  const { BrowserInterfaceIframe, generateCriticalCSS } = require( 'jetpack-boost-critical-css-gen' );

  const [css, warnings] = await generateCriticalCSS( {
    urls: [ 'http://example.com' ],
    viewports: [ { width: 800, height: 600 } ],
    progressCallback: ( step, stepCount ) => { console.log( `Step ${ step } of ${ stepCount }.` ); },
    browserInterface: new BrowserInterfaceIframe( {} ),
    filters: {
      properties: ( key, value ) => {
        return ! /^\s*url\s*\(/.test( value );
      },
    },
  } );

Example usage in TypeScript with Playwright:

import { chromium, Page } from 'playwright';
import { BrowserInterfacePlaywright, generateCriticalCSS } from 'jetpack-boost-critical-css-gen';

async function playwrightGenerator( urls: string[] ): Promise< string > {
    const browser = await chromium.launch();
    const context = await browser.newContext();

    // Open playwright pages for each URL.
    const pages: { [ url: string ]: Page } = {};
    for ( const url of urls ) {
        pages[ url ] = await context.newPage();
        await pages[ url ].goto( url );
    }

    // Call the Critical CSS generator.
    const [ css, warnings ] = await generateCriticalCSS( {
        urls,
        viewports: [
            { width: 640, height: 480 },
            { width: 1024, height: 768 },
            { width: 1280, height: 1024 },
        ],
        browserInterface: new BrowserInterfacePlaywright( pages ),
    } );

    if ( warnings.length ) {
        console.warn( warnings );
    }

    return css;
}

Releasing new version

Local development

Since we generally do development in docker, it is hard to test this with Jetpack Boost. npm link does not work in this case. To get around this issue, we can use yalc and do some customization to make development and testing easy. Here is what to do:

See: pc9hqz-1NI-p2