【问题标题】:Serve gzip files in node throws net::ERR_CONTENT_DECODING_FAILED in browser在节点中提供 gzip 文件会在浏览器中抛出 net::ERR_CONTENT_DECODING_FAILED
【发布时间】:2017-09-17 05:38:35
【问题描述】:

我正在尝试遵循这一点,以便提供一些静态压缩文件。 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.jsnet::ERR_CONTENT_DECORDING_FAILED

可能,我还需要做更多的事情,但不知道具体是什么。

谢谢你,Ionut

【问题讨论】:

    标签: node.js webpack gzip


    【解决方案1】:

    您似乎缺少内容类型。在您的快速服务器中使用此代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-23
      • 1970-01-01
      • 2018-03-24
      • 2023-03-05
      • 2017-12-09
      • 1970-01-01
      • 2013-11-10
      • 2021-02-16
      相关资源
      最近更新 更多