Laravel response compression middleware
chr15k/laravel-response-compression is a Laravel package for laravel response compression middleware.
It currently has 2 GitHub stars and 2.520 downloads on Packagist (latest version 0.3.0).
Install it with composer require chr15k/laravel-response-compression.
Discover more Laravel packages by chr15k
or browse all Laravel packages to compare alternatives.
Last updated
Boost your Laravel application's performance by optimizing HTTP responses with middleware for compression.
Install the package via Composer:
composer require chr15k/laravel-response-compression
Publish the configuration file:
php artisan vendor:publish --provider="Chr15k\ResponseCompression\ResponseCompressionServiceProvider"
This package provides the following middleware:
Applies Gzip or Brotli compression to HTTP responses based on client support. This reduces the size of the response payload and enhances load times.
Ideal For: Large JSON responses, static files, or data-intensive endpoints.
[!NOTE] To use Brotli effectively, ensure that the Brotli PHP extension is properly installed. https://pecl.php.net/package/brotli
[!WARNING] When using Brotli, a client-side decoding error may occur with non-secure connections, as modern browsers generally support Brotli compression only over HTTPS.
Apply the middleware globally to all requests:
// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
...
$middleware->web(append: [
...
\Chr15k\ResponseCompression\Middleware\CompressResponse::class,
]);
})
Alternatively, register it as route middleware for selective application:
use Chr15k\ResponseCompression\Middleware\CompressResponse;
Route::get('/profile', function () {
// ...
})->middleware(CompressResponse::class);
/**
* Enable or disable the response compression.
*/
'enabled' => env('RESPONSE_COMPRESSION_ENABLED', true),
/**
* The compression algorithm to use. Can be either 'gzip' or 'br'.
*/
'algorithm' => env('RESPONSE_COMPRESSION_ALGORITHM', 'gzip'),
/**
* The minimum length of the response content to be compressed.
*/
'min_length' => env('RESPONSE_COMPRESSION_MIN_LENGTH', 1024),
'gzip' => [
/**
* The level of compression. Can be given as 0 for no compression up to 9
* for maximum compression. If not given, the default compression level will
* be the default compression level of the zlib library.
*
* @see https://www.php.net/manual/en/function.gzencode.php
*/
'level' => env('RESPONSE_COMPRESSION_GZIP_LEVEL', 5),
],
'br' => [
/**
* The level of compression. Can be given as 0 for no compression up to 11
* for maximum compression. If not given, the default compression level will
* be the default compression level of the brotli library.
*
* @see https://www.php.net/manual/en/function.brotli-compress.php
*/
'level' => env('RESPONSE_COMPRESSION_BROTLI_LEVEL', 5),
]
| Before | After |
|---------|---------|
| |
|
composer test
Contributions are welcome! Submit a pull request or open an issue to discuss new features or improvements.
The MIT License (MIT). Please see License File for more information.