As a developer, you likely want to squeeze every unnecessary bit out of your Nuxt.js app. To accomplish this there are lots of nifty tools: From code-splitting over caching to compression. This post focuses on the latter.
If you’ve ever heard compression related to the web,
Other compression methods were introduced in that time but for long none was able to show up better compression results, browser support, and compression speed.
This changed when
So, why shouldn’t we use
Already earlier in 2018 I thought this might be a good idea and now we are here! Adding
Important notice: In general, I highly recommend to configure your platform provider, e.g. Heroku or AWS, or web server, like Apache or NGINX, to handle Brotli and GZIP compression because it will be way more performant. If you can’t for any reason, then setting it up in Nuxt is a decent option.
Every time Nuxt.js renders a page it will call a bunch of middleware (f.ex. the error middleware to handle possible server-side errors). An important middleware that will be called is the compression middleware which will, as the name hints, compress the response. By default, the
This package does not support
What if we could swap out the middleware for another… would this solve our problem? At least it would bring us one step further to the goal.
And as I said above, with Nuxt 2 it’s possible.
Now we have to find a suitable middleware!
During my research, I found a package called shrink-ray which is actually a fork of the
Alright, all prerequisites fulfilled. Let’s get into the code!
First, install the packages (
Now edit your
import shrinkRay from 'shrink-ray-current'
export default {
render: {
compressor: shrinkRay()
}
}
Be careful to actually invoke
And that’s it! Start your app (in production mode, otherwise no compression will take place) and open your browser. Jump into your developer tools, select your network tab and reload the page. Now click the table header in the network tab and add the
If you see

Firefox network tab displaying different scripts loaded with Brotli compression
Oh right, before we forget it! What happens if a user with an older browser (say IE 11) wants to connect to our app? Will they get an error?
No! That’s not a problem. Our
If you are using a proxy (likely the
I usually create a mapping from
export default {
modules: ['@nuxtjs/axios'],
render: {
compressor: shrinkRay()
},
// ...
proxy: {
'/api/': { target: 'api.myurl.com', pathRewrite: { '^/api/': '' } }
}
// ...
}
You now have to exclude all requests starting with
export default {
modules: ['@nuxtjs/axios'],
render: {
compressor: shrinkRay({
filter: (req, res) => {
if (/^\/api/.test(req.originalUrl)) {
return false
}
return shrinkRay.filter(req, res)
}
})
},
// ...
proxy: {
'/api/': { target: 'api.myurl.com', pathRewrite: { '^/api/': '' } }
}
// ...
}
Now we exclude all
To ensure that Brotli is enabled on your site now, you can use an online tool called Brotli.Pro which will tell you whether your website supports Brotli compression or not!
UPDATE: Outdated!
Now you might think the next would be setting up
On the client-side, axios will delegate the decoding to the browser (and most modern browser support
Once again, we did it! As a personal summary, I’ve decreased my blog’s index page size from 210kB to 170kB, which is a total of 19% decrease in size.
As soon as you finished your setup (which should be soon because you reached the end of the post) be sure to tell me how many kilobytes you saved!
I’ve uploaded a sample setup for you on GitHub. All you have to do is to clone the repository, install the package and you are good to go!
All in all, I hope this article helped you a little. If so it would be awesome if you could spread the word (for example by using the buttons below the article).
Questions left? Critics? Have you successfully stepped through the setup?
Hit me up on Twitter (@TheAlexLichter) or write me a mail (blog at lichter dot io).
Originally published at September 9, 2018

I'm Alex, a German web engineering consultant and content creator. Helping companies with my experience in TypeScript, Vue.js, and Nuxt.js is my daily business.
More about meJamstack is a growing and modern web architecture. I gradually migrated several Nuxt.js projects from server side rendering over to JAMstack and write about my experiences, recommendations and the migration process itself.
There are three common ways to integrate an API with Nuxt. In this blog post, I'll share my personal opinion regarding all of them, my typical procedure when deciding for one approach and the benefits and disadvantages for each of them.