首页 > 解决方案 > jQuery Mobile 应用程序强制浏览器忽略缓存页面

问题描述

我编写了一个 jQuery Mobile 应用程序,超过 200 人每天都在大量使用它,但我遇到了以下问题。每次我更改应用程序中的某些内容(HTML、CCS 或 js)时,用户仍在使用旧版本。就像他们的移动浏览器没有检测到文件已更改......他们必须清除浏览器缓存才能获得最新版本。有没有办法让我强制浏览器忽略缓存版本并重新下载当前版本???

标签: jqueryjquery-mobile

解决方案


在 IIS (7+) 下:

  • 快捷方式:

在 IIS 管理器中,选择 Your Website,双击HTTP Response Headers,然后选择 Action Set Common Headers

这将打开对话窗口Set Common HTTP Response Headers

如果您选中Expire Web content并选择Immediately,这会将Cache-Control: no-cache所有文件发送到浏览器。

  • 微调:

下载并安装 IIS 的 URL 重写扩展。这将添加URL 重写功能。

添加一个新的空白规则并设置如下:

Name: ResponseNoCache (or whatever You like)
Match: Server Variable
Variable Name: RESPONSE_CACHE_CONTROL
Variable value: Matches the pattern 
Using: Wildcards 
Pattern: *

Action type: Rewrite
Value: no-cache, no-store, must-revalidate

现在,为了获得更多粒度,您可以设置先决条件

点击Precondition,选择Create New Precondition...

例如,我可以按文件扩展名过滤:

Name: NoCacheRequestFiles (or whatever You like)
Using: Regular Expressions

单击添加..按钮,将打开添加条件对话框窗口。

Condition input: {REQUEST_FILENAME} 
Check if input string: Matches the pattern
Pattern: \.html|\.js|\.css

之后,您需要单击应用操作并重新启动IIS。

然后web.config看起来像这样:

<rewrite>
  <outboundRules>
    <rule name="ResponseNoCache" preCondition="NoCacheRequestFiles" patternSyntax="Wildcard">
      <match serverVariable="RESPONSE_CACHE_CONTROL" pattern="*" />
      <action type="Rewrite" value="no-cache, no-store, must-revalidate" />
    </rule>
    <preConditions>
      <preCondition name="NoCacheRequestFiles">
        <add input="{REQUEST_FILENAME}" pattern="\.html|\.js|\.css" />
      </preCondition>
    </preConditions>
  </outboundRules>
</rewrite>

现在,您可以在 Chrome 开发人员工具(或任何您喜欢的工具)中检查 IIS 是否有效地发送了该Cache-Control: no-cache, no-store, must-revalidate响应标头。


附加信息:

我也在使用静态和动态压缩。因此,我注意到 IIS 正在缓存 g 压缩文件,具体取决于两个附加参数:

  • 频繁命中阈值
  • 频繁的HitTimePeriod

我没有进一步研究压缩内容和缓存控制标头之间的交互,因为整体是一个相当复杂的主题,需要经常更改和修补程序。但是,如果此解决方案也适用于移动设备浏览器,那么很高兴听到您的反馈。


推荐阅读