Adonis: use GZIP Encoding


Hello guys,

So I am using Adonis 4.1 in combination with React on the front-end for my web app.

However, the bundled version of my React file is pretty heavy and I would like to GZIP encode my asset file in order to reduce loading time on mobile phones. I created a middleware like this:

However, this gives me a browser error:


I figured the content must not actually be encoding properly but how do I fix that? The docs of zlib is somewhat poorly written and not much of a help when it comes to understanding how to use in a Node.js framework environment.


Why would you serve react bundle using a custom route?

Simply put the gzip version of the bundle inside the public folder and serve it from their


@virk I don’t understand what you mean by “serve a react bundle using a custom route”.

And to respond to your 2nd statement, I’m not understanding but it sounds like you are telling me to do this as if it was a one-off process.

The site would be receiving continuous updates and going out of my way to gzip my compiled js and css myself and putting them in the /public folder would mean I would have to do that for every single change to the code which doesn’t sound right. Having a middleware to automatically gzip every page and asset is much cleaner and direct.

I’m not really too sure what are you telling me here.


How are you building your bundle? Surely there’s an option to define the destination, and the ability to add gzipping to the build process.

Webpack can do this, and you could always just have it be part of your production deployment process too, most likely you don’t need gzipped files in development.

gzip via middleware may be (minimally) more convenient for you, in the short term, but all it would really accomplish would be to add extra cpu overhead and latency to your response times.

If you’re not serving your react assets from the public folder, how are you serving them?

Couple years old, but some good general info here:


@willvincent The bundle is built using Laravel-Mix@2.1.14. Unfortunately there is no such option in that package.

Through laravel-mix we have the ability to pass in Webpack configurations but the compression-webpack-plugin which performs the gzipping during the build process is only supported for Webpack 4 and up but laravel-mix is only supported for 3.1.


The 1.x.x versions of compression-webpack-plugin should work with webpack 3.x


Thank you very much. I downgraded compression-webpack-plugin and was able to get it working. I did not know about the down sides of on the fly Gzipping as opposed to doing it during the build phase.

The problem I now have is to get the .gz assets loaded instead of the regular .js ones. I would really appreciate your help @willvincent for finding the adonis equivalent for this Node.js snippet:


You shouldn’t gunzip your assets by yourself.
In real life situations is always better to charge it on your proxy server, like nginx.
You can use it along with app server, like Phusion Passenger.

It will be faster, easier for you and better for production.
It not so hard to set up, but if you really worried about your users experience and production environment quality, you definitely should do it.


That’s a good point… a proxy server is probably a better option.

I’m running my blog (Ghost-based) behind lightify reverse proxy which basically does a bunch of pagespeed-related enhancements on the fly. Could certainly use it or similar with an adonis app.

But gzip in middleware of your app definitely isn’t a good choice :slight_smile:


I wrote a provider that has a server middleware for compressing responses since I have a slightly different use case of trying to compress verbose JSONAPI payloads and not for serving up static assets. Like many have mentioned here, if your purpose is to serve static assets, don’t use Node.js.

But for JSONAPI use case, profiling bottlenecks in production at my company, I found that when I eagerload and include lot of nested relationships in the JSONAPI payload, the response body grows quite substantially (~500kb - 1Mb). Passing around such large response body actually takes more time than it takes for CPU cycles to compress it, making the tradeoff well worth it IMHO.

To install, just run:

adonis install @verdigris/adonis-compression


Hi, @bbrodriguez, Is it working different than native nginx compression?


@deromanenko it works similarly to the native nginx compression. In fact, lot of the configuration options for the middleware are similar (e.g. compression level, thresholds, compression algorithm, etc.). The adonis-compression provider itself it just a thin layer around the compression module.

In my use case, my API server sits behind a reverse proxy on another machine. My JSONAPI payload can get really verbose. So for my use case, the tradeoff between CPU cycles and I/O wait was worth it.

Performance optimizations are not as black and white or as clear cut. Everyone has different use cases and everyone’s situation will be different. Be sure to profile your code and architecture before deciding where it’s worth making the optimizations.