IIS 8.0 add both Expires header and Cache-Control
Asked Answered
B

1

3

i can see stackoverflow add's both Expires and Cache-Control for their images , css, js etc, so i am trying to do the same, i tried this article

web.config

<location path="Content">
    <system.webServer>
      <staticContent>

    <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" 
    cacheControlMaxAge="365.00:00:00" />

    <clientCache cacheControlMode="UseExpires" httpExpires="Mon, 01 May 2023 00:00:00 GMT" />

      </staticContent>
    </system.webServer>
</location>

when i try to acess a css file in content folder : http://localhost:11111/Content/bootstrap.min.css

i get the following error

Error:

HTTP Error 500.19 -

Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid.

Module           CustomErrorModule
Notification     SendResponse
Handler          StaticFile
Error Code       0x8007000d

Config Error Configuration element 'clientCache' already defined

i know clientCache already defined, but i am concerned and want to know how to set both Cache-Control and Expires in response headers ?

Any help would be great.

Update:

As @Vitaly Kulikov answered and discussed in comment and using this and this posts i came to conclusion that in IIS we can's set both Expires and Cache-Control in web.config, so i have planned to use OutputCache to set Expires for images at least and it works with 0.9 milliseconds delay. Hope helps someone.

Buckling answered 13/3, 2016 at 11:10 Comment(0)
M
4

According to the specification there is no way you can have both of them at the same time. But you have no reason to do that, you'll be fine with any of them.

Specification

If you look into Chrome source code, caching in browser has several simple rules

First:

The max-age directive takes priority over Expires, so if max-age is present in a response, the calculation is simply: freshness_lifetime = max_age_value

Second:

Otherwise, if Expires is present in the response, the calculation is: freshness_lifetime = expires_value - date_value

Third:

If we missed previous steps and "cache-control" != "must-revalidate", then browser has one more caching rule: It caches resource for 10% of a time passed since last modified date of a resource.

That's it, no more rules there.

Malynda answered 16/3, 2016 at 6:18 Comment(6)
thanks for answering, all famous site's set both of them and i can see stack overflow set both "Expires" and "max-age" for their images and css etc , like: Cache-Control:public, max-age=60 Expires:Sun, 13 Mar 2016 08:05:18 GMT how do they do that ? do they do using web.config or they configure in their CDN ?Buckling
Most "famous sites" do not use IIS. But Tomcat, Apache Web Server or Nginx can be configured to return both header simultaneouslyMalynda
I've edited my answer to clarify how browser caching worksMalynda
you mean IIS doesnt provide that option, ok if i set cacheControlMode="UseMaxAge" i am missing the expire header in response, and google speed test says add expires for images, css, js. what can be done ?Buckling
Page Speed Test is written by a human, it also has bugs in its logic. Who do you believe more, chrome source code, or some web page?Malynda
i test the site using google speed or Yslow as adviced by developers like you , i have the same issue this folk faced, anyhow i am gonna leave the expire issue for now for js and css , i have planned to use OutputCache to set Expires for images atleast and it works, thanks for the answer, can i use Nginx and IIS with asp mvc, if yes, then any refrence ?Buckling

© 2022 - 2024 — McMap. All rights reserved.