m2-boilerplate / module-critical-css

m2-boilerplate/module-critical-css

Magento 2 module to automatically generate critical css with the addyosmani/critical npm package

magento2-module Compatibility: 2.4.7 Code Quality: Fail Tests: N/A Security: Pass MIT

M2Boilerplate Critical CSS

Magento 2 module to automatically generate critical css with critical

Features:

  • generate critical css with a simple command
  • Fallback critical css for "empty" pages
  • Add urls by creating a custom provider

Installation

Install the critical binary. Install instructions can be found on the critical website. Only versions >=2.0.3 are supported.

Add this extension to your Magento installation with Composer:

composer require m2-boilerplate/module-critical-css

Usage

Configuration

The critical css feature needs to be enabled (available in 2.3.4+):

bin/magento config:set dev/css/use_css_critical_path 1

Features can be customised in Stores > Configuration > Developer > CSS.

Generate critical css

Run the following command

bin/magento m2bp:critical-css:generate

Afterwards you should find the the generated css in var/critical-css. The css will now be integrated automatically on your website.

Add additional URLs via a custom provider

The following example adds the magento contact page via a custom provider:

<?php

namespace Vendor\Module\Provider;

use Magento\Framework\App\Request\Http;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Store\Api\Data\StoreInterface;
use M2Boilerplate\CriticalCss\Provider\ProviderInterface;
  
class CustomProvider implements ProviderInterface
{
    const NAME = 'custom_example';

    /**
     * @var UrlInterface
     */
    protected $url;

    public function __construct(UrlInterface $url)
    {
        $this->url = $url;
    }


    public function getUrls(StoreInterface $store): array
    {
        return [
            'contact_index_index' => $this->url->getUrl('contact'),
        ];
    }

    public function getName(): string
    {
        return self::NAME;
    }

    public function isAvailable(): bool
    {
        return true;
    }

    public function getPriority(): int
    {
        return 1200;
    }

    public function getCssIdentifierForRequest(RequestInterface $request, LayoutInterface $layout): ?string
    {
        if ($request->getModuleName() !== 'contact' || !$request instanceof Http) {
            return null;
        }
        
        if ($request->getFullActionName('_') === 'contact_index_index') {
            return 'contact_index_index';
        }

        return null;
    }
}

Add the new provider via DI:

in your module´s etc/di.xml add the following:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="M2Boilerplate\CriticalCss\Provider\Container">
        <arguments>
            <argument name="providers" xsi:type="array">
                <item name="custom_example" xsi:type="object">Vendor\Module\Provider\CustomProvider</item>
            </argument>
        </arguments>
    </type>
</config>

License

See the LICENSE file for license info (it's the MIT license).

No changelog yet

The vendor hasn't published a changelog. Tagged releases appear in the Versions tab.

Versions
Version Stability QA Status Compatibility Released
1.0.20 stable Fail Magento 2.4.7 Details 2024-03-15 15:22:51
1.0.19 stable Not tested Not yet tested Details 2023-08-09 08:17:05
1.0.18 stable Not tested Not yet tested Details 2022-07-05 08:57:59
1.0.17 stable Not tested Not yet tested Details 2022-02-10 09:55:12
1.0.16 stable Not tested Not yet tested Details 2022-01-24 15:10:18
1.0.15 stable Not tested Not yet tested Details 2021-10-22 09:49:08
1.0.14 stable Not tested Not yet tested Details 2021-08-19 14:52:25
1.0.13 stable Not tested Not yet tested Details 2021-07-26 05:56:42
1.0.12 stable Not tested Not yet tested Details 2021-07-26 04:26:15
1.0.11 stable Not tested Not yet tested Details 2021-07-12 08:39:53
1.0.10 stable Not tested Not yet tested Details 2021-04-19 09:58:58
1.0.9 stable Not tested Not yet tested Details 2021-04-19 09:42:48
1.0.8 stable Not tested Not yet tested Details 2021-04-08 09:52:23
1.0.7 stable Not tested Not yet tested Details 2021-04-08 09:51:40
1.0.6 stable Not tested Not yet tested Details 2021-04-08 08:39:18
1.0.5 stable Not tested Not yet tested Details 2021-04-07 16:18:15
1.0.4 stable Not tested Not yet tested Details 2021-04-07 14:45:00
1.0.3 stable Not tested Not yet tested Details 2021-04-04 16:03:53
1.0.2 stable Not tested Not yet tested Details 2021-04-03 10:57:38
1.0.1 stable Not tested Not yet tested Details 2021-02-23 12:04:15
1.0.0 stable Not tested Not yet tested Details 2020-12-25 14:03:39

Requires 2

Package Constraint
magento/framework ^102.0|^103.0
php >=7.1.0

Compatibility

Each Magento release line is installed on its supported PHP versions, then the module is built (DI compilation + static-content deploy) and its unit and integration suites are run. The matrix shows the lines and PHP versions the module is confirmed to install and run on. Code-quality results further down (phpstan, phpcs, …) are reported separately and never affect compatibility.

Compatibility matrix (Magento × PHP)
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 Pass Pass
2.4.8 Fail di error Fail di error
2.4.9 Fail di error Fail di error

Code Quality

Advisory checks against the module's source. Static analysis runs once across the whole module; PHPStan re-runs per Magento + PHP version because resolvable symbols differ between releases. These NEVER affect the Compatibility badge — a phpcs finding can't make a module incompatible.

Static analysis

Coding standards (phpcs), mess detection (phpmd), copy-pasted code (cpd), PHP cross-version compatibility, composer.json validity. Each runs once for the whole module.

Static analysis results
Tool Status Findings Summary
PHPCS Warning 90 90 warnings (ruleset: Magento2) — 44 auto-fixable with phpcbf
PHPMD Warning 9 9 rule violations (UnusedFormalParameter:3, EmptyCatchBlock:2, CountInLoopExpression:2, IfStatementAssignment:1, MissingImport:1)
Cpd Pass 0
Composer validate Pass 0

PHPStan

Type-checks the module's PHP against a real Magento install at the configured gate level. Re-runs per Magento and PHP version because resolvable symbols differ between releases. Cell → details modal.

PHPStan results by Magento and PHP version
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 16 16
2.4.8 19 20
2.4.9 22 22

Tests

Unit and integration suites, run for each applicable Magento and PHP version. A test failure speaks to the module's behaviour, not its compatibility with a Magento line, so it is reported here separately and never reddens the compatibility matrix.

Unit tests

Unit tests results by Magento and PHP version
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 N/A N/A
2.4.8 N/A N/A
2.4.9 N/A N/A

Integration tests

Integration tests results by Magento and PHP version
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 N/A N/A
2.4.8 N/A N/A
2.4.9 N/A N/A

Security

Security checks run directly against the module: an audit of its declared dependencies for known vulnerabilities (composer audit) and a scan of its source for malware and web-shell signatures. Each runs once. A malware detection fails the version outright.

Security results
Tool Status Findings Summary
Composer audit Pass 0
Malware scan Pass 0
License
MIT
Make it pay

Turn an existing module into recurring revenue.

If you already maintain a Magento 2 module on GitHub or GitLab, listing it on Packagento takes about five minutes. We mirror your tags, handle distribution signing, and route paid licenses through Stripe Connect, so you can keep shipping the way you already do.