8

How can I enable caching for static resources such as CSS and JavaScript files in a Sitecore solution? I need these resources to have the following response header present:

Cache-Control:public, max-age=14400

I have the following settings in web.config with no success.

#1 system.webServer/caching

This breaks Sitecore completely. Returns 500 response code with no error information.

<configuration>
  <system.webServer>
    <caching enabled="true" enableKernelCache="true">
      <profiles>
        <add extension=".js" policy="CacheForTimePeriod" duration="365.00:00:00"/>
      </profiles>
    </caching>
  </system.webServer>
</configuration>

#2 system.webServer/staticContent/clientCache

This makes no change to the response headers.

<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00"/>
    </staticContent>
  </system.webServer>
</configuration>

#3 location/system.webServer/staticContent/clientCache

This also makes no change to the response headers.

<configuration>
  <location path="themes">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:00:15" />
      </staticContent>
    </system.webServer>
  </location>
</configuration>
7
  • 1
    Hmm, are you sure the last one doesn't work? We have a web.config in our static content folder, containing: <staticContent> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30:00:00" /> </staticContent> and it returns the correct headers for files in this folder: Cache-Control: max-age=108000 Content-Length: 212946 Content-Type: application/x-javascript Last-Modified: Mon, 24 Apr 2017 19:00:52 GMT Accept-Ranges: bytes ETag: "0caf0172dbdd21:0" May 3, 2017 at 13:05
  • @ChrisvandeSteeg I tried what you suggested but it doesn't seem to make any difference. I created a web.config in the themes folder with the following content: <?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <staticContent> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30:00:00" /> </staticContent> </system.webServer> </configuration> May 3, 2017 at 13:44
  • Can you please confirm that your using IIS (and not IIS Express) and which version. May 4, 2017 at 11:17
  • @RichardHauer using regular IIS version 10 (running in Windows 10). May 4, 2017 at 11:39
  • What's the value of the cache-control header you are getting? In the "HTTP Response Headers" section of your site in IIS, do you have a "Cache-Control" custom header? BTW forget about that first snippet, the duration is too long and anyway it appends a no-cache directive.
    – maz
    May 4, 2017 at 13:03

1 Answer 1

8

Option #3 actually does work. I forgot to do a hard refresh in my browser during testing. Oops.

So you can add this to your main web.config

<configuration>
  <location path="themes">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
      </staticContent>
    </system.webServer>
  </location>
</configuration>

Or if you prefer, you can create a new web.config in the themes folder with the following content which achieves the same thing:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
    </staticContent>
  </system.webServer>
</configuration>

When testing caching settings, don't forget to do a hard refresh!

4
  • 2
    You should go back and retry #2 as well. If #3 works, chances are #2 will also. May 7, 2017 at 3:48
  • How-to hard refresh ?
    – Kiquenet
    Feb 1, 2018 at 13:35
  • @Kiquenet [Ctrl] + [Shift] + [R] in chrome Feb 2, 2018 at 0:13
  • 1
    If a hard refresh is necessary to verify that this caching fix works, then by definition, it doesn't work. Feb 2, 2021 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.