Download the PHP package backstage/laravel-minify-html-middleware without Composer
On this page you can find all versions of the php package backstage/laravel-minify-html-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download backstage/laravel-minify-html-middleware
More information about backstage/laravel-minify-html-middleware
Files in backstage/laravel-minify-html-middleware
Package laravel-minify-html-middleware
Short Description This package minifies HTML responses in Laravel safely.
License MIT
Homepage https://github.com/backstagephp/laravel-minify-html-middleware
Informations about the package laravel-minify-html-middleware
Laravel HTML Minify Middleware
A lightweight and efficient Laravel middleware package that automatically minifies HTML responses, reducing page size and improving load times. The package intelligently removes unnecessary whitespace, comments, and optimizes your HTML output while preserving functionality.
Features
- Automatic HTML Minification: Minifies HTML responses on-the-fly with zero configuration required
- Smart Detection: Only processes HTML responses, automatically skipping JSON, AJAX requests, and other non-HTML content
- Safe Minification: Preserves content in sensitive elements like
<pre>,<textarea>, and<script>tags - Framework-Aware: Compatible with Livewire, Knockout.js, and other JavaScript frameworks
- Configurable Transformers: Choose which transformations to apply or create custom ones
- Performance Optimized: Minimal overhead with efficient regex-based transformations
- Laravel 10, 11, and 12 Support: Works seamlessly with modern Laravel versions
What Gets Minified?
The package includes three built-in transformers:
- Remove Comments: Strips HTML comments while preserving framework-specific comments (Livewire, Knockout.js)
- Remove Whitespace: Eliminates unnecessary whitespace between tags and multiple spaces
- Trim Scripts: Removes extra whitespace from within
<script>tags
Before Minification:
After Minification:
Requirements
- PHP 8.2 or higher
- Laravel 10.x, 11.x, or 12.x
Installation
Install the package via Composer:
The package will automatically register its service provider.
Publish Configuration
Publish the configuration file to customize the transformers:
This will create a config/minify-html.php file with the following default configuration:
Usage
Global Middleware
To minify all HTML responses in your application, add the middleware to the global middleware stack in bootstrap/app.php (Laravel 11+):
For Laravel 10, add to app/Http/Kernel.php:
Route Middleware
To apply minification to specific routes or route groups, register the middleware with an alias:
Laravel 11+ (bootstrap/app.php):
Laravel 10 (app/Http/Kernel.php):
Then apply it to specific routes:
Conditional Minification
The middleware automatically determines if a response should be minified based on several conditions:
- ✅ Request method is
GETorHEAD - ✅ Response contains HTML (checks
Acceptheader) - ❌ Request is JSON
- ❌ Request is AJAX (XMLHttpRequest)
- ❌ Request is a precognitive request
- ❌ Response has no DOCTYPE declaration in the first 100 characters
This ensures that only actual HTML page responses are minified, avoiding issues with API responses or partial HTML fragments.
Configuration
Customizing Transformers
You can customize which transformers are applied by modifying the config/minify-html.php file:
Disabling Specific Transformers
Remove or comment out any transformer you don't want to use:
Creating Custom Transformers
You can create your own HTML transformers by implementing a simple transform method:
Then add it to your configuration:
Example: Uppercase Title Transformer
Built-in Transformers Explained
RemoveComments
Removes HTML comments while preserving special comments needed by frameworks:
RemoveWhitespace
Intelligently removes whitespace while preserving content in:
<pre>tags (preformatted text)<textarea>tags (form inputs)<script>tags (JavaScript code)
The transformer:
- Temporarily hides protected elements
- Removes multiple spaces, tabs, and newlines
- Removes spaces between tags (
> <becomes><) - Restores protected elements
TrimScripts
Removes leading and trailing whitespace from within <script> tags without affecting functionality:
Performance Benefits
HTML minification can reduce your HTML file size by 10-30% on average, depending on your code formatting:
- Faster Page Loads: Smaller HTML files download quicker
- Reduced Bandwidth: Lower data transfer costs
- Improved SEO: Faster page loads contribute to better search rankings
- Better Mobile Experience: Crucial for users on slower connections
Example size reduction:
- Before: 45 KB
- After: 32 KB
- Savings: 28.9% reduction
Framework Compatibility
Livewire
The package preserves Livewire comments and attributes, ensuring full compatibility:
Inertia.js
Works seamlessly with Inertia.js responses as they are JSON and automatically skipped.
Alpine.js
Alpine.js directives and attributes are preserved:
Troubleshooting
HTML Not Being Minified
- Check your response type: Ensure the response has an
Accept: text/htmlheader - Verify middleware is registered: Confirm the middleware is in your global or route middleware
- Check request method: Only
GETandHEADrequests are processed - DOCTYPE declaration: Make sure your HTML includes
<!DOCTYPE html>early in the response
Broken Layout or Functionality
- Whitespace-dependent CSS: If your layout relies on whitespace between inline elements, add those elements to the
ignoreElementsarray in a custom transformer - Template literals in scripts: Complex JavaScript might need special handling
- Pre-formatted content: Ensure
<pre>and<textarea>tags are being preserved
Debugging
Temporarily disable transformers one by one to identify which transformation is causing issues:
Testing
Run the package tests:
Run tests with coverage:
Run static analysis:
Fix code style:
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Mark van Eijk
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-minify-html-middleware with dependencies
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^10.0 || ^11.0 || ^12.0||^13.0