前端:gzip可以优化http请求的,提高加载速度 第一步:下载 npm install compression-webpack-plugin 第二步:导入 第三步:在cofig.plugins.push里进行配置,不如文件的大小,是否要缓存,匹配的文件扩展名 后端:通过nginx进行配置gzip压缩 gzip压缩的原理:客户端向服务端发送请求的时候,会在请求头上加上accept-encoding:gzip或者是flate,服务端响应的
npm install compression-webpack-plugin --save-dev
const CompressionPlugin = require("compression-webpack-plugin");
// 开启gzip压缩
config.plugins.push(new CompressionPlugin({
algorithm: 'gzip',
test: new RegExp("\\.(" + ["js", "css"].join("|") + ")$"), // 匹配文件扩展名
// threshold: 10240, // 对超过10k的数据进行压缩
threshold: 5120, // 对超过5k的数据进行压缩
minRatio: 0.8,
cache: true, // 是否需要缓存
deleteOriginalAssets:false // true删除源文件(不建议);false不删除源文件
}))