Overview
The Critical CSS plugin:- Analyzes your rendered HTML pages
- Extracts critical above-the-fold CSS
- Inlines critical CSS into the
<head> - Adds preload tags for full stylesheets
- Supports custom viewport dimensions
- Minifies inlined CSS automatically
Installation
The Critical CSS plugin is a built-in Scully plugin and requires no separate installation.Configuration
Register and configure the plugin in yourscully.config.ts:
Configuration Options
Inline images into the pages when smaller than 10240 bytes (10KB). This reduces HTTP requests for small images.
Width of the target viewport in pixels. Used to determine which CSS is “above the fold”.
Height of the target viewport in pixels. Used to determine which CSS is “above the fold”.
An array of viewport dimension objects. Takes precedence over
width and height if set.An array of fully qualified paths to asset directories. If not provided, defaults to
[outDir, outDir/assets].Specify CSS rules to ignore during critical CSS extraction.
Usage Examples
Basic Configuration
Minimal setup with default options:Mobile-First Configuration
Optimize for mobile viewports:Multi-Device Configuration
Generate critical CSS for multiple viewport sizes:Custom Asset Paths
Specify custom locations for static assets:Ignore Specific CSS
Exclude certain CSS rules from critical extraction:How It Works
- HTML Analysis: After Scully renders a page, the plugin analyzes the HTML
- CSS Discovery: All CSS files in the dist folder are found and read
- Viewport Simulation: The page is rendered in a headless browser at specified dimensions
- Critical Extraction: CSS required for above-the-fold rendering is identified
- Inline Injection: Critical CSS is minified and injected into the
<head> - Preload Addition: Original stylesheets are preloaded for lazy loading
- Image Inlining: Small images are converted to data URIs if enabled
Output Example
Before Critical CSS plugin:Performance Benefits
Faster First Paint
Critical CSS eliminates render-blocking resources, showing content to users faster.
Reduced Layout Shifts
Inlined CSS ensures proper layout before external stylesheets load.
Better Lighthouse Scores
Improves Performance and Best Practices scores.
Improved User Experience
Faster perceived load times lead to better user engagement.
When to Use
The Critical CSS plugin is beneficial when:- You have large CSS files
- First paint performance is critical
- You want to improve Lighthouse scores
- You’re building content-heavy sites
- SEO performance matters
Best Practices
Monitor build times
Critical CSS extraction adds processing time. Balance optimization with build speed.
Error Handling
If critical CSS extraction fails for a route:- A warning is logged with the route information
- The original HTML is returned unchanged
- The build continues with other routes
Failed critical CSS extraction doesn’t break your site—it simply means that route won’t have optimized CSS inlining.
Limitations
- No CSS Extraction: The plugin doesn’t remove critical CSS from full stylesheets (to ensure SPA functionality)
- Build Time: Processing time increases with page count and CSS size
- Static Analysis: Only analyzes static rendered HTML, not dynamic JavaScript-rendered content
Troubleshooting
CSS not being inlined
CSS not being inlined
- Verify CSS files exist in the dist folder
- Check that
outDiris correctly configured - Ensure CSS files are generated before Scully runs
Build time is too slow
Build time is too slow
- Reduce viewport dimensions array
- Disable image inlining with
inlineImages: false - Consider selective application to important routes only
Images not inlining
Images not inlining
- Ensure images are smaller than 10KB
- Verify
inlineImages: trueis set - Check that asset paths are correct
Related
Flash Prevention Plugin
Prevent flash of unstyled content
Performance Optimization
Learn more optimization techniques

