清除前端缓存的方式

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`

}

}

}

相关推荐
zizisuo4 小时前
6.1.多级缓存架构
缓存·架构
JavaGuide7 小时前
腾讯Java后端一面,被速通了!
网络·http·缓存·程序员·idea·多线程·校招·java基础·并发编程·aio·计算机基础·认证授权
南隅。7 小时前
【Redis】基础2:作为缓存
redis·缓存
TE-茶叶蛋8 小时前
Redis 原子操作
数据库·redis·缓存
我科绝伦(Huanhuan Zhou)11 小时前
Nginx性能优化:从配置到缓存,全面提升Web服务器性能
nginx·缓存·性能优化
星星点点洲17 小时前
【缓存与数据库结合最终方案】伪从技术
数据库·缓存
Ivan陈哈哈1 天前
Redis是单线程的,如何提高多核CPU的利用率?
数据库·redis·缓存
头顶秃成一缕光1 天前
Redis的主从模式和哨兵模式
数据库·redis·缓存
观无1 天前
Redis安装及入门应用
数据库·redis·缓存
XY.散人1 天前
初识Redis · 哨兵机制
数据库·redis·缓存