Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Gzip Laravel Package

erlandmuchasaj/laravel-gzip

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require erlandmuchasaj/laravel-gzip
    

    Publish the config file (if needed):

    php artisan vendor:publish --provider="ErlandMuchasaj\LaravelGzip\GzipServiceProvider"
    
  2. Enable Gzip Add the middleware to your app/Http/Kernel.php:

    protected $middleware = [
        // ...
        \ErlandMuchasaj\LaravelGzip\Middleware\Gzip::class,
    ];
    
  3. First Use Case

    • Test Gzip Compression: Send a request to any route and check the Content-Encoding: gzip header in the response.
    • Verify Performance: Use Chrome DevTools (Network tab) to compare response sizes before/after enabling Gzip.

Implementation Patterns

Middleware Integration

  • Global vs. Route-Specific

    • Use the middleware globally (as shown above) for all responses.
    • For selective routes, apply it conditionally:
      Route::middleware(Gzip::class)->group(function () {
          // Routes where Gzip is enforced
      });
      
  • Excluding Routes Configure exclusions in config/gzip.php:

    'excluded_routes' => [
        'admin/*',
        'api/v1/uncompressed',
    ],
    

Dynamic Compression Control

  • Conditional Gzip by Request Override logic in a custom middleware:

    public function handle($request, Closure $next)
    {
        if ($request->header('X-No-Gzip')) {
            return $next($request);
        }
        return app(Gzip::class)->compress($next($request));
    }
    
  • Content-Type Awareness The package auto-detects text-based responses (e.g., text/html, application/json). For custom types, extend:

    'compressible_content_types' => [
        'text/html',
        'application/json',
        'application/xml',
        // Add custom MIME types here
    ],
    

Integration with Caching

  • Cache + Gzip Gzip works seamlessly with Laravel’s cache (e.g., Response::cache()). Ensure cached responses are still compressed by placing the middleware after caching middleware in Kernel.php.

Gotchas and Tips

Pitfalls

  1. Double Compression

    • Issue: If a response is already Gzip-compressed (e.g., from a CDN or upstream service), re-compressing it wastes CPU.
    • Fix: Check Content-Encoding headers upstream or use:
      'skip_if_already_compressed' => true, // in config/gzip.php
      
  2. Non-Text Responses

    • Issue: Binary data (e.g., images, PDFs) may corrupt if Gzip is applied.
    • Fix: Exclude binary MIME types or use:
      'excluded_content_types' => [
          'image/*',
          'application/pdf',
      ],
      
  3. Caching Headers Conflict

    • Issue: Gzip can interfere with Cache-Control or ETag headers if not handled carefully.
    • Fix: Ensure middleware runs after caching logic in Kernel.php.

Debugging

  • Verify Compression Check headers with:

    curl -I http://your-site.com
    

    Look for:

    Content-Encoding: gzip
    
  • Log Compression Stats Enable debug mode in config/gzip.php:

    'debug' => env('GZIP_DEBUG', false),
    

    Logs will appear in storage/logs/laravel.log.

Extension Points

  1. Custom Compression Levels Override the default level (e.g., 9 for max compression) in a service provider:

    $this->app->singleton(Gzip::class, function () {
        return new \ErlandMuchasaj\LaravelGzip\Gzip(9); // Custom level
    });
    
  2. Pre/Post-Processing Extend the Gzip class to modify responses:

    class CustomGzip extends \ErlandMuchasaj\LaravelGzip\Gzip
    {
        public function compress($response)
        {
            $response = parent::compress($response);
            // Add custom logic (e.g., modify headers)
            return $response;
        }
    }
    
  3. Browser-Specific Rules Use middleware to conditionally apply Gzip based on User-Agent:

    if (str_contains($request->userAgent(), 'Mobile')) {
        return app(Gzip::class)->compress($next($request));
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity