Web. config client caching policy for static file js CSS img

Source: Internet
Author: User
Tags browser cache

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Configuration>3     <system.webserver>4         <staticcontent>5             <ClientcacheCachecontrolmode= "Usemaxage"Cachecontrolmaxage= "7.00:00:00" />6         </staticcontent>7         <handlersAccesspolicy= "Script,read">8             <!--for any request to a file exists on disk, return it via native HTTP module. accesspolicy= "Script" above is-to-allow for a managed 404 page.  -9             <Addname= "Staticfile"Path="*"verb="*"Modules= "StaticFileModule"Precondition= "Integratedmode"ResourceType= "File"requireaccess= "Read" />Ten         </handlers> One     </system.webserver> A </Configuration>

HTTP Status Code 304 page not modified

Features: 304 Page not modified

The requested page has not been modified since the last request. When the server returns this response, the Web page content is not returned.
If the page has not changed since the requestor last requested it, you should configure the server to return this response (known as the If-modified-since HTTP header). The server can tell Googlebot that the webpage has not changed since the last crawl, thus saving bandwidth and overhead.

When you add these items to the Response-header:

    last-modified:wed, 18  jun 2008  14 : 22 : 27  gmt
    cache-control:max-age=< Span style= "color: #ff6600;" >600
    expires:wed, 18  jun  2008  14 : 48 : 39  gmt
    date:wed, 18  jun 2008  14 : 38 : 39  gmt

The date after last-modified is the last time the config was updated, the current time after date, Expires followed by the current time + 10 minutes, and 10 minutes is the max-age behind Cache-control, in seconds.

Last-modified

If the client receives a Response that contains last-modified, the next request will include the If-modified-since field in the request Header, which is the value of the last server sent Last-modified , the server will determine if the last config time is later than If-modified-since. If the config has been updated since the last request, the server will return the full content, and if the config is not updated during the period, then the server will not need to return the full content, just send a 304 not Modified status code to the client.

Cache-control, Date, and Expires

The combination of these parameters tells the browser: How long this file will not change, in this time no need to request, for the sake of conservatism, I set the 10 minutes.

Browser behavior

If you just click the click click between links in the site, the browser will follow the above behavior completely. This minimizes the number of requests, as well as the amount of data response.

If you click on a page of the browser refresh button or press F5, the browser will ignore the Expires time, the page needs all the files requested again.

If you hold down CTRL and Refresh or CTRL-F5 (commonly known as forced refresh), the browser will not send the last-modified Header, request all required files again, the server will return the full contents of the file, instead of just a 304 not Modified status code.

HTTP Code 304 Basics

1) What is "last-modified"?

When the browser requests a URL for the first time, the server-side return status is 200, the content is the resource you requested, and there is a last-modified attribute that marks the last time the file was modified at the end of the service period, similar in format:

Last-Modified: Fri, 12 May 2006 18:53:33 GMT

When the client requests this URL for the second time, according to the HTTP protocol, the browser transmits the If-modified-since header to the server, asking if the file has been modified after that time:

If-Modified-Since: Fri, 12 May 2006 18:53:33 GMT


If the server-side resource does not change, the HTTP 304 (not Changed.) Status code is returned automatically, and the content is empty, which saves the amount of data transferred. When the server-side code changes or restarts the server, the resource is re-emitted, similar to when the first request is returned. This ensures that the resources are not duplicated to the client, and that the client is able to get the latest resources when the server changes.

2) What is an "Etag"?

The HTTP protocol specification defines the etag as the entity value of the requested variable (see-section 14.19). Another argument is that the ETag is a token that can be associated with a Web resource. A typical web resource can be a Web page, but it may also be a JSON or XML document. The server is solely responsible for judging what the token is and what it means, and transmitting it to the client in the HTTP response header, which is the format returned by the server side:

ETag: "50b1c1d4f775c61:df3"

The client's query update format is this:

If-None-Match: W/"50b1c1d4f775c61:df3"

If the etag does not change, it returns the status 304 and does not return, which is the same as last-modified. I test the etag is mainly in the breakpoint download is more useful.

How do last-modified and etags help improve performance?

Smart developers will use the HTTP header for Last-modified and ETAGS requests, which can take advantage of caching by clients such as browsers. Because the server first generates the LAST-MODIFIED/ETAG tag, the server can later use it to determine if the page has been modified. Essentially, the client requires the server to validate its (client) cache by passing the token back to the server.
The process is as follows:
The 1> client requests a page (a).
The 2> server returns page A and adds a last-modified/etag to a.
The 3> client presents the page and caches the page along with the Last-modified/etag.
4> the Customer requests page A again and passes the Last-modified/etag that the server returned when the last request was sent to the server.
The 5> server checks the last-modified or ETag and determines that the page has not been modified since the last client request, directly returning a response of 304 and an empty response body.

Browser cache

Reduce the round-trip between servers and shorten the corresponding time.
Cache static Content: in the HTTP header information, the Cache-control property represents the cache information.
Set the Cache-control property, you can set the value of Cache-control in IIS Manager, and you can configure it in the Web. config file:

<system.webserver>
<staticContent>
<clientcache cachecontrolage= "Usage" cachecontrolmaxage= "365.00:00:00"/>
</staticContent>
</system.webserver>

Later, you can also set a shorter time for a specific static resource.

Turn off the browser cache

You can turn off the browser cache for a specific static resource, either in IIS or in the Web. config file:

<location path= "Image.jpg" >
<system.webserver>
<staticContent>
<clientcache cachecontrolmode= "DisableCache"/>
</staticContent>
</system.webserver>
</location>

Caching dynamic Content

Implement the cache declaratively in the ASPX page and set the expiration time:
<% @outputcache duration= "86400" location= "Client" varybyparam= "None"%>
This statement generates an HTTP header in the runtime, allows the browser to cache for 86,400 seconds (1 days), and must also set the varybyparam= "None" attribute, which indicates that multiple versions of the page do not require a separate cache.
You can also set it programmatically, either in the post code or in the HttpModule:

void Setcache ()
{
This. Response.Cache.SetExpires (DateTime.Now.AddDays (1));//Can be ignored
Timespane ds = new Timespane (1,0,0,0);
This. Response.Cache.SetMaxage (DS);//Key code
}

Using Cache Configuration
Configure a cache in the Web. config file, which can then be used in ASPX outputcache:

<system.web>
<caching>
<outputcachesetting>
<outputcacheprofiles>
<add name= "Cacheday" duration= "86400" location= "Client" varybyparam= "None"/>
</outputcacheprofiles>
</outputcachesetting>
</caching>
</system.web>

Web. config client caching policy for static file js CSS img

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.