清除前端缓存的方式

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`

}

}

}

相关推荐
手握风云-12 小时前
Redis:不只是缓存那么简单(十五)
数据库·redis·缓存
谙忆13 小时前
图片CDN与边缘优化实战:缓存策略、图片处理型CDN与动态裁剪
java·spring·缓存
小杍随笔16 小时前
【 .npmrc 终极配置指南:统一管理 npm/pnpm 缓存与全局目录,告别磁盘混乱】
前端·缓存·npm
IvorySQL16 小时前
PG 日报|新增 VFD 缓存监控视图,精细化数据库调优
大数据·数据库·人工智能·缓存·postgresql·区块链
吴声子夜歌17 小时前
Redis 6.x——监控指标
数据库·redis·缓存
heimeiyingwang1 天前
【架构实战】缓存一致性:Cache Aside与双写问题的破解
spring·缓存·架构
青山木2 天前
Redis 高可用的最后一公里:Cluster 分片、Gossip 与故障转移全流程
数据库·redis·后端·缓存
952362 天前
Redis - 基本操作
数据库·redis·spring·缓存
记忆停留w2 天前
Celery+Redis 分布式异步任务队列工程落地业务逻辑
大数据·人工智能·redis·分布式·缓存·架构·wpf
吴声子夜歌2 天前
Reids 6.x——缓存的持久化
缓存·reids