一斤多少 發表於 2021-5-19 11:26:00

微信公众号开发,清除微信内置浏览器缓存方法

<p>本人用微信公众号开发时,使用的html5页面,页面使用js或css文件方法修改了,但是更新到微信公众号上面总是看不到效果,后面在网上找了原因,发现微信是使用内置浏览器,会自动帮我们缓存数据,导致更新不及时或没有效果。</p>
<p>经过网上搜索清除微信内置浏览器缓存方法,具体如下</p>
<p>1)使用链接 https://debugx5.qq.com(适用于安卓端,适合开发者测试使用)</p>
<p><img src="https://img2020.cnblogs.com/blog/775247/202105/775247-20210519112458618-500705042.png"></p>
<p>2)使用微信设置清除(我--&gt;设置--&gt;通用--&gt; 存储空间--&gt;缓存--&gt;清理) (安卓端有时可以,有时不行;苹果端清除缓存会把微信视频、图片清除,不合理)</p>
<p>3)在html5添加meta标签,取消使用缓存(原理:<code><span class="com">配合 meta禁用缓存标签,实现禁用浏览器缓存,自动刷新</span></code>)&nbsp; 经测试,微信公众号上面不起作用</p>
<div class="cnblogs_code">
<pre>&lt;meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /&gt;
&lt;meta http-equiv="Pragma" content="no-cache" /&gt;
&lt;meta http-equiv="Expires" content="0" /&gt;</pre>
</div>
<p>4)在请求页面的url或资源文件添加随机版本号(原理:让微信浏览器以为是新资源文件,每次都会更新缓存)(经测试,在微信公众号上面不去作用)</p>
<div class="cnblogs_code">
<pre>&lt;script src=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">./js/orderForm.js?v=</span><span style="color: rgba(128, 0, 0, 1)">"</span>+ Math.random() +<span style="color: rgba(128, 0, 0, 1)">""</span>&gt;&lt;/script&gt;
&lt;link rel=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">stylesheet</span><span style="color: rgba(128, 0, 0, 1)">"</span> href=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">./css/orderForm.css?v=</span><span style="color: rgba(128, 0, 0, 1)">"</span>+ Math.random()+<span style="color: rgba(128, 0, 0, 1)">""</span> /&gt;</pre>
</div>
<p>5)在请求页面Html标签添加不存在manifest文件链接(原理:根据HTML5 W3C规范,解析器下载manifest header 404/410时,缓存即失效) 已测试,无效</p>
<div class="cnblogs_code">
<pre>&lt;html manifest=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">IGNORE.manifest</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;</pre>
</div>
<p>6)更改引用的资源文件名称(原理:每次页面更新,都是一个新的js、css文件版本) (若以上方法还是不能解决,可以最后尝试,无奈之举)&nbsp;&nbsp;</p>
<div class="cnblogs_code">
<pre>&lt;script src=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">./js/orderForm_210820.js“&gt;&lt;/script&gt;</span>
&lt;link rel=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">stylesheet</span><span style="color: rgba(128, 0, 0, 1)">"</span> href=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">./css/orderForm_210820.css</span><span style="color: rgba(128, 0, 0, 1)">"</span> /&gt;</pre>
</div>
<p>总之,微信内置浏览器缓存机制好坑啊、好坑啊、好坑啊</p>
<p>参考网址:</p>
<p>https://blog.csdn.net/xialong_927/article/details/88687080</p>
<p>https://www.jianshu.com/p/3f8e1209aad2</p>

</div>
<div id="MySignature" role="contentinfo">
    平时多记记,到用时才能看看,记录你的进步,分享你的成果<br><br>
来源:https://www.cnblogs.com/xielong/p/14784357.html
頁: [1]
查看完整版本: 微信公众号开发,清除微信内置浏览器缓存方法