清除前端缓存的方式

1.定义

浏览器缓存(Browser Caching)是为了节约网络的资源加速浏览,浏览器在用户磁盘上对最近请求过的文档进行存储,当访问者再次请求这个页面时,浏览器就可以从本地磁盘显示文档,这样就可以加速页面的阅览。

2.类型

缓存协商:Last-modified ,Etag

彻底缓存:cache-control,Expires

3.解决方案(以vue举例)

①.修改 webpack.prod.conf.js 文件

const date = new Date();

const timestamp = date.getTime(); // 时间戳

const version = timestamp; // 打包时候的版本号

var webpackConfig = merge(baseWebpackConfig, {

module: {

rules: utils.styleLoaders({

sourceMap: config.build.productionSourceMap,

extract: true

})

},

devtool: config.build.productionSourceMap ? '#source-map' : false,

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 }`)

},

}

②. html 页面前面加 meta 标签,让所有的css/js资源重新加载

<meta http-equiv="pragram" content="no-cache">

<meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate">

<meta name="viewport" content="width=device-width,initial-scale=1.0">

③. 后端服务器 nginx 配置,让 index.html 不缓存

location = /index.html { add_header Cache-Control "no-cache, no-store"; }

④.vue.config.js或者 vue_loader.config.js

const version = new Date().getTime();

module.exports = {

loaders: utils.cssLoaders({

sourceMap: isProduction

? config.build.productionSourceMap

: config.dev.cssSourceMap,

extract: isProduction

}),

css: {

// 是否使用css分离插件 ExtractTextPlugin

extract: {

// 修改打包后css文件名 // css打包文件,添加时间戳

filename: `css/[name].${version}.css`,

chunkFilename: `css/[name].${version}.css`

}

},

configureWebpack: {

output: { // 输出重构 打包编译后的 文件名称 【模块名称.版本号.时间戳】

filename: `[name].{process.env.VUE_APP_Version}.{version}.js`,

chunkFilename: `[name].{process.env.VUE_APP_Version}.{version}.js`

}

}

}

相关推荐
小毛驴85011 小时前
redis 如何持久化
数据库·redis·缓存
Kevinyu_17 小时前
Redisson
java·redis·缓存
代码老y18 小时前
Redis 生产实战 7×24:容量规划、性能调优、故障演练与成本治理 40 条军规
数据库·redis·缓存
dexianshen21 小时前
缓存雪崩、缓存穿透,缓存击穿
缓存
许苑向上2 天前
分布式缓存击穿以及本地击穿解决方案
java·分布式·缓存
L_qingting3 天前
Redis 主从复制
数据库·redis·缓存
代码老y3 天前
在百亿流量面前,让“不存在”无处遁形——Redis 缓存穿透的极限攻防实录
数据库·redis·缓存
阿巴~阿巴~3 天前
深入解析:磁盘级文件与内存级(被打开)文件的本质区别与联系
linux·运维·服务器·数据库·缓存
Dajiaonew3 天前
Redis主从同步原理(全量复制、增量复制)
数据库·redis·缓存
秋恬意3 天前
redis红锁
数据库·redis·缓存