微信小程序清除web-view缓存方案记录
1、h5页面添加请求头,(效果未知)代码如下:
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
2、web-view页面添加时间戳,(无效),代码如下:
<web-view src="https://baidu.com?timer={{timer}}" ></web-view>
this.setData({
timer: new Date().getTime()
})
3、微信小程序在js中添加,(亲测有效),代码如下:
onShow() {
this.onLoad()
},
4、h5页面index.html中添加代码,(效果未知),代码如下:
<script>
(function(){
var href = origin = location.origin, pathname = location.pathname, search = location.search, hash = location.hash;
var searchs = search.replace(/_t_=[^&\?]*[&\?]*/g, '').replace(/(^[&\?]*)|([&\?]*$)/g, '');
if(localStorage.getItem('__clearcache__')) {
localStorage.removeItem('__clearcache__');
return history.replaceState({}, null, origin + pathname + (searchs ? '?' : '') + searchs + hash);
}
localStorage.setItem('__clearcache__', '1');
location.replace(origin + pathname + '?' + searchs + (searchs ? '&' : '') + '_t_=' + new Date().valueOf() + hash);
})()
</script>
6、每次请求更换url地址,在微信小程序中添加请求每次更新版本号。相等于每次更新给web-view设置新的请求地址以达到清除缓存的目的。
7、在小程序内调用wx.redirectTo(OBJECT)方法。(效果未知)
欢迎大家共同探讨有效解决方案