最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue項(xiàng)目開啟Gzip壓縮和性能優(yōu)化操作

 更新時(shí)間:2020年10月26日 09:33:00   作者:溫九月  
這篇文章主要介紹了vue項(xiàng)目開啟Gzip壓縮和性能優(yōu)化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

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 compression-webpack-plugin@1.1.2

第六步:再次運(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 Options
options should NOT have additional properties
 at validateOptions (E:\workspace\webpack-stack\analyze-demo\node_modules\compression-webpack-plugin\node_modules\schema-utils\src\validateOptions.js:32:11)
building for production...E:\workspace\webpack-stack\analyze-demo\node_modules\compression-webpack-plugin\dist\index.js:175
 compiler.hooks.emit.tapAsync({ name: 'CompressionPlugin' }, emit);
 
TypeError: Cannot read property 'emit' of undefined
 at CompressionPlugin.apply (E:\workspace\webpack-stack\analyze-demo\node_modules\compression-webpack-plugin\dist\index.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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

黄浦区| 韶山市| 古交市| 沙湾县| 区。| 绥宁县| 五大连池市| 腾冲县| 双峰县| 曲松县| 朔州市| 德化县| 永城市| 成都市| 南木林县| 时尚| 上虞市| 罗源县| 陆川县| 龙山县| 嵩明县| 冕宁县| 金川县| 河源市| 津市市| 瑞金市| 通化县| 莱芜市| 松原市| 达尔| 天水市| 陇川县| 潮安县| 于都县| 藁城市| 金川县| 钦州市| 七台河市| 宣恩县| 涞水县| 凤山县|