如何禁用通过IIS提供的单页面应用程序HTML文件的缓存?

And*_*rew 30 asp.net iis caching angularjs single-page-application

我有一个单页面应用程序(angular-js),它通过IIS提供.如何防止HTML文件的缓存?需要通过更改index.html或web.config中的内容来实现解决方案,因为无法通过管理控制台访问IIS.

我目前正在调查的一些选项是:

IIS是带有.NET framework 4的7.5版

And*_*rew 38

将以下内容添加到web.config解决方案中可以在Chrome,IE,Firefox和Safari中使用:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <location path="index.html">
    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <add name="Cache-Control" value="no-cache" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
  </location>

</configuration>
Run Code Online (Sandbox Code Playgroud)

这将确保在请求时将该Cache-Control标头设置为.no-cacheindex.html

  • 我认为只有在点击一个直接包含index.html的网址时才有效.. SPA中的所有请求都有虚拟网址,不会映射到实际位置路径.可以做些什么呢? (26认同)
  • @MichailMichailidis 对于 SPA,您可能有一条规则将您的 url 重写为“/”?如果您也添加该路径怎么办? (2认同)

ttu*_*tes 15

对于.NET Core,我使用了以下内容.

        app.UseStaticFiles(new StaticFileOptions
        {
            OnPrepareResponse = context =>
            {                   
                if (context.File.Name == "index.html" ) {
                    context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store");
                    context.Context.Response.Headers.Add("Expires", "-1");
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

感谢如何在ASP.NET核心rc2中禁用浏览器缓存?

  • 谢谢,我会再检查一次(我想我已经检查过了,但为了确定)。断点只被击中两次,这是针对我提到的文件的,尽管探测器实际上似乎获取了许多其他资源。页面“index.html”可能作为默认文档/根 URL 获取,没有指定路径或文件,这就是为什么我在“if”上放置一个断点,以查看“context.File.name”实际出现的内容设想。但是……什么也没有…… (2认同)

Raf*_*eto 14

SPA 的主要前提是从不缓存index.html

按照MDN 建议防止缓存,我们必须将具有no-store、max-age=0值的Cache-Control HTTP 标头添加到资源(在本例中为index.html文件)。

为什么不使用存储而不是使用缓存

在无存储的情况下,资源不存储在任何地方。使用no-cache,资源可以被存储,但在使用它之前应该由存储通过服务器验证。

为什么max-age=0

强制清除预先存在的有效缓存响应(无存储则不会)。

在 IIS 中,我们可以通过web.config文件管理应用程序缓存配置。这是一个完整的web.config文件(必须位于我们应用程序的根目录中),其中包括index.html文件的缓存配置以及路由配置(我已添加 SPA 路由和 HTTP 重定向到 HTTPS)作为例子):

<configuration>
  <location path="index.html">
    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <add name="Cache-Control" value="no-store, max-age=0" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
  </location>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="HTTP to HTTPS" enabled="true" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
        </rule>
        <rule name="SPA Routes" enabled="true" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)


Mar*_*rio 8

这些答案都不适合我,因为正如 @MichailMichailidis 提到的,“ SPA 中的所有请求都有虚拟 url,并且不映射到真实位置路径”,所以location path永远不会匹配index.html

我在这篇博文中找到了解决方案,该博文链接到这个答案。两者都向您展示了如何使用重写模块根据条件outboundRules更改响应标头。cache-control

这就是我的 web.config 经过此配置后的样子:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <outboundRules>
        <rule name="RewriteCacheControlForIndexHtmlFile" 
          preCondition="IsIndexHtmlFile">
          <match serverVariable="RESPONSE_Cache_Control" pattern=".*" />
          <action type="Rewrite" value="max-age=0" />
        </rule>
        <preConditions>
          <preCondition name="IsIndexHtmlFile">
            <add input="{REQUEST_FILENAME}" pattern="index.html" />
          </preCondition>
        </preConditions>
      </outboundRules>
...
Run Code Online (Sandbox Code Playgroud)


gov*_*vin 6

在提供html文件时,您可以附加随机查询字符串.这将阻止浏览器使用旧版本,即使该文件位于浏览器缓存中也是如此.

/index.html?rnd=timestamp
Run Code Online (Sandbox Code Playgroud)

另一个选项是在IIS级别添加no-cache设置.这会在响应中添加Cache-Control:no-cache,告诉浏览器不缓存文件.它从IIS 7开始工作.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <!-- Note the use of the 'location' tag to specify which 
       folder this applies to-->
  <location path="index.html">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="DisableCache" />
      </staticContent>
    </system.webServer>
  </location>
</configuration>
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案不适用于Google Chrome. (5认同)

小智 5

  <meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0">
Run Code Online (Sandbox Code Playgroud)

  • 该标头仅适用于 index.html 文件还是也适用于其中包含的所有内容?(CSS,JS,图像,...) (2认同)