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-cache
index.html
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)
Raf*_*eto 14
按照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)
这些答案都不适合我,因为正如 @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)
在提供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)
小智 5
<meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0">
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26903 次 |
最近记录: |