最近部署的vue项目,发现页面渲染速度越来越慢,打开开发者工具发现,由于之前使用
config.optimization.splitChunks({
chunks: 'all',
// maxSize: 2000000,
cacheGroups: {
libs: {
name: 'chunk-libs',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial' // only package third parties that are initially dependent
},
elementUI: {
name: 'chunk-elementUI', // split elementUI into a single package
priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
},
monacoEditor: {
chunks: 'async',
name: 'chunk-monacoEditor',
priority: 19,
test: /[\\/]node_modules[\\/]_?monaco-editor(.*)/,
},
commons: {
name: 'chunk-commons',
test: path.resolve(__dirname, 'src/components'),
minChunks: 3, // minimum common number
priority: 5,
reuseExistingChunk: true
},
}
})
配置编译文件,导致chunk-libs.js被分割成了很多小文件,但是也因为这个原因使得chunk-libs.js的加载越到后面越慢。
然后我百度后使用
new CompressionWebpackPlugin({
filename: '[path].gz[query]', // 目标文件名
algorithm: 'gzip', // 压缩算法
test: productionGzipExtensions, // 满足正则表达式的文件会被压缩
threshold: 10240, // 只处理比这个值大的资源。按字节计算 10240=10KB
minRatio: 0.8, // 只有压缩率比这个值小的资源才会被处理
deleteOriginalAssets: true // 是否删除原资源
})
希望可以使用gzip压缩编译文件,可以使项目整体体积变小,以达到加速页面渲染的目的,但不知道为什么使用nginx部署后,一直提示Uncaught SyntaxError: Unexpected token ‘<’
,也没有找到解决办法,最后查到了可以使用nginx自带的压缩功能。
首先配置打开gzip支持
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
然后在server中配置gzip_static on;
即可
location / {
gzip_static on;
root D:/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
完成配置后,页面渲染的速度确实快了不少,记一下,以防以后忘记了!