user8685433
user8685433

Reputation: 398

Can't serve gzipped files properly using Express

I have gzipped webpack bundle, and when I try to serve it, I get ERR_CONTENT_DECODING_FAILED error on the client-side. Here is my middleware:

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

Does anybody have any idea about what's happening? I've tried to use .header() or .setHeader() methods, but also haven't got any desired result. Thanks in advance.

Here is plugin for compression:

new CompressionPlugin({
      asset: "[path].gz[query]",
      algorithm: "gzip",
      test: /\.js$/,
      threshold: 10240,
      minRatio: 0.8
    })

Upvotes: 0

Views: 1419

Answers (1)

user8685433
user8685433

Reputation: 398

I've solved this simply just by putting this midleware declaration:

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

before app.use('/static', Express.static('dist'));. (Earlier it was vice versa)

Upvotes: 2

Related Questions