I haven’t done much research on HTTP in the past, but recently I have time to read some. This article mainly discusses the browser cache and some knowledge of 304 status code, here to do a share, here is the first HTTP request flow diagram:
First, detailed discussion
1. Whether to disable caching
Caching disabled means that nothing about client requests and server responses can be stored in the cache. The complete response is downloaded for each request initiated by the client.
In the request header, cache-control: no-store and Pragma: no-cache can both disable caching,
Pragma: no-cache is compatible with HTTP 1.0, while cache-Control: no-store is provided with HTTP 1.1. Therefore, Pragma: no-cache can be applied to HTTP 1.0 and HTTP 1.1, while cache-Control: no-store can only be applied to HTTP 1.1.
2. Check whether the local copy is expired
Whether to check whether the local version is expired is controlled by the no-cache and must-revalidate values of cache-contro, where:
no-cache
: tells the browser and cache server to verify the validity of the resource copy on the source server before using the resource copy, regardless of whether the local copy is expired.must-revalidate
: tells the browser and cache server that the local copy can be used before the local copy expires. Once the local copy expires, the validity of the local copy must be verified on the source server.
3. Whether the local copy is expired
To know if a local copy is expired, we need to understand the expiration mechanism of the cache:
(1) In the expiration mechanism, the most important instruction is max-age=
, which indicates the maximum time that resources can be delayed; This is usually used with must-revalidate, as follows:
Cache-Control: max-age=60, must-revalidate
Copy the code
(2) If it does not contain the Max-age attribute, it will check whether the Expires attribute is included, and determine whether the cache is still valid by comparing the Expires value with the Date attribute in the header.
(3) If max-age and Expires are not present, look for last-Modified information in the header. If so, the lifetime of the cache is equal to the Date value in the header minus the last-modified value divided by 10.
4. If the local copy is not expired
If the local copy is not expired, the resource is read directly from the cache and a 200 status code is returned.
5. If the local copy expires
If the local copy expires, pre-validation is performed on the source server.
First, the if-none-match field in the request header is looked for, which is the value of the last ETag response header returned by the server:
If there is no if-none-match field in the request header, the if-Modified-since field is looked for in the request header. The value is the date in the Last last-Modified response header returned by the server:
If neither if-none-match nor if-modified-since exists, data is requested directly from the server.
6. Verify the validity on the source server
If the request header contains if-none-match or if-modified-since, the request will be validated to the source server. If the source server resources have not changed, 304 will be returned. If there is a change, return 200;
7. Some of the above processes can also be represented in the following figure
Second, the supplementary
Private and public caches
Cache-control also has two values: private and public, where:
The public directive indicates that the response can be cached by any middleman (such as an intermediate proxy, a CDN, and so on). If public is specified, pages that would not normally be cached by the middleman (because the default is private), such as pages with HTTP authentication information (account password) or some specific impact status code, will be cached by the middleman.
Private means that the response is dedicated to a single user, the middleman cannot cache the response, and the response can only be applied to the browser’s private cache.
Iii. Reference documents
HTTP protocol MDN SegmentFault website ‘Zhao Yong’ answer ‘Ziyun Fei’ blog
Original address Wang Yulue’s personal website