vue項(xiàng)目開啟Gzip壓縮和性能優(yōu)化操作
vue 項(xiàng)目開啟gzip自拍壓縮和部署 nginx 開啟gzip優(yōu)化性能
第一步:在vue項(xiàng)目中安裝依賴并將productionGzip改為true,開啟Gzip壓縮:
npm install --save-dev compression-webpack-plugin
第二步:運(yùn)行 npm run build打包項(xiàng)目,這時(shí)可能會(huì)報(bào)錯(cuò),提示ValidationError: Compression Plugin Invalid Options。
根據(jù)官網(wǎng)提示,需要將CompressionWebpackPlugin的設(shè)置由asset改為filename。
第三步:再次運(yùn)行 npm run build打包項(xiàng)目,這時(shí)可能會(huì)繼續(xù)報(bào)錯(cuò),提示TypeError: Cannot read property ’emit’ of undefined。據(jù)我查證,是安裝的compression-webpack-plugin依賴有問題,需要卸載compression-webpack-plugin更改安裝低版本 v1.1.12。
第四步:卸載當(dāng)前安裝的compression-webpack-plugin:
npm uninstall --save-dev compression-webpack-plugin
第五步:安裝低版本compression-webpack-plugin:
npm install --save-dev [email protected]
第六步:再次運(yùn)行 npm run build打包項(xiàng)目,這時(shí)將正常包vue項(xiàng)目,愉(ku)快(bi)的j將vue開發(fā)上線了。
第七步:開啟 nginx 服務(wù)端 gzip性能優(yōu)化。找到nginx配置文件在 http 配置里面添加如下代碼,然后重啟nginx服務(wù)即可。
http:{ gzip on; gzip_static on; gzip_buffers 4 16k; gzip_comp_level 5; gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;}
注意:過程中可能出現(xiàn)的報(bào)錯(cuò):
throw new ValidationError(ajv.errors, name); ^ValidationError: Compression Plugin Invalid Optionsoptions should NOT have additional properties at validateOptions (E:workspacewebpack-stackanalyze-demonode_modulescompression-webpack-pluginnode_modulesschema-utilssrcvalidateOptions.js:32:11)
building for production...E:workspacewebpack-stackanalyze-demonode_modulescompression-webpack-plugindistindex.js:175 compiler.hooks.emit.tapAsync({ name: ’CompressionPlugin’ }, emit); TypeError: Cannot read property ’emit’ of undefined at CompressionPlugin.apply (E:workspacewebpack-stackanalyze-demonode_modulescompression-webpack-plugindistindex.js:175:20)
補(bǔ)充知識(shí):vue填坑之webpack run build 靜態(tài)資源找不到
vue cli搭建的項(xiàng)目,在本地測(cè)試調(diào)試都OK,運(yùn)行npm run dev之后運(yùn)行正常,今天放到服務(wù)器上跑,結(jié)果RD說找不到打包后的靜態(tài)資源,瀏覽器控制臺(tái)錯(cuò)誤代碼404
問了RD,因?yàn)榉?wù)器上線方式的調(diào)整,不會(huì)指定具體項(xiàng)目路徑因此,
https://bigdata.yiche.com/static/css/app.149f36018149fcbe537f02cafdc6f047.css
這個(gè)文件找不到,看看我們正常打包好的目錄:
正確的訪問路徑是:
https://bigdata.yiche.com/deploy/static/css/app.149f36018149fcbe537f02cafdc6f047
config/index.js配置如圖:
思來想去之前打包好的文件直接扔到nginx就可以使用,實(shí)在不清楚原因。于是找到我們的美女組長(zhǎng)姐姐來幫忙,分分鐘改了config/index.js下的幾行代碼,如圖:
這里需要注意assetsPublicPath:’/deploy/’ 末尾的斜杠一定要加,不然部分js打包后會(huì)出現(xiàn)
https://bigdata.yiche.com/deploystatic/css/app.149f36018149fcbe537f02cafdc6f047
這樣的情況。
看下打包好的目錄,對(duì)比之后會(huì)發(fā)現(xiàn)多了一層deploy目錄,這個(gè)多出來的路徑是index和assetsRoot這兩個(gè)設(shè)置決定的
而assetsPublicPath則是確定打包后的文件引用路徑:看看打包后的index.html文件的js和css資源的引用路徑:
對(duì)比之前默認(rèn)配置的路徑:
好了再放到服務(wù)器上,問題解決了。
問題總結(jié):
原因是服務(wù)器沒有指定項(xiàng)目目錄,因此需要在打包時(shí)對(duì)打包文件添加訪問的項(xiàng)目名稱,所以在配置打包路徑是要加上項(xiàng)目名稱,下面是vue cli默認(rèn)webpack config/index.js的配置解釋
var path = require(’path’) module.exports = { build: { // production 環(huán)境 env: require(’./prod.env’), // 使用 config/prod.env.js 中定義的編譯環(huán)境 index: path.resolve(__dirname, ’../dist/index.html’), // 編譯輸入的 index.html 文件 assetsRoot: path.resolve(__dirname, ’../dist’), // 編譯輸出的靜態(tài)資源路徑 assetsSubDirectory: ’static’, // 編譯輸出的二級(jí)目錄 assetsPublicPath: ’/’, // 編譯發(fā)布的根目錄,可配置為資源服務(wù)器域名或 CDN 域名 productionSourceMap: true, // 是否開啟 cssSourceMap // Gzip off by default as many popular static hosts such as // Surge or Netlify already gzip all static assets for you. // Before setting to `true`, make sure to: // npm install --save-dev compression-webpack-plugin productionGzip: false, // 是否開啟 gzip productionGzipExtensions: [’js’, ’css’] // 需要使用 gzip 壓縮的文件擴(kuò)展名 }, dev: { // dev 環(huán)境 env: require(’./dev.env’), // 使用 config/dev.env.js 中定義的編譯環(huán)境 port: 8080, // 運(yùn)行測(cè)試頁面的端口 assetsSubDirectory: ’static’, // 編譯輸出的二級(jí)目錄 assetsPublicPath: ’/’, // 編譯發(fā)布的根目錄,可配置為資源服務(wù)器域名或 CDN 域名 proxyTable: {}, // 需要 proxyTable 代理的接口(可跨域) // CSS Sourcemaps off by default because relative paths are 'buggy' // with this option, according to the CSS-Loader README // (https://github.com/webpack/css-loader#sourcemaps) // In our experience, they generally work as expected, // just be aware of this issue when enabling this option. cssSourceMap: false // 是否開啟 cssSourceMap } }
本人個(gè)人理解,如有不對(duì)歡迎指出!
以上這篇vue項(xiàng)目開啟Gzip壓縮和性能優(yōu)化操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. Python 操作 MySQL數(shù)據(jù)庫3. Python數(shù)據(jù)相關(guān)系數(shù)矩陣和熱力圖輕松實(shí)現(xiàn)教程4. 開發(fā)效率翻倍的Web API使用技巧5. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼8. 什么是Python變量作用域9. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式10. python 如何在 Matplotlib 中繪制垂直線
