el-schneider / statamic-html-minify
An addon to minify HTML for Statamic 5 and 6
Package info
github.com/el-schneider/statamic-html-minify
pkg:composer/el-schneider/statamic-html-minify
Fund package maintenance!
Requires
- php: ^8.2
- statamic/cms: ^5.0 || ^6.0
- voku/html-min: ^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- orchestra/testbench: ^10.0 || ^11.0
- pestphp/pest: ^3.8 || ^4.0 || ^5.0
- phpunit/phpunit: ^11.5 || ^12.0 || ^13.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Minifies HTML responses in Statamic 5 and 6 through Laravel's web middleware stack.
Installation
composer require el-schneider/statamic-html-minify
php artisan vendor:publish --provider="ElSchneider\HtmlMinify\HtmlMinifyServiceProvider"
Configuration
The published config/html-minify.php file controls the middleware and each voku/html-min option exposed by the addon.
HTML_MINIFY_ENABLED=true HTML_MINIFY_SKIP_ON_DEBUG=true HTML_MINIFY_REMOVE_COMMENTS=true
Publish the configuration first:
php artisan vendor:publish --provider="ElSchneider\HtmlMinify\HtmlMinifyServiceProvider"
Every option can also be set through the listed environment variable.
| Config key | Environment variable | Default | Effect |
|---|---|---|---|
enabled |
HTML_MINIFY_ENABLED |
true |
Minify eligible HTML responses. |
skip_on_debug |
HTML_MINIFY_SKIP_ON_DEBUG |
false |
Return HTML unchanged when APP_DEBUG=true. |
optimizeViaHtmlDomParser |
HTML_MINIFY_OPTIMIZE_VIA_DOM_PARSER |
true |
Parse and optimize the HTML DOM. |
removeComments |
HTML_MINIFY_REMOVE_COMMENTS |
true |
Remove regular HTML comments. Protected conditional comments remain. |
sumUpWhitespace |
HTML_MINIFY_SUM_UP_WHITESPACE |
false |
Collapse runs of whitespace. Test before enabling: voku/html-min 5.0.0 can remove literal non-breaking spaces. |
sortCssClassNames |
HTML_MINIFY_SORT_CSS_CLASS_NAMES |
false |
Sort CSS class names for more compressible output. |
sortHtmlAttributes |
HTML_MINIFY_SORT_HTML_ATTRIBUTES |
false |
Sort attributes for more compressible output. |
removeWhitespaceAroundTags |
HTML_MINIFY_REMOVE_WHITESPACE_AROUND_TAGS |
false |
Remove whitespace around tags. This can change rendered text. |
removeSpacesBetweenTags |
HTML_MINIFY_REMOVE_SPACES_BETWEEN_TAGS |
false |
Remove whitespace between tags. This joins text separated by inline elements. |
removeOmittedQuotes |
HTML_MINIFY_REMOVE_OMITTED_QUOTES |
false |
Remove attribute quotes where HTML permits it. |
removeOmittedHtmlTags |
HTML_MINIFY_REMOVE_OMITTED_HTML_TAGS |
false |
Remove optional HTML tags. This can affect scripts and selectors that inspect source markup. |
Existing installs and whitespace upgrades
Existing users who already published the config file will keep their previous values after package updates. When upgrading, explicitly set both of these to false (or set the environment variables to false) if your pages rely on existing whitespace boundaries:
sumUpWhitespaceremoveSpacesBetweenTags
If config is cached, clear and rebuild it after changing these values:
php artisan config:clear php artisan config:cache
Behavior
The middleware skips streamed responses, JSON responses, disabled/debug requests, and responses whose Content-Type is not text/html.
The non-breaking-space behavior is tracked at voku/HtmlMin#136.
Requirements
- PHP 8.2+
- Statamic 5 or 6
Testing
composer test
Credits
Maintained by el-schneider. Based on the original addon by Vaggelis Yfantis.
License
MIT