I am currently working to make my page optimized.I came across the cache expiry.I studied about it and how it works.I got the following code to be added in web.config file.
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="14.00:00:00" />
</staticContent>
</system.webServer>
But when i test my web page with yslow ,it still says set expiry header for css,js,image files.Infact addding this code brings no change in the yslow grade.I tried other example as well such as
`<%# OutputCache Duration="60" VaryByParam="None" VaryByCustom="browser" %>`
And it does affect the rating,rating increase by one.But still the css,image,js files are not getting the expiry date.
Do i need to change anything else then the webconfig file to add expiry header to css,js files.?
DO i need to make change in iis?
I am testing my offline localhost site by the way?
Please help!
The reason why that happens is, ASP.Net does not serve the CSS, JS, images, etc files. Those are static files and ASP.Net processing pipeline just ignores them leaving those for the IIS server to serve. If you want to achieve caching on those files, you should set Caching properties in your Web server. That would be in addition to the settings you have already done in the web.config.
Depending upon which version of IIS Server you are using, the Caching headers can be set differently.
Related
I'm well aware of how I could set it myself, but I am concerned about having it configured in two different places. To receive the bounty, please tell me where I should look to find the existing setting.
Places we've already looked for the existing setting, without success:
the web.config <StaticContent> section
IIS output caching section, at all three levels:
machine
site
application
Code (I did a global search for SetMaxAge)
Context
We recently noticed that our CSS and JS files aren't getting refreshed. When I inspect the network traffic, they are coming back from the server with a header (Cache-Control: max-age=604800) that gives them a seven-day lifespan.
We need to reduce the cache lifetime. But for the life of me I can't find where it is set.
It is not set in the web.config <StaticContent> section.
It is not set in the IIS output caching section (I looked under the machine, the site, and the application-- they are all blank).
It is not set in code-- I did a global code search for SetMaxAge and got nuthin'.
Where else can I look?
Is it possible it is being set by the gateway or load balancer in our data center?
You can do like this in web config, or you can use IIS ui to change (see link below):
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge"
cacheControlMaxAge="1.00:00:00" /> <!-- 1 day -->
</staticContent>
</system.webServer>
</configuration>
See more here: https://www.iis.net/configreference/system.webserver/staticcontent/clientcache
IIS documentation states that the default value is 1 day.
Here are some other ways what could have affect these settings:
http://www.galcho.com/blog/post/2008/02/27/IIS7-How-to-set-cache-control-for-static-content.aspx
overrideModeDefault value in applicationHost.config
earlier appcmd.exe settings
or client setting can also override your values.
According to Ultra-Fast ASP.NET: Chapter 3 - Caching:
Files that the browser retrieves from the server should be stored in
the browser’s cache as long as possible to help minimize server
round-trips.
But how does IIS know what a static content actually is and is not?
Is it just images, CSS, JS and not ASPX, ashx...?
Where can I see in IIS what is already considered to be static and what is not ?
What about the scenario where a page has been declared with <%# OutputCache header (without location)? Are the images, CSS and JS source files inside of it also being output cached with the same properties?
As a best practice, I should set one year into the future as the maximum expiration time. I should use that as the default for all static content on the site
So I did this :
But later, after pressing OK, I can't find any summary menu which shows me: to whom I already put a response header (in this case: the css folder).
Currently, in order to see that css folder has been applied with response headers - I have to go to the css folder again --> Http Response Header-->Set Common Headers --> and then I see it. It isn't written in the web.config.
But if I do it for a file (Login.aspx for example): I do see it in web.config:
<configuration>
<location path="Login.aspx">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseExpires" cacheControlMaxAge="1.00:00:00" httpExpires="Fri, 15 Feb 2013 00:00:00 GMT" />
</staticContent>
</system.webServer>
</location>
</configuration>
I understand your situation. Sometime its confusing how IIS handles a file. Its also different for IIS 6 vs IIS 7 and different for Classic App Pools and Integrated mode app pools. My experience is mostly with Integrated App Pools on IIS 7.5, so thats the environment I can comment on most accurately.
First Question
But how does IIS knows what is actually a static content and what is
not?
Is it just images , css , js and not ASPX , ashx...?
Where can I see in the IIS what is already considered to be static and
what not ?
You can inspect the list of file handlers in IIS by navigating to your website and then click 'Handler Mappings'. By default these are inherited from the .Net base web.config which is in a different location depending on your .Net framework version.
C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config
If a file being requested isn't already explicitly mapped to another handler it falls to a catch all handler (*) as the last option (System.Web.DefaultHttpHandler) which determines if it is a static file or a directory browsing request. So Static files are simply files not bound to another handler already. For example you'll see that *.aspx is already mapped to System.Web.UI.PageHandlerFactory prior to this default handler. So its going to be processed by that handler and not be considered a static file. If you removed that mapping you could technically serve *.aspx as a static file if you really wanted to (just for proof of how it works).
But you can also explicitly list a file type as a static file by adding an entry in your web.config's httpHandlers section mapping the file extensions to System.Web.StaticFileHandler in IIS. For example:
<configuration>
<system.webServer>
<handlers>
<add name="StaticHandler" verb="*" path="*.zip" type="System.Web.StaticFileHandler" preCondition="integratedMode" />
</handlers>
</system.webServer>
</configuration>
This example is using the <system.webServer> config section, so its for an App Pool running in Integrated Mode.
Second Question
What about the scenario where a page has been declared with <%#
OutputCache header(without location) . does the images,css,js src
files inside of it , are also being output cached with the same
properties?
No. Because the page is being server as a separate request (maybe even by a separate handler) it can have totally different cache headers/hints. The host page and the resources it may use are not related from a caching perspective.
In fact you may even want to have a shorter cache period for *.html and a longer cache period for *.jpg or *.png? Something to consider.
Third Question
As a best prcatice , I should set one year into the future as the
maximum expiration time.I should use that as the default for all
static content on the site
Hmm... I might not go as far as one year. How about one month? I would set a global policy like this:
<configuration>
<system.webServer>
<staticContent>
<!-- Set expire headers to 30 days for static content-->
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
</staticContent>
</system.webServer>
</configuration>
This is the same as the sample you showed above, but is not inside a <location> element, instead it is right in the root <configuration> element so it is the default policy. Again this is for an App Pool running in Integrated Mode. Sometimes you also need to turn on:
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<!-- stuff -->
</modules>
</system.webServer>
<system.webServer>
This just makes sure that static files are processed through the managed static file handler which respects the above configuration elements.
Edit to Address Comments
The documentation for the configuration dialog you posted above is located here: Configure the HTTP Expires Response Header (IIS 7)
Apparently these settings are saved in C:\Windows\System32\inetsrv\config\applicationHost.config
I do not have IIS7 and personally develop on IIS 7.5 now. So please post a comment if you can verify this location is accurate!
The static content is the one that IIS is read and send to the browser with out any processing. There you can setup IIS to include some Cache-Control Header to cache it on clients browser computers.
You can do that ether by direct setup IIS, ether by commands on web.config as you say. The commands that you add on web.config and concern the IIS, did not have to do with asp.net it self, but the IIS, and IIS saves his configuration on a different file, so when you change that cache control headers direct on IIS you do not see them on web.config.
Now for the static content like images, CSS, JavaScript, and other similar files they say that you can follow the "never expire" policy by adding 10 years expire.
The issue here is that if you can not change the content of the static file, if for example you cache a javascript file with 10 years, and you make a small change on it, then you need ether to change the file name, ether to add some parameter at the end of it.
Now the <%# OutputCache on a control is referred to the server cache and not to the client, and what is actually do is to cache the render of the control on the server so the next time you ask it to not lose time to renders it again but read it from cache - is still send it to the browser.
And you can also read this answer for some more: What are difference between IIS (Dynamic and Static) cache,OutPutCache and browser cache
In IIS, can I configure it to return different cache-control headers for different static file types in the same folder?
I'm aware that I can use the HTTP Headers feature to set Expires Immediately, but that seems to affect all content. Is there a way to do it for specific file extensions for static content?
While not an ideal answer, I did find a work-around. In my application, I have control over where my JS, CSS, and other similar files live in the directory structure. What I did was put a web.config in the IIS root directory with these lines (among others, unrelated):
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="DisableCache" setEtag="true" />
</staticContent>
</system.webServer>
</configuration>
This sets the default in IIS that nothing should be cached. This is where the HTML page (index.html) for my single-page-application (SPA) lives, so that's what I want.
Then, in immediate subdirectories for images, JS, CSS, etc, I have web.config files with the following:
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="14.00:00:00" setEtag="true" />
</staticContent>
</system.webServer>
</configuration>
You can of course set the age to whatever you want. I am starting with 14 days for now until I'm sure I everything right, then I will probably bump that to several months.
This way, the HTML of my SPA will never get cached, but all the JS/CSS/images will live as long as I want. When I rebuild the project for next deployment, web-pack will generate new file names for the packed JS and CSS, so no cache problem there. The HTML file will be the same name of course, but the included JS file names will be different. So it's key that the HTML file not ever be cached, which this setup accomplishes.
IIS should really make this simpler. A cache-by-file-type config would work perfectly and not require strict directory structure management, which not all projects have the flexibility to follow.
Why does a request for a .gif image cause the session_start event to fire in my asp.net application?
In my local IIS I tried setting up a module mapping for *.gif pointing to StaticFileModule thinking that would prevent IIS from routing the request through asp.net, but for some reason my session start event is still firing when just requesting this single image.
Can anyone advise what needs to be set in IIS to prevent this from happening?
Thanks
I think you may also have to disable Session State for those folders:
https://serverfault.com/questions/77852/is-it-possible-to-set-a-folder-as-cookieless-in-iis7
Do you have a modules section within your web.config which has runAllManagedModulesForAllRequests set to true? This causes all requests (including .gif, .jpg, etc) to go through asp.net, not just managed ones.
Anything like this:
<modules runAllManagedModulesForAllRequests="true">
...
</modules>
Or alternatively, have you defined any custom <httpHandlers> within your web.config?
It sounds like your CRM might be causing the asp.net runtime to handle the request (is it a seperate .NET app? and if so, what about it's web.config?)
The Stylesheet in the App_Theme folder gets cached in the browser. What should be the approach? so that whenever there is a new deployment the browser should take the latest stylesheets and not the one cached in the browser.
This was happening for other css(which are not in theme folder) too, so used custom control as mentioned in the link
http://blog.sallarp.com/asp-net-automatic-css-javascript-versioning/
How this could be done for the CSS in the Theme folder?
Edit: The theme name is mentioned in the web.config as mentioned below. so its not just the html link tag which I had solved by using the method mentioned in the link.
<pages styleSheetTheme="Default">
<controls>
</controls>
</pages>
I too have come across this and the solution I came up with is to add a version to your CSS filename, not pretty but without disabling cache on IIS I could think of no other way.
Rename the CSS file to say
mycss-V1.0.css, which will force your
user's web browsers to reload the CSS
When deploying the web application, include the version number in the themes path. For example, App_Themes/Default/v1.2.0.4321/, where v1.2.0.4321 is the folder added at deployment for version 1.2.0.4321. This preserves both the theme name (e.g., "Default") and the file names, which makes source code control and path references much easier. ASP.NET loads all of the CSS files in the current theme folder regardless of subfolders. This not only solves the problem referencing CSS files, but images that are referenced from within the CSS files (e.g., background-image).
Additionally, the browser cache duration for App_Themes may be increased to improve performance while ensuring that the next time the web application is updated, all the theme files will be updated.
Add this to the <configuration> section of the Web.Config to have the browsers cache for 120 days.
<location path="App_Themes">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="120.00:00:00" />
</staticContent>
</system.webServer>
</location>
You could timestamp the css file name or use htaccess to setup caching limits as discussed here http://css-tricks.com/can-we-prevent-css-caching/
may be send the time stamp as a get parameter as well.
EG:
http://mysite.com/theme/dir/style.css?id=24033957203712
where 24033957203712 is the time stamp.
The browser cache is based on the expiration time set in the response header or browser setting.
There are timing when we are deploying CSS and wish to push them to the user immediately, but are unable to version the css file as referenced from asp file (such as style.css?v2).
In these cases, we can add the changed/new style classes in the particular CSS file alone to the Head section of the aspx file. It will not create any style overrides and fix the issue.