shemshad / laravel-blade-minify-plus
Minify the final HTML output of your Laravel Blade views without breaking your pages. Supports Laravel versions 8 through 12.
Installs: 9
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/shemshad/laravel-blade-minify-plus
Requires
- php: >=8.1
- illuminate/support: ^8.0|^9.0|^10.0|^11.0|^12.0
- voku/html-min: ^4.5
README
laravel-blade-minify-plus
is a lightweight package for Laravel 8 through 12 that
minifies the final HTML produced by your Blade templates. Minified HTML
reduces response sizes, improves the effectiveness of gzip compression and can
deliver a small performance boost to your users without having to change your
views. The underlying voku/html‑min
library removes extra whitespace, comments and other unneeded characters
without breaking the content structure. The result
is cleaner HTML that is safe for dynamic pages and SEO.
Installation
Install the package via Composer:
composer require shemshad/laravel-blade-minify-plus
The service provider is auto‑discovered by Laravel. If you are not using
package discovery you can register it manually in your config/app.php
file:
'providers' => [ // ... BladeMinifyPlus\BladeMinifyPlusServiceProvider::class, ],
Version Compatibility & Middleware Registration
This package provides a MinifyHtmlMiddleware
. How you register and use it depends on your Laravel version.
Laravel 10 (and earlier)
Register an alias in app/Http/Kernel.php
(in newer Laravel 10 apps the property is called $middlewareAliases
; older apps may still use $routeMiddleware
):
// app/Http/Kernel.php protected $middlewareAliases = [ // ... 'minify' => \BladeMinifyPlus\Middleware\MinifyHtmlMiddleware::class, ];
Configuration
Publish the configuration file to customize the behaviour:
php artisan vendor:publish --tag=blade-minify-plus-config
This will create a config/blade-minify-plus.php
file where you can
adjust the following options:
Key | Description |
---|---|
enabled |
Enable or disable minification globally. |
skip_routes |
Array of named routes that should not be minified. Useful for APIs or routes that render JSON/XML. |
options |
A map of booleans corresponding to the methods exposed by voku\HtmlMin . These options allow you to fine‑tune the minification process. For example, doRemoveComments controls whether HTML comments are stripped and doRemoveWhitespaceAroundTags removes unnecessary whitespace between tags. |
The default options are conservative; many of the more aggressive transformations are disabled by default to avoid breaking pages. You can experiment with enabling them if you wish to squeeze out a few more bytes.
Usage
Route middleware
The package registers a middleware alias named minify
. You can apply
minification to a specific route or group:
Route::get('/about', function () { return view('about'); })->middleware('minify'); Route::middleware(['minify'])->group(function () { Route::view('/', 'welcome'); Route::view('/contact', 'contact'); });
Global middleware (optional)
If you wish to enable minification for all web routes, add the
MinifyHtmlMiddleware
to the web
middleware group in your
app/Http/Kernel.php
:
protected $middlewareGroups = [ 'web' => [ // ... existing middleware ... \BladeMinifyPlus\Middleware\MinifyHtmlMiddleware::class, ], ];
Alternatively you can uncomment the pushMiddlewareToGroup
call in
BladeMinifyPlusServiceProvider
to automatically append the middleware to
the web
group whenever the package is enabled.
Skipping minification
Some pages—such as ones that return JSON, serve files, or use complex JavaScript frameworks—may not benefit from HTML minification. You can exclude them in two ways:
- Set the
skip_routes
array in the configuration to the names of the routes you wish to ignore. - Disable minification on a per‑route basis by simply omitting the
minify
middleware from that route or group.
How it works
When a request is processed, the MinifyHtmlMiddleware
inspects the
response. If the response is HTML, the middleware passes the content
through a configured HtmlMinifier
instance which uses the
voku/html‑min
library to optimise it. Non‑HTML responses (JSON, XML,
binary files) and routes listed in skip_routes
are left untouched. The
minifier supports a rich set of flags that control how aggressively
optimisation should be applied. By default only safe
optimisations such as removing comments and compressing excess
whitespace are enabled.
Laravel’s own HTMLMin\HTMLMin
package warns that forcing optimisations can
be dangerous and recommends enabling them only when you understand the
implications. With Blade‑Minify‑Plus you remain
in control: minification is opt‑in via middleware and each option can be
tuned to your needs.
Testing
To ensure minification isn’t silently breaking pages, you can write feature tests that assert your views still contain expected strings. You may also dump the response body locally and visually inspect it before enabling minification in production. Remember to clear your view cache whenever changing Blade templates:
php artisan view:clear
Compatibility
This package supports Laravel versions 8.x through 12.x. It requires PHP 8.1 or newer. Because it relies on Laravel’s middleware stack, it does not need to touch the Blade compiler and thus remains forward compatible with future releases.
Security and contribution
If you discover a security vulnerability, please open an issue or submit a pull request. Contributions are welcome; feel free to file issues or pull requests to improve the package.
License
Blade‑Minify‑Plus is open‑source software licensed under the MIT license.
Laravel 12 Setup (Kernel-less)
Register the middleware in bootstrap/app.php
as shown in examples/bootstrap.app.l12.php
.