Skip to content

puklipo/laravel-vapor-gzip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gzip Response middleware for Laravel Vapor

Requirement

  • PHP ^8.1 (+ext-zlib)
  • Laravel ^10.x

Installation

composer require puklipo/laravel-vapor-gzip

Laravel 11 (Slim skeleton)

Add to bootstrap/app.php

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

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

Laravel 10

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

About

Gzip Response middleware for Laravel Vapor

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages