您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Vue如何實現(xiàn)首屏加載過慢出現(xiàn)長時間白屏效果”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“Vue如何實現(xiàn)首屏加載過慢出現(xiàn)長時間白屏效果”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
公司業(yè)務展示官網(wǎng)開發(fā),構建版本后在外網(wǎng)測試環(huán)境下,發(fā)下首屏加載損耗高達幾十秒(服務器在國外,所以也導致加載時間變長),于是采用了以下方法來達到提速目的。
問題攻克解決步驟如下:
- 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;預渲染
- 在首頁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
- 用到的插件: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; }
- 預渲染解決百度搜索引擎抓爬不到單頁面子鏈接問題。可以把需要seo頁面 寫在頁面中 隱藏起來。
<div > <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有個過大的文件 注意首屏引入的資源大小
讀到這里,這篇“Vue如何實現(xiàn)首屏加載過慢出現(xiàn)長時間白屏效果”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內(nèi)容的文章,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。