在使用vue-cli脚手架构建完项目,项目完成后,需打包上线。默认打包方式则是 npm build
,然后项目根目录会生成 dist 文件夹。服务端将该文件夹替换线上即可。但是当第n(n>1)次上线后,由于在用户端会默认缓存index.html入口文件,而由于vue打包生成的css/js都是哈希值,跟上次的文件名都不同,因此会出现找不到css/js的情况,导致白屏的产生。虽然刷新一下就好,但当用户量大起来之后,这是一个非常巨大的问题了就。
虽然按照说法,使用meta标签禁止使用缓存:如下
但经过实际测试,根本没用,该用缓存还是继续使用,并没有达到每次都发请求。
因此考虑每次发版把前一次的文件都保存起来,这样即使调用缓存,请求之前的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文件夹,解决白屏