vue 首屏优化 主要针对index.js过大问题,splitChunks分包白屏

本文介绍如何通过配置nginx开启gzip压缩、使用webpack-bundle-analyzer分析打包文件、利用CDN引入库文件、实施分包策略以及应用懒加载技术来显著减少前端项目加载时间。

接手了一个项目,打包完index.js居然有十几M,本地还好,部署线上以后
网速快的情况可能十几秒就能加载完,网速慢的话就会出现
“喂,你们项目挂了,白屏一直。。 哦!! 有了有了有了 挂了哈。”
慢到一定境界了。

查询了很多资料,发现原来webpack也不是那么那么的晦涩难懂,真用到的时候,
学学 用用还挺好玩。

  1. 首先是最管用的一招,就是直接在服务端nginx开启gzip压缩。贼好使,开了就管用。具体方法如下:
	gzip on;
	gzip_disable "msie6";
	gzip_static on;
	gzip_vary on;
	gzip_proxied any;
	gzip_comp_level 6;
	gzip_buffers 4 32k;
	gzip_http_version 1.1;
	gzip_min_length 1k;
	gzip_proxied expired no-cache no-store private auth;	
    gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg 
               image/gif image/png;

在这里插入图片描述
具体每一块代表的意思 我这里粘一下,但是其实我这段代码 可以直接粘过去用。

l gzip:决定是否开启gzip模块。

l gzip_disable:(IE5.5和IE6 SP1使用msie6参数来禁止gzip压缩)。指定哪些不需要gzip压缩的浏览器(将和User-Agents进行匹配)。

l gzip_vary:增加响应头”Vary: Accept-Encoding”。

l gzip_proxied:Nginx做为反向代理的时候启用:·

off –关闭所有的代理结果数据压缩· expired –如果header中包含”

Expires”头信息,启用压缩· no-cache –如果header中包含”Cache-Control:no-cache”头信息,启用压缩·

no-store –如果header中包含”Cache-Control:no-store”头信息,启用压缩·

private –如果header中包含”Cache-Control:private”头信息,启用压缩·

no_last_modified –启用压缩,如果header中包含”Last_Modified”头信息,启用压缩·

no_etag –启用压缩,如果header中包含“ETag”头信息,启用压缩·

auth –启用压缩,如果header中包含“Authorization”头信息,启用压缩·

any –无条件压缩所有结果数据

l gzip_types:这个参数是指设置需要压缩的MIME类型,如果不在设置类型范围内的请求不进行压缩。lgzip_comp_level:设置gzip压缩等级,等级越压缩速度越快文件压缩比越小,反之速度越慢文件压缩比越大。范围是1-9,根据需求设定。

l gzip_buffers:设置用于处理请求压缩的缓冲区数量和大小。比如32 4K表示按照内存页(one memory page)大小以4K为单位(即一个系统中内存页为4K),申请32倍的内存空间。建议此项不设置,使用默认值。

l gzip_http_version:用于识别http协议的版本,早期的浏览器不支持gzip压缩,用户会看到乱码,所以为了支持前期版本加了此选项。默认在http/1.0的协议下不开启gzip压缩。

在自己的nginx服务器的server上方加上gzip的这些配置,重启nginx就可以了。

  1. webpack-bundle-analyze
const { join, resolve } = require('path'); const path = require('path') const fs = require('fs-extra'); const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const scenarioNames = fs.readdirSync(join('./src/scenarios')).filter(name => !name.startsWith('.')); const VueLoaderPlugin = require('vue-loader/lib/plugin'); const CopyWebpackPlugin = require("copy-webpack-plugin"); module.exports = ({ onGetWebpackConfig }) => { onGetWebpackConfig((config) => { config.plugin('VueLoaderPlugin').use(VueLoaderPlugin) config.merge({ module: { rule: [ { test: /\.vue$/, loader: 'vue-loader' } ] }, }); config.resolve.plugin('tsconfigpaths').use(TsconfigPathsPlugin, [ { configFile: './tsconfig.json', }, ]); config.merge({ node: { fs: 'empty', }, }); config.merge({ resolve: { alias: { "@": path.resolve(__dirname, "./src") } }, }); scenarioNames.forEach(name => { const hasTsx = fs.existsSync(join(`./src/scenarios/${name}/index.tsx`)); config.merge({ entry: { [name]: hasTsx ? require.resolve(`./src/scenarios/${name}/index.tsx`) : require.resolve(`./src/scenarios/${name}/index.ts`), }, }); config .plugin(name) .use(HtmlWebpackPlugin, [ { inject: false, minify: false, templateParameters: { scenario: name, version: new Date().getTime() }, template: require.resolve('./public/index.ejs'), filename: `${name}.html`, }, ]); }) config .plugin('preview') .use(HtmlWebpackPlugin, [ { inject: false, templateParameters: { version: new Date().getTime() }, template: require.resolve('./public/preview.html'), filename: 'preview.html', }, ]); config .plugin('archive') .use(HtmlWebpackPlugin, [ { inject: false, templateParameters: { version: new Date().getTime() }, template: require.resolve('./public/archive.html'), filename: 'archive.html', }, ]); config .plugin('eaccountingArchive') .use(HtmlWebpackPlugin, [ { inject: false, templateParameters: { version: new Date().getTime() }, template: require.resolve('./public/eaccountingArchive.html'), filename: 'eaccountingArchive.html', }, ]); config .plugin('appending') .use(HtmlWebpackPlugin, [ { inject: false, templateParameters: { version: new Date().getTime() }, template: require.resolve('./public/appending.html'), filename: 'appending.html', }, ]); config.plugin('CopyWebpackPlugin').tap(([args]) => { return [ [ { ...(args[0] || {}), }, { from: "./node_modules/prop-types/prop-types.js", to: "./" } ] ] }) config.plugins.delete('hot'); config.devServer.hot(false); const proxyHost = "http://kwesit.huawei.com"; // siT环境 config.devServer.proxy({ '/*': {}, '/icp/comment': { target: proxyHost, // logLevel: 'debug', changeOrigin: true, secure: false }, '/fin/front/one/gw': { target: proxyHost, changeOrigin: true, secure: false }, '/efinance': { target: proxyHost, changeOrigin: true, secure: false }, // '/efinance/icp/designstudio/services/jalor/lookup':{ // target: proxyHost, // changeOrigin: true, // secure: false // }, }); // config.optimization.minimize(true), config.module // fixes https://github.com/graphql/graphql-js/issues/1272 .rule('mjs$') .test(/\.mjs$/) .include .add(/node_modules/) .end() .type('javascript/auto'); config.optimization .splitChunks({ name: 'vendor', chunks: 'all', cacheGroups: { vendor: { test: /node_modules/, chunks: 'initial', name: 'vendor', enforce: true, }, }, }); }); }; 上面的代码添加 config.optimization .splitChunks({ name: 'vendor', chunks: 'all', cacheGroups: { vendor: { test: /node_modules/, chunks: 'initial', name: 'vendor', enforce: true, }, }, });后页面白屏了,没有错误信息,为什么?怎么解决?
最新发布
06-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值