在使用vue-cli腳手架構建完項目,項目完成後,需打包上線。默認打包方式則是 npm build
,然後項目根目錄會生成 dist 文件夾。服務端將該文件夾替換線上即可。但是當第n(n>1)次上線後,由於在用戶端會默認緩存index.html入口文件,而由於vue打包生成的css/js都是哈希值,跟上次的文件名都不同,因此會出現找不到css/js的情況,導致白屏的產生。雖然刷新一下就好,但當用戶量大起來之後,這是一個非常巨大的問題了就。
雖然按照說法,使用meta標籤禁止使用緩存:如下
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">
但經過實際測試,根本沒用,該用緩存還是繼續使用,並沒有達到每次都發請求。
因此考慮每次發版把前一次的文件都保存起來,這樣即使調用緩存,請求之前的js~文件,也能找到對應的文件,不會出現白屏的狀況,用戶體驗大幅提升,同時接口向下兼容。
打包後的文件格式如下
具體操作:將static改爲攜帶版本號(從1開始遞增的證書)---------每次發版webpack導出文件爲staticN(static1,static2等樣式)---------刪除static(N-2)
代碼操作:在config/index.js裏面加上每次發版的版本號,還有webpack的導出地址由static改爲static+版本號
'use strict';
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.
var path = require('path');
const version = 2;
module.exports = {
version:version,
dev: {
// Paths
assetsSubDirectory: 'static' + version,
assetsPublicPath: '/',
proxyTable: {},
// Various Dev Server settings
// host: 'localhost', // can be overwritten by process.env.HOST
host: '0.0.0.0',
port: 6060, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
autoOpenBrowser: false,
errorOverlay: true,
notifyOnErrors: true,
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
// Use Eslint Loader?
// If true, your code will be linted during bundling and
// linting errors and warnings will be shown in the console.
useEslint: false,
// If true, eslint errors and warnings will also be shown in the error overlay
// in the browser.
showEslintErrorsInOverlay: false,
/**
* Source Maps
*/
// https://webpack.js.org/configuration/devtool/#development
devtool: 'cheap-module-eval-source-map',
// If you have problems debugging vue-files in devtools,
// set this to false - it *may* help
// https://vue-loader.vuejs.org/en/options.html#cachebusting
cacheBusting: true,
cssSourceMap: true,
},
build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static'+ version,
// assetsPublicPath: './',
assetsPublicPath: '/',
/**
* Source Maps
*/
productionSourceMap: false,
// https://webpack.js.org/configuration/devtool/#production
devtool: '#source-map',
// 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,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report,
},
checkout: {
productionSourceMap: true,
},
};
這樣就做到了每次發版都會生成staticN文件夾,不信你試試npm run build;
接下來就是刪除static(N-2)了,在build/build.js文件
注意這個插件rimarf,本用於刪除static文件夾
var rm = require('rimraf');
rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
if (err) throw err
我們將之加上版本號
rm(path.join(config.build.assetsRoot,'static'+(config.version-2)), err => {
這樣每次打包之後都會保留兩個版本的static文件夾,解決白屏