Install requirements for the render engine
masterWhile the static engine has minimal requirements, the render engine requires playwright and a browser to function. Install them using:
npm i -D playwright && npx playwright install chromiumrepository·master·Indexed 27 days ago
https://github.com/addyosmani/criticalA tool for improving web performance by extracting above-the-fold critical-path CSS, inlining it into HTML, and deferring remaining stylesheets to prevent render-blocking. Version 9.0.0-next.0 features a dual-engine system: a browser-free static engine for SSG/SSR and a Playwright-based render engine for SPAs and viewport-accurate sets. It provides a CLI, a programmatic API, and an MCP server for AI agents.
While the static engine has minimal requirements, the render engine requires playwright and a browser to function. Install them using:
npm i -D playwright && npx playwright install chromiumWhen using inline: true, Critical inserts a <style> tag into the <head> and converts <link rel="stylesheet"> tags into <link rel="preload"> tags that are moved to the end of the <body>.
To support a strict Content Security Policy (CSP), you can pass a nonce within the inline configuration object to stamp the injected <style> tag.
await critical({
src: "dist/index.html",
inline: {
nonce: "your-csp-nonce"
}
});Install critical as a development dependency using npm:
npm install --save-dev criticalCritical includes a Model Context Protocol (MCP) server that allows AI agents to use it as a tool. The server exposes the optimize_critical_css tool, which accepts src/html (and optional css, engine, inline, width, height) and returns the critical CSS, rewritten HTML, and a structured report.
Requirements:
@modelcontextprotocol/sdk as a peer dependency.CLI Usage:
node src/mcp.jsProgrammatic Usage:
import { createServer } from "critical/mcp";When choosing a tool for inlining critical-path CSS, consider the trade-offs between viewport awareness, speed, and the type of HTML being processed:
optimizeCss.You can optimize the critical CSS set using the following methods:
[data-critical-fold] attribute. The static engine will scope matching CSS to that subtree, creating a tighter set without requiring a browser.width, height, or dimensions to define the "above the fold" area. Providing multiple dimensions will union the results, allowing you to ship a single critical set that covers multiple device types (e.g., mobile and desktop).@font-face, @keyframes, and custom properties are preserved if referenced. Unused @keyframes and empty @media, @supports, or @layer blocks are automatically pruned.The CLI allows you to process HTML files or entire directories to inline critical CSS and defer the rest of the stylesheets.
Common tasks:
--inline and --write to update all HTML files in a build folder.--explain to see the engine decision and size statistics without modifying files.--json to emit structured results for CI/CD pipelines.# Optimize a build directory in place (inlines critical CSS, defers the rest)
critical ./dist --inline --write
# See what it would do and why, without writing anything
critical ./dist --explain
# A single file to stdout
critical index.html --inline > index.critical.htmlUse the following decision logic to select the appropriate tool for your project:
| Scenario | Recommended Tool |
|---|---|
| Statically generated / SSR site (wanting simplicity and speed) | Beasties (or framework-native integrations like Next.js optimizeCss) |
| Long pages (where below-the-fold CSS bloats the payload) | Critical (using the render engine) |
| Single-page app (SPA) (with an empty HTML shell at build time) | Critical (using the render engine) or prerender/SSR the page first |
| Need structured/JSON output for build scripts or agents | Critical |
| Low-level extraction (you already have CSS and a rendered page) | Penthouse |
critical command extracts and inlines critical-path CSS for HTML files. It accepts a directory, a specific .html file, or input via stdin.The Critical MCP server allows AI agents (like Claude Code) to call Critical's optimization logic directly as a tool. Instead of parsing stdout from a CLI, the agent receives a structured report containing the engine choice, reasoning, and bytes saved, alongside the rewritten HTML.
To run the server directly via Node.js:
node src/mcp.jsNote: This requires @modelcontextprotocol/sdk as a peer dependency. If it is missing, install it using:
npm i @modelcontextprotocol/sdknode src/mcp.jsYou can import critical to process HTML and CSS files manually. This is useful when you need to control exactly where the output is written.
import { writeFile } from "node:fs/promises";
import { critical } from "critical";
const { html, css } = await critical({ src: "dist/index.html", inline: true });
await writeFile("dist/index.html", html);
await writeFile("dist/critical.css", css);For Single Page Applications (SPAs) where the initial HTML shell is empty until JavaScript executes, you must use the render engine to ensure the content is captured correctly.
await critical({ src: "dist/index.html", engine: "render", inline: true });