Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement browser cache-related http headers to minimize the number of http requests

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge about "how to realize browser cache related http header to minimize http requests". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Read Yahoo Gold 34 recently to learn how to optimize website performance. One of them: "Specify Expires or Cache-Control for file headers," specifically for static content: set the value of Expires for file header expiration to "Never expire"; and for dynamic content: use the appropriate Cache-Control file header to help browsers make conditional requests.

This can be related to the first yahoo 34, which is to minimize http requests (many yahoo 34 are trying to reduce http requests for the purpose), after all, if there are too many resources to download, the time required to stack new http links cannot be ignored. So you can use caching techniques to optimize the performance of your website to avoid unnecessary http requests. Just in time to sort out browser-related caching technology and cache-related http headers:

1. Expires:

The HTTP header Expires attribute is the basic means of HTTP cache control, telling browsers how long the cache will last. After this time, the cache sends a request to the origin server to check if the document has been modified. It is suitable for setting static image files, etc., and is also useful for controlling regularly changing web pages, such as setting fixed intervals to update, etc.; if the Expires file header is used, the file name of the content must be changed when the page content changes. Yahoo, for example, often uses the procedure of adding a version number, such as yahoo_2.0.6.js, to the file name of the content to be able to update it voluntarily.

Use expires: If you are using Apache, you can use ExpiresDefault to set the expiration time relative to the current date, use mod_expires, and add httpd.conf or. httaccess

The code is as follows:

ExpiresDefault "access plus 10 years"

2.Cache-Control

The instructions in each message have the following meanings:

1. Public indicates that the response can be cached by any cache;

2. Private indicates that all or part of the response message for a single user cannot be processed by the shared cache. This allows the server to describe only a portion of a response message when the user is responding, which response message is not valid for other users 'requests.

3.no-cache indicates that the request or response message cannot be cached;

No-store is used to prevent important information from being released unintentionally. Sending in the request message will cause neither request nor response messages to use caching;

max-age Indicates that the client can receive responses with a lifetime no longer than the specified time in seconds.

6.min-fresh Indicates that the client can receive responses with response times less than the current time plus the specified time.

7.max-stale Indicates that the client can receive response messages beyond the timeout period. If a value for max-stale messages is specified, the client may receive response messages that exceed the specified value for the timeout period;

Examples:

Generally speaking, this static file should never expire. If you really want to add a deadline to this Cache, then I hope it is--ten thousand years.

Cache-Control: max-age = 31536000000

3.Last-Modified/If-Modified-Since

A question-and-answer mode, here ask you whether you have updated, there answer, very easy to understand;

4. Configure ETag

Entity tags (ETags) are a mechanism used by web servers and browsers to determine whether content in the browser cache matches the original content in the server ("entities" are so-called "content," including images, scripts, style sheets, etc.). Adding ETags provides a more flexible mechanism for entity validation than using "last-modified date." Etag is a unique string identifying the version number of the content. The only formatting restriction is that it must be enclosed in double quotes. The origin server specifies the ETag of the page's content by responding with an ETag file header. For example:

HTTP/1.1 200 OK

Last-Modified: Tue, 12 Dec 2006 03:03:59 GMT

ETag: "10c24bc-4ab-457e1c1f"

Content-Length: 12195

Later, if the browser wants to validate a file, it uses the If-None-Match file header to pass the ETag back to the origin server. In this example, if the ETag matches, a 304 status code is returned, saving 12195 bytes of response.

GET /i/yahoo.gif HTTP/1.1

Host: us.yimg.com

If-Modified-Since: Tue, 12 Dec 2006 03:03:59 GMT

If-None-Match: "10c24bc-4ab-457e1c1f"

HTTP/1.1 304 Not Modified

Regarding the priority of the above several caching mechanisms, I found a saying on the Internet:

no-cache>Expires>Last-Modified

That is to say, the most important thing in the front is that after the front takes effect, the latter basically loses effect.

"How to achieve browser cache related http header to minimize the number of http requests" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report