bref icon indicating copy to clipboard operation
bref copied to clipboard

Automatically gzip large responses

Open jaulz opened this issue 2 years ago • 16 comments

I wonder if Bref could automatically try to gzip the response if it's bigger than the 6MB limit. I gave it a try and at least this approach works for me (removed the Vapor header): https://bannister.me/blog/gzip-compression-on-laravel-vapor/

Since it's already possible via (Laravel) middleware, it's not urgent but maybe a nice addition.

jaulz avatar Oct 11 '23 14:10 jaulz

Hey @jaulz, that is interesting! Do you have any numbers on bigger payloads that you are able to return that way?

mnapoli avatar Oct 11 '23 19:10 mnapoli

Yeah, for the one critical response I was dealing with it's a factor of 24 (from 8MB of text to 300KB): image

I used this code:

      $content = $response->getContent();
      $compressedContent = gzencode($content, 9);

      $response->setContent($compressedContent);
      $response->headers->add([
        'Content-Encoding' => 'gzip',
        'X-Original-Size' => mb_strlen($content, '8bit'),
        'X-Compressed-Size' => mb_strlen($compressedContent, '8bit'),
      ]);

jaulz avatar Oct 12 '23 08:10 jaulz

Now AWS supports response streaming to bypass 6MB limit

  • additional cost if you pass 6MB and with a soft limit of 20MB (maybe ampliable on request)

Its supported on NodeJS official runtime but AWS allows to support on custom runtimes too.

Maybe this will be interesting also in combination with gzip encoding, because GZIP works great on text responses but on binary responses maybe not

https://aws.amazon.com/es/blogs/compute/introducing-aws-lambda-response-streaming/ https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html

kevincerro avatar Oct 24 '23 12:10 kevincerro

In my case from 87,6kB to 5,8kB on a simple SF project using GZIP

Captura de pantalla 2023-10-24 a las 14 11 49

kevincerro avatar Oct 24 '23 12:10 kevincerro

@kevincerro FYI https://github.com/brefphp/bref/issues/1503

mnapoli avatar Oct 24 '23 16:10 mnapoli