[Solved] Can I serve gzipped client files from Glitch Node/Express app?


We have a Node/Express app that exposes a REST-ish API and serves a client SPA (react build) to consume it. To reduce client build size I’d love to have Glitch serve the gzipped version of our build.

Can this be done? And if ‘yes’, are there instructions on doing so?


From a quick search, I found two modules that can help you with this.

  1. compression: much more popular, supports gzip. Usage:
    var compression = require('compression')
    // ...
  2. shrink-ray-current: a less-popular fork of compression that supports Brotli, a newer compression aglorithm. It also caches the response at a higher compression for future requests, resulting in smaller sizes for both Brotli and gzip. Usage:
    const shrinkRay = require('shrink-ray-current');
    // ...


Thanks @j-f

But those libraries appear to compress “on the fly”. I have pre-compressed build/bundle files that I just want to serve.

Having read this SO answer:

I wonder if I could mod that into some basic middleware…


So it turns out that the above is correct - all it takes is adding a little middleware to intercept and modify requests for specific assets.

One caveat: I needed to define this middleware BEFORE using express.static:

// middleware to convert requests for .js files > gzipped
app.get('*.js', (req, res, next) => {
    req.url = req.url + '.gz';
    res.set('Content-Encoding', 'gzip');
    res.set('Content-Type', 'text/javascript');

// set static path
app.use(express.static(path.join(__dirname, '/client/build/')));


@j-f – I think your solution (compression) is a better idea than mine. My middleware only handles requests for .js files. compression can/will compress all http responses greater than 1kB. That’s a whole lotta nice utility!

Thank you for pointing me in the right direction.