- <meta http-equiv="pragram" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate">
- <meta http-equiv="expires" content="0">
-
vue 默认配置,打包后 css 和 js 的名字后面都加了哈希值,不会有缓存问题,但是 index.html 在服务器端可能是有缓存的,需要在服务器配置不让缓存 index.html。
- location = /index.html {
- add_header Cache-Control "no-cache, no-store";
- }
-
- // 动态版本号
- const version = new Date().getTime()
- // 配置
- module.exports = {
- devServer: {},
- filenameHashing: false, // 打包的时候不使用 hash 值,因为后面自行添加时间戳或者版本号了
- css: {
- // 是否使用 css 分离插件 ExtractTextPlugin
- extract: {
- // 输出编译后的文件名称:【文件名称.时间戳】、【文件名称.版本号.时间戳】...
- filename: `css/[name].${version}.css`,
- chunkFilename: `css/[name].${version}.css`
- // filename: `css/[name].${process.env.VUE_APP_VERSION}.${version}.css`,
- // chunkFilename: `css/[name].${process.env.VUE_APP_VERSION}.${version}.css`
- }
- },
- configureWebpack: {
- output: { // 输出编译后的文件名称:【文件名称.时间戳】、【文件名称.版本号.时间戳】...
- filename: `js/[name].${version}.js`,
- chunkFilename: `js/[name].${version}.js`
- // filename: `js/[name].${process.env.VUE_APP_VERSION}.${version}.js`,
- // chunkFilename: `js/[name].${process.env.VUE_APP_VERSION}.${version}.js`
- }
- }
- }
-
- const date = new Date()
- const version = moment(date).format('YYYYMMDDHHmmssSSS') // 打包时候的版本号
- const timestamp = date.getTime() // 时间戳
- // 注意需下面这段放到配置导出中
- output: {
- path: config.build.assetsRoot,
- filename: utils.assetsPath(`js/[name].[chunkhash:8].${ version }.js?_t=${ timestamp }`),
- chunkFilename: utils.assetsPath(`js/[name].[chunkhash:8].${ version }.js?_t=${ timestamp }`)
- }
-
- // 在 build 时,每次创建/更新版本文件
- const version = require('./src/utils/version')
- version.create()
-
- import router from '@/router'
- const version = require('@/utils/version')
- // 路由跳转后执行
- router.afterEach((to, from) => {
- // 如果不想每个路由都检查是否有新版本,只需要在特定的页面才需要检查版本,可以在这里做白名单判断
- // 兼容版本,如果是新版本则进行刷新并缓存
- version.getPro()
- })
- // 路由跳转前执行
- router.beforeEach((to, from, next) => {
- next()
- })
-