chr15k/laravel-response-compression

Laravel response compression middleware

0.1.1 2024-12-29 09:37 UTC

This package is auto-updated.

Last update: 2024-12-29 09:48:43 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Boost your Laravel application's performance by optimizing HTTP responses with middleware for compression.

Installation

Install the package via Composer:

composer require chr15k/laravel-response-compression

Publish the configuration file:

php artisan vendor:publish --provider="Chr15k\ResponseCompression\ResponseCompressionServiceProvider"

Middleware Overview

This package provides the following middleware:

Compression 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.

Setup

Register Middleware

Global Middleware

Apply the middleware globally to all requests:

// bootstrap/app.php

->withMiddleware(function (Middleware $middleware) {
    ...
    $middleware->web(append: [
        ...
        \Chr15k\ResponseCompression\Middleware\CompressResponse::class,
    ]);
})

Route Middleware

Alternatively, register it as route middleware for selective application:

use Chr15k\ResponseCompression\Middleware\CompressResponse;

Route::get('/profile', function () {
    // ...
})->middleware(CompressResponse::class);

Config

/**
 * 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),
]

Testing

composer test

Contributing

Contributions are welcome! Submit a pull request or open an issue to discuss new features or improvements.

License

The MIT License (MIT). Please see License File for more information.