How to add no-cache header for IIS when request is localhost/site root

Viewed 998

I have a website hosted in IIS 10

The .js and .css files are versioned using this pattern ?v=a.b.c (where a.b.c is changed with a new number every time is required) in order to invalidate the cache browser.

This is how the site starts :

  • user enters www.sitename.com (or "localhost" in my case)
  • the index.html file is loaded . This files loads a main.js?v=a.b.c file that loads/configures "require.js" . Every other file from this point forward is handled by require (also management of the versioning for the html/css/js)

The problem that I am facing is the following : How to invalidate the cache browser for index.html ?

I tried to send a no-cache header for the index.html with the following web.config file in IIS that works if I write localhost/index.html in the browser URL...but it does not work if I write only localhost (in this case the old index.html from the cache is loaded)

<configuration>
 <system.web>
   <urlMappings enabled="true">
     <add url="~/" mappedUrl="~/index.html" />
   </urlMappings>
 </system.web>
 <location path="index.html">
    <system.webServer>
        <staticContent>
            <clientCache cacheControlMode="DisableCache" />
        </staticContent>
    </system.webServer>
 </location>
</configuration>

Can I receive some help ?

1 Answers

you can set the no-cache header by using iis URL rewrite rule:

<system.webServer>
<rewrite>
 
<outboundRules>

  <rule name="RewriteCache-Control" preCondition="old url with 301" stopProcessing="true">
    <match serverVariable="RESPONSE_Cache-Control" pattern="(.*)" />
    <conditions>
    </conditions>
    <action type="Rewrite" value="no-cache" />
  </rule>
  <preConditions>
    <preCondition name="old url with 301">
                        <add input="{URL}" pattern="index.html|^$" />
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
    </preCondition>&gt;
  </preConditions>
</outboundRules>

 </rewrite>
        <tracing>
            <traceFailedRequests>
                <add path="*">
                    <traceAreas>
                        <add provider="WWW Server" areas="Rewrite" verbosity="Verbose" />
                    </traceAreas>
                    <failureDefinitions statusCodes="100-900" />
                </add>
            </traceFailedRequests>
        </tracing>
</system.webServer>

another way:

<?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>

enter image description here

Related