Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在React服务器中提供gzip文件_Javascript_Reactjs_Express_Webpack_Gzip - Fatal编程技术网

Javascript 在React服务器中提供gzip文件

Javascript 在React服务器中提供gzip文件,javascript,reactjs,express,webpack,gzip,Javascript,Reactjs,Express,Webpack,Gzip,实际上,我在React中为bundle.js文件提供gzip压缩时遇到了问题。我正试图大幅缩小此文件的大小,我已经完成了丑陋和重复数据消除等工作。。。它从2.9mb降到了2.6mb,这对我来说真的很奇怪。我现在使用压缩插件,得到了一个输出的gzip文件,但现在我仍然在为bundle.js服务,而不是bundle.js.gz 我不想使用express中的压缩中间件,因为我正在构建过程中使用gzip。无论如何,这是我的distServer文件: import express from 'expres

实际上,我在React中为bundle.js文件提供gzip压缩时遇到了问题。我正试图大幅缩小此文件的大小,我已经完成了丑陋和重复数据消除等工作。。。它从2.9mb降到了2.6mb,这对我来说真的很奇怪。我现在使用压缩插件,得到了一个输出的gzip文件,但现在我仍然在为bundle.js服务,而不是bundle.js.gz

我不想使用express中的压缩中间件,因为我正在构建过程中使用gzip。无论如何,这是我的distServer文件:

import express from 'express';
import path from 'path';
import open from 'open';

/* eslint-disable no-console */

const port = process.env.PORT || 3000;
const app = express();


app.use(express.static('dist'));

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

app.get('*', function(req, res) {
  res.sendFile(path.join( __dirname, '../dist/index.html'));
});

app.listen(port, function(err) {
  if (err) {
    console.log(err);
  } else {
    open(`http://localhost:${port}`);
  }
});
我的网页配置是:

import webpack from 'webpack';
import path from 'path';
import ExtractTextPlugin from 'extract-text-webpack-plugin';

var CopyWebpackPlugin = require('copy-webpack-plugin');
var CompressionPlugin = require("compression-webpack-plugin");
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

export default {
  devtool: 'source-map',
  noInfo: false,
  entry: [
    './src/index'
  ],
  target: 'web',
  output: {
    path: __dirname + '/dist', // Note: Physical files are only output by the production build task `npm run build`.
    publicPath: '/',
    filename: 'bundle.js'
  },
  plugins: [
    // new BundleAnalyzerPlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.DefinePlugin({
      'process.env': {NODE_ENV: '"production"'}
    }),
    new ExtractTextPlugin('styles.css'),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.UglifyJsPlugin({
      mangle: true,
      compress: {
        warnings: false, // Suppress uglification warnings
        pure_getters: true,
        unsafe: true,
        unsafe_comps: true,
        screw_ie8: true
      },
      output: {
        comments: false,
      },
      exclude: [/\.min\.js$/gi] // skip pre-minified libs
    }),
    new CopyWebpackPlugin([
      { from: 'src/robots.txt', to: 'robots.txt' },
      { from: 'src/sitemap.xml', to: 'sitemap.xml' }
    ], {
      copyUnmodified: true
    }),
    new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/]),
    new CompressionPlugin({
      asset: "[path].gz[query]",
      algorithm: "gzip",
      test: /\.js$|\.css$|\.html$/,
      threshold: 10240,
      minRatio: 0
    })
  ],
  module: {
    loaders: [
      {test: /\.js$/, include: path.join(__dirname, 'src'), loaders: ['babel']},
      {test: /(\.css)$/, loader: ExtractTextPlugin.extract("css?sourceMap")},
      {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file'},
      {test: /\.(woff|woff2)$/, loader: 'url?prefix=font/&limit=5000'},
      {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream'},
      {test: /\.(jpe?g|png|gif|svg)$/i, loader: "url?limit=10000"},
      {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml'},
      {
        test: /favicon\.ico$/,
        loader: 'url',
        query: {
          limit: 1,
          name: '[name].[ext]',
        },
      }
    ]
  }
};
我以为app.get函数会根据需要引入这些gzip文件,但我可能错过了一步?另外,在我的index.html文件中,我是否需要省略将捆绑包文件放在一起的脚本标记


任何指导和想法都将不胜感激

听起来您首先要验证是否正在提供gzip文件,这可以通过Chrome DevTools轻松完成

打开Chrome开发工具,进入
性能
选项卡,并配置您的网站。加载后,检查网络请求部分,找到要验证的Javascript文件正在gzip。如果操作正确,您将看到,
编码数据
是通过导线发送的(希望是压缩的)数据的大小,
解码体
是未压缩数据的大小

如果两者相等,则可以正确地进行压缩,但不能将其发送过来。


我建议您在反向代理级别处理此问题,最好是使用服务于gzip文件。无论如何,您确实应该使用NGINX来提供静态内容,因为它将减轻节点服务器的负载

您应该将静态列表放在中间件之后

import express from 'express';
import path from 'path';
import open from 'open';

/* eslint-disable no-console */

const port = process.env.PORT || 3000;
const app = express();


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

app.use(express.static('dist'));

app.get('*', function(req, res) {
  res.sendFile(path.join( __dirname, '../dist/index.html'));
});

app.listen(port, function(err) {
  if (err) {
    console.log(err);
  } else {
    open(`http://localhost:${port}`);
  }
});

使用我在上面写的压缩中间件,我不想使用压缩中间件,因为我在构建阶段构建gzip。