我已经压缩了webpack捆绑包,当我尝试提供它时,在客户端出现ERR_CONTENT_DECODING_FAILED错误。
这是我的中间件:

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


有人对发生的事情有任何想法吗?我尝试使用.header().setHeader()方法,但是也没有得到任何想要的结果。
提前致谢。

javascript - 无法使用Express正确提供压缩文件-LMLPHP

这是压缩插件:

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

最佳答案

我只是通过放置以下中间件声明来解决了这个问题:

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

app.use('/static', Express.static('dist'));之前。 (反之亦然)

10-05 20:41
查看更多