在节点中服务gzip文件在浏览器中抛出net :: ERR_CONTENT_DECODING_FAILED

我试图按照这个为了服务一些静态压缩文件。 https://medium.com/@rajaraodv/two-quick-ways-to-reduce-react-apps-size-in-production-82226605771a

我正在使用webpack压缩插件来生成gzip文件。

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

在我的服务器上,我有这个中间件。

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

当我运行的应用程序,在浏览器中,我得到GET http:// localhost:8080 / app.js net :: ERR_CONTENT_DECODING_FAILED

也许,我还需要做更多的事情,但不知道究竟是什么。

谢谢,Ionut

看起来你错过了内容types。 在快递服务器中使用以下代码:

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