Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serve gzip files in node throws net::ERR_CONTENT_DECODING_FAILED in browser

I'm trying to follow this in order to serve some static compressed files. https://medium.com/@rajaraodv/two-quick-ways-to-reduce-react-apps-size-in-production-82226605771a

I'm using webpack compression plugin to generate the gzip files.

new CompressionPlugin({
    asset: '[path].gz[query]',
    algorithm: 'gzip',
    test: /\.(js|css)$/,
    deleteOriginalAssets: true
})

On my server I have this middleware.

app.get('*.js', function (req, res, next) {
    req.url = req.url + '.gz';
    res.set('Content-Encoding', 'gzip');
    next();
});

When I'm running the app, in browser I'm getting GET http://localhost:8080/app.js net::ERR_CONTENT_DECODING_FAILED

Probably, I still have to do something more but no idea what exactly.

Thank you, Ionut

like image 559
Ionut Cirja Avatar asked Feb 05 '26 17:02

Ionut Cirja


1 Answers

Looks like you're missing the content type. Use this code in your express server:

app.get('*.js', function(req, res, next) {
  req.url = req.url + '.gz';
  res.set('Content-Encoding', 'gzip');
  res.set('Content-Type', 'text/javascript');
  next();
});

app.get('*.css', function(req, res, next) {
  req.url = req.url + '.gz';
  res.set('Content-Encoding', 'gzip');
  res.set('Content-Type', 'text/css');
  next();
});
like image 200
Isaac Pak Avatar answered Feb 07 '26 05:02

Isaac Pak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!