How to disable caching of single page application HTML file served through IIS?
<meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0">
Adding the following into web.config
solution worked across Chrome, IE, Firefox, and 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>
This will ensure that the that Cache-Control
header is set to no-cache
when requesting index.html
.
For .NET Core, I used the following.
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");
}
}
});
Credit to How to disable browser cache in ASP.NET core rc2?
When serving your html files, you can append a random query string. This will prevent the browser from using the old versions even if the file is in the browser cache.
/index.html?rnd=timestamp
The other option is to add the no-cache setting at IIS level. This adds Cache-Control: no-cache in the response which tells browsers to not cache the file. It works from IIS 7 onwards.
<?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>