Serve Gzip Files in Node Throws Net: :Err_Content_Decoding_Failed in Browser

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.

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 net::ERR_CONTENT_DECODING_FAILED

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

Thank you, Ionut

1 Answer

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();
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.