Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web.Config - StaticContent ClientCache setup for specific files

I want to be able to apply caching to Static files on my site.

I want the caching to apply to specific file extensions only but I'm not 100% certain of the syntax to add to my web.config file.

This is what I have so far:

<staticContent>
  <remove fileExtension=".svg" />
  <remove fileExtension=".jpg" />
  <remove fileExtension=".png" />
  <remove fileExtension=".gif" />
  <remove fileExtension=".css" />
  <remove fileExtension=".js" />
  <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
  <mimeMap fileExtension=".svg" mimeType="image/svg+xml"/>
  <mimeMap fileExtension=".jpg" mimeType="image/jpg"/>
  <mimeMap fileExtension=".png" mimeType="image/png"/>
  <mimeMap fileExtension=".gif" mimeType="image/gif"/>
  <mimeMap fileExtension=".css" mimeType="text/css"/>
  <mimeMap fileExtension=".js" mimeType="text/javascript"/>
</staticContent>

Am I right in thinking this will apply 1 day cache to the static files with the following extensions?

  • .svg
  • .jpg
  • .png
  • .gif
  • .css
  • .js

It looks like the clientCache node in the config doesn't directly tie to the mimeMap statements. I don't necessarily want the clientCache to work for files outside of the specified list.

Also, are there any 'gotchas' to this method I should be wary of?

Thanks for any help.

Site details:

  • ASP.NET MVC 3
  • IIS7
like image 795
scgough Avatar asked Oct 27 '25 21:10

scgough


1 Answers

You could apply a client cache-control setting by using the below code:

    <?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <staticContent>
            <mimeMap fileExtension=".text" mimeType="text/plain" />
            <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
        </staticContent>
        <rewrite>
            <outboundRules>
                <rule name="RewriteCacheControlForHTMLFiles" preCondition="FileEndsWithHtml">
                    <match serverVariable="RESPONSE_Cache_Control" pattern=".*" />
                    <action type="Rewrite" value="max-age=86400" />
                </rule>
                <preConditions>
                    <preCondition name="FileEndsWithHtml">
                        <add input="{REQUEST_FILENAME}" pattern="\.html$" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>

Note:use your file extentions.

You could also use location tag to do this but for that, you need to move that specific file extension files to another folder any apply this setting to that folder.

  <configuration>
  <!-- Note the use of the 'location' tag to specify which 
       folder this applies to-->
  <location path="images">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:00:15" />
      </staticContent>
    </system.webServer>
  </location>
</configuration>
like image 79
Jalpa Panchal Avatar answered Oct 29 '25 11:10

Jalpa Panchal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!