Skip to content

Latest commit

 

History

History
66 lines (53 loc) · 1.69 KB

README.md

File metadata and controls

66 lines (53 loc) · 1.69 KB

Gzip Response middleware for Laravel Vapor

Requirement

  • PHP ^8.2 (+ext-zlib)
  • Laravel ^11.x

Installation

composer require puklipo/laravel-vapor-gzip

Slim skeleton (Laravel 11 or later)

Add to bootstrap/app.php

use Puklipo\Vapor\Middleware\GzipResponse::class;

->withMiddleware(function (Middleware $middleware) {
     $middleware->append(GzipResponse::class);
})

Legacy kernel

Add to app/Http/Kernel.php

    protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \App\Http\Middleware\TrustProxies::class,
        \Illuminate\Http\Middleware\HandleCors::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
+       \Puklipo\Vapor\Middleware\GzipResponse::class,
    ];

Customize "When to encode with gzip?"

App\Providers\AppServiceProvider

use Illuminate\Http\Request;
use Puklipo\Vapor\Middleware\GzipResponse;

public function boot(): void
{
    GzipResponse::encodeWhen(function (Request $request, mixed $response): bool {
        return in_array('gzip', $request->getEncodings())
            && $request->method() === 'GET'
            && function_exists('gzencode')
            && ! $response->headers->contains('Content-Encoding', 'gzip')
            && ! $response instanceof BinaryFileResponse;
    });
}

When this package abandoned

You can use just the GzipResponse.php. Copy to your Laravel project, and change namespace.

namespace App\Http\Middleware;

LICENSE

MIT