3

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
scgough
  • 5,099
  • 3
  • 30
  • 48

1 Answers1

2

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>
Jalpa Panchal
  • 8,251
  • 1
  • 11
  • 26