Vue首屏加載過慢出現(xiàn)長時間白屏的實現(xiàn)
需求場景
公司業(yè)務展示官網(wǎng)開發(fā),構建版本后在外網(wǎng)測試環(huán)境下,發(fā)下首屏加載損耗高達幾十秒(服務器在國外,所以也導致加載時間變長),于是采用了以下方法來達到提速目的。
問題攻克解決步驟如下:
1. 采用懶加載的方式(官網(wǎng)有所提及,這里就不做詳細描述)
2. webpack開啟gzip壓縮文件傳輸模式:
- gizp壓縮是一種http請求優(yōu)化方式,通過減少文件體積來提高加載速度。html、js、css文件甚至json數(shù)據(jù)都可以用它壓縮,可以減小60%以上的體積。 - webpack打包時借助 compression webpack plugin實現(xiàn)gzip壓縮,安裝插件如下:npm i -D compression-webpack-plugin - 在vue-cli 3.0 中,vue.config.js配置如下:
const CompressionPlugin = require('compression-webpack-plugin');//引入gzip壓縮插件 module.exports = { plugins:[ new CompressionPlugin({//gzip壓縮配置 test:/.js$|.html$|.css/,//匹配文件名 threshold:10240,//對超過10kb的數(shù)據(jù)進行壓縮 deleteOriginalAssets:false,//是否刪除原文件 }) ] }
- 服務器nginx開啟gzip:
gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; #壓縮級別:1-10,數(shù)字越大壓縮的越好,時間也越長 gzip_buffers 16 8k; gzip_http_version 1.1; gzip_min_length 256; #gzip壓縮最小文件大小,超出進行壓縮(自行調(diào)節(jié)) gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;預渲染
3.依賴模塊采用第三方cdn資源
- 在首頁index.html中引入,如:
<script?src="cdn.bootcss.com/vue/2.6.10/…">? <script?src="cdn.bootcss.com/vuex/3.0.1/…"> <script?src="cdn.bootcss.com/vue-router/…"> <script?src="cdn.bootcss.com/element-ui/…">
- 修改vue.config.js,關于externals配置請自行查閱相關資料。
module.exports = { ... externals: { 'vue': 'Vue', 'vuex': 'Vuex', 'vue-router': 'VueRouter', 'element-ui': 'ELEMENT' } ... }
- 修改 src/store/index.js
... // 注釋掉 // Vue.use(Vuex) ...
- 修改 src/router/index.js
// import Vue from 'vue' import VueRouter from 'vue-router' // 注釋掉 // Vue.use(VueRouter) ...
- 修改 src/main.js
import Vue from 'vue' import ELEMENT from 'element-ui' import App from './App.vue' import router from './router' import store from './store' import 'mint-ui/lib/style.css' import echarts from 'echarts' import download from './mixin/download' import NProgress from 'nprogress' import 'nprogress/nprogress.css' import '@/static/css/reset.css' import '@/static/css/font.css' import '@/assets/fonts/font.css' Vue.config.productionTip = false Vue.use(ELEMENT) router.afterEach(() => { NProgress.done() }) new Vue({ router, store, render: h => h(App), // 添加mounted,不然不會執(zhí)行預編譯 mounted () { document.dispatchEvent(new Event('render-event')) } }).$mount('#app') 這里element-ui變量名要用ELEMENT, 因為element-ui的umd模塊名是ELEMENT
4. 預渲染
- 用到的插件:prerender-spa-plugin
yarn add prerender-spa-plugin -D or npm install prerender-spa-plugin --save-dev
- vue.config.js中配置如下:
const PrerenderSpaPlugin = require('prerender-spa-plugin'); const Render = PrerenderSpaPlugin.PuppeteerRenderer; const path = require('path'); configureWebpack: () => { if (process.env.NODE_ENV !== 'production') return; return { plugins: [ new PrerenderSPAPlugin({ // 生成文件的路徑,也可以與webpakc打包的一致。 // 下面這句話非常重要?。。? // 這個目錄只能有一級,如果目錄層次大于一級,在生成的時候不會有任何錯誤提示,在預渲染的時候只會卡著不動。 staticDir: path.join(__dirname, 'dist'), // 對應自己的路由文件,比如a有參數(shù),就需要寫成 /a/param1。 routes: ['/', '/Login', '/Home'], // 這個很重要,如果沒有配置這段,也不會進行預編譯 renderer: new Renderer({ inject: { foo: 'bar' }, headless: false, // 在 main.js 中 document.dispatchEvent(new Event('render-event')),兩者的事件名稱要對應上。 renderAfterDocumentEvent: 'render-event' }) }) ] }; }
- main.js中配置:
new Vue({ router, store, render: h => h(App), // 添加mounted,不然不會執(zhí)行預編譯 mounted () { document.dispatchEvent(new Event('render-event')) } }).$mount('#app')
- 首頁加載一般進入的是路由首頁,可以通過nginx配置,指向預渲染的首頁靜態(tài)頁,nginx配置如下:
location = / { root /data/release/pokio_web/client/dist; try_files /home/index.html /index.html; } location / { root /data/release/pokio_web/client/dist; try_files $uri $uri/ /index.html; }
5.遇見的問題:
- 預渲染解決百度搜索引擎抓爬不到單頁面子鏈接問題。可以把需要seo頁面 寫在頁面中 隱藏起來。
<div style="display: none;"> <a href="/about/about-.../" rel="external nofollow" >...</a> <a href="/home/" rel="external nofollow" >home</a> <a href="/clubs/" rel="external nofollow" >home</a> </div>
- build后發(fā)現(xiàn)app.js還是很大:首屏引入的資源 svg有個過大的文件 注意首屏引入的資源大小
結(jié)束語
整個流程跑通后 首屏加載有了質(zhì)的飛躍國外的服務器在國內(nèi)加載首屏大概用了3s時間左右
到此這篇關于Vue首屏加載過慢出現(xiàn)長時間白屏的實現(xiàn)的文章就介紹到這了,更多相關Vue首屏加載過慢白屏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue實現(xiàn)手機號、驗證碼登錄(60s禁用倒計時)
這篇文章主要介紹了Vue實現(xiàn)手機號、驗證碼登錄(60s禁用倒計時),幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2020-12-12vue+element-ui?校驗開始時間與結(jié)束時間的實現(xiàn)代碼
這篇文章主要介紹了vue+element-ui?校驗開始時間與結(jié)束時間的代碼實現(xiàn),最主要的需求是開始時間不能早于當前時間,感興趣的朋友跟隨小編一起看看吧2024-07-07vue+echarts+datav大屏數(shù)據(jù)展示及實現(xiàn)中國地圖省市縣下鉆功能
這篇文章主要介紹了vue+echarts+datav大屏數(shù)據(jù)展示及實現(xiàn)中國地圖省市縣下鉆,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11vue采用EventBus實現(xiàn)跨組件通信及注意事項小結(jié)
EventBus是一種發(fā)布/訂閱事件設計模式的實踐。這篇文章主要介紹了vue采用EventBus實現(xiàn)跨組件通信及注意事項,需要的朋友可以參考下2018-06-06