open-southeners/laravel-vapor-response-compression

Add server-side response compression with a range of different algorithms (Gzip, brotli, deflate...)

2.1.0 2024-02-14 17:22 UTC

This package is auto-updated.

Last update: 2024-04-14 17:51:44 UTC


README

Add server-side response compression with a range of different algorithms (Gzip, brotli, deflate...)

Why use this package?

The reason why is because AWS hard limit its API Gateway service to 10MB (at the date of writing this), and by hard we mean that there's no way you can increase this limitation.

Note: Thought CloudFlare is a good solution for the end user, it doesn't solve this issue as CloudFlare does this between the server and the client, so the response already passed through AWS (API Gateway).

Read more here: https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#http-api-quotas

Or on our blog article: https://opensoutheners.com/how-to-prevent-large-responses-to-trigger-502-errors

Getting started

composer require open-southeners/laravel-vapor-response-compression

Then publish the configuration file defaults into your application's config folder:

php artisan vendor:publish --tag=response-compression

Then add the following to you app/Http/Kernel.php as a global middleware:

Note: Remember this is different in older versions of Laravel, Laravel 9 should look like the following.

/**
 * The application's global HTTP middleware stack.
 *
 * These middleware are run during every request to your application.
 *
 * @var array<string, array<int, class-string|string>>
 */
protected $middleware = [
    // ...
    \OpenSoutheners\LaravelVaporResponseCompression\ResponseCompression::class,
];

Configuration

If you already have this config/response-compression.php file, you can skip this step, otherwise please use the following artisan command first:

php artisan vendor:publish --tag=response-compression

Note: As for smaller responses this threshold will prevent to compress the response if it doesn't reach an specific number of bytes. We encourage you to configure this and not leave it as ±0 bytes otherwise response will be always compressed.

This configuration is defaulted to 10000 bytes, you may customise this as your application demands.

Setting up Brotli extension in Vapor

First of all, this will require you to use Docker containers on your Vapor environments if you're not familiar with them, you can still use this extension as it uses the first client requested algorithm available at server side.

👉 Read more about using containers in Vapor here

Anyway, in case you want to proceed, add this to your environment Dockerfile(s), please use comments as references only:

# FROM laravelphp/vapor:php81

RUN apk add --no-cache brotli

# COPY . /var/task

And ensure your project depends on vdechenaux/brotli.

Alternative Brotli setup

Another alternative is to use: https://github.com/kjdev/php-ext-brotli

TODO: Setup instructions for Vapor/Dockerfile

Credits