innocode-digital/wp-critical-css-aws-lambda

Generates critical stylesheets for templates via AWS Lambda.

Installs: 3 654

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 15

Forks: 1

Type:wordpress-muplugin

3.0.0 2022-06-18 16:52 UTC

This package is auto-updated.

Last update: 2024-04-18 21:15:40 UTC


README

Description

Generates critical stylesheets for templates via AWS Lambda. See also AWS Lambda Critical CSS.

Install

  • Preferable way is to use Composer:

    composer require innocode-digital/wp-critical-css-aws-lambda
    

    By default, it will be installed as Must Use Plugin. It's possible to control with extra.installer-paths in composer.json.

  • Alternate way is to clone this repo to wp-content/mu-plugins/ or wp-content/plugins/:

    cd wp-content/plugins/
    git clone git@github.com:innocode-digital/wp-critical-css-aws-lambda.git
    cd wp-critical-css-aws-lambda/
    composer install
    

If plugin was installed as regular plugin then activate AWS Lambda Critical CSS from Plugins page or WP-CLI: wp plugin activate wp-critical-css-aws-lambda.

Usage

Add the following constants to wp-config.php:

define( 'AWS_LAMBDA_CRITICAL_CSS_KEY', '' );
define( 'AWS_LAMBDA_CRITICAL_CSS_SECRET', '' );
define( 'AWS_LAMBDA_CRITICAL_CSS_REGION', '' ); // e.g. eu-west-1

define( 'AWS_LAMBDA_CRITICAL_CSS_FUNCTION', '' ); // Optional, default value is "critical-css-production-processor"

Create serverless function on your favorite service. Expected default name is critical-css-production-processor, but you may use any other. There is a prepared function AWS Lambda Critical CSS.

Usage

To generate critical CSS from enqueued styles:

add_filter( 'innocode_critical_css_styles', function () {
    return [
        // List of enqueued styles. 
        // Specify styles which you think are needed for critical CSS.
    ];
} );

Caveats

  • Relative paths to custom fonts or images in stylesheets should be changed to absolute:
add_filter( 'innocode_critical_css_stylesheet', function ( $stylesheet ) {
    $stylesheet = str_replace(
        '../fonts/',
        get_template_directory_uri() . '/path/to/fonts/',
        $stylesheet
    );
    $stylesheet = str_replace(
        '../images/',
        get_template_directory_uri() . '/path/to/images/',
        $stylesheet
    );

    return $stylesheet;
} );
  • This plugin is only for generating and rendering critical CSS, to defer CSS files you may use Deferred loading.

  • If page caching is used then you may want to add variable value from cookie in cache key. E.g. for Batcache it's possible to do with next code before advanced-cache.php file is included:

foreach ( array_keys( $_COOKIE ) as $name ) {
    if ( strpos( $name, 'innocode_critical_css_' ) === 0 ) {
        $GLOBALS['batcache']['unique']['innocode_critical_css'] = substr( $name, strlen( 'innocode_critical_css_' ) );
    }
}