安装compression-webpack-plugin插件
npm install --save-dev compression-webpack-plugin
安装后进行vue.config.js配置
在 chainWebpack 节点下添加以下配置
if (process.env.NODE_ENV === 'production') {
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['html','js', 'css']
return {
plugins: [
new CompressionWebpackPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
threshold: 10240, // 只有大小大于该值的资源会被处理 10240
minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
deleteOriginalAssets: false // 删除原文件true=删
})
]
}
}
在nginx添加完整配置
server {
listen 80;
server_name xx.xx.com;
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
location / {
# 指定文件的根目录,主要结尾不要带/
root /www/xx/web/dist;
# 指定默认跳转页面尾 /index.html
index index.html;
# 防止刷新后404
try_files $uri $uri/ /index.html;
}
}
修改配置后重启即可 经测试 原来5秒多现在一秒左右