When the content does not change, the server only returns a status code, and conditional requests rely on which identifier to judge.
When the content does not change, the server can only return a status code, and the identification for conditional requests is determined by which indicator, the answer is the version identifier carried by the resource itself. When the browser first receives the resource, the response header will include an ETag, which is the response header that identifies a certain version of the resource;When the local cache expires, the browser does not hurry to redownload the entire content, but initiates a conditional request, using the If-None-Match request header to send back the old value obtained last time.The server compares the returned value with the current version, if they match, it returns 304, the response has no text, just tells the client: the version you have is still valid.
Is cache expiration the same as redownloading the entire content?
The working rhythm of caching is divided into two segments. During the fresh period, resources are directly read from the local copy, and there is no need to send a request; after expiration, resources are not immediately discarded, but first ask the server a question: Is this version still valid? This question is a conditional request. Without version identification, expiration can only be handled by redownloading all the content, even if not a single character has changed, it is obviously a waste to repeatedly transport the entire text or image; with identification, the judgment is degraded to comparing two strings, and when they match, there is no need to send the text even.
What does this identification represent?
The rule is only one: if the content of the resource at the same address changes, a new identifier must be generated; if the content does not change, the value remains unchanged. It does not describe what the content looks like, nor does it encode how long the content is, but it answers a question: Is this version the same as the last one you had.
It is a separate check from the way it is judged according to the last modification time. The time method compares when the file was last modified, and the version method compares whether it is the same version of content. The time granularity is coarse, and multiple changes within the same second may not be distinguishable; when the file is saved again but the content does not change, the update of the timestamp may cause misjudgment. The version identifier is more direct in judging content changes, and both can coexist. The client takes which condition header, and the server judges according to the corresponding logic.
How to compare during the follow-up visit
This is how the process is put together: The server provides an identifier value in the response, and the client stores the content along with it; after the cache expires, the client sends a new request and attaches the old value in If-None-Match; the server calculates the current version identifier and compares it. If consistent, it returns 304, and the client continues to use the local copy; if not consistent, it returns 200 and the complete new content, along with the new value, and the client replaces the old cache.
The key to generating an identifier is to distinguish between actually changing the content and just re-rendering it. If there is a fluctuating component in the input that changes with each request, such as the rendering time or random numbers, a new value will be calculated each time the response is given, and comparison will always fail, resulting in the verification degenerating into a full download each time, which is equivalent to no caching. Websites that mainly render content from databases are more likely to encounter this issue: the main text exists in the library, the accompanying resources are stable, and the identifier should only depend on the true input that represents the content. The computing input of each node in the cluster must also be consistent, otherwise the same version of the content will calculate different values, and the verification will mysteriously fail.
What is the difference between strong validation and weak validation
If a prefix W/ appears before the identifier value, it indicates that it is a weak validation identifier; if there is no prefix, it is a strong validation identifier. Weak identifiers are easy to generate, but are not suitable for byte-by-byte comparison: if both sides are equal, it can only mean that the content is roughly equivalent in terms of semantics, and cannot guarantee that each byte is the same.
The more practical impact is on segmented requests. Browsers sometimes use Range headers to request only a segment of a file: weak identifiers can cause such range requests to fail to hit the cache, while range requests under strong identifiers can still be cached. Because range requests require the segment obtained to strictly correspond to the entire file, since weak identifiers do not promise byte-by-byte consistency, it is uncertain whether this segment of content is still the original one. For scenarios that depend on segmented fetching of resources, strong identifiers are more suitable.
How to use sites with frequent content changes
An article revised once becomes one edition, the identifier follows the change, and the revisitor will naturally receive new content. What needs to be considered is the changeable part: if the page contains real-time statistics, personalized greetings, and other content that changes every time, the entire page identifier will change every time, and all revisit verifications will fail.Commonly, the approach is to split the variable part out into a separate request, allowing the main content to maintain a stable version, thus making the identifier constant. For static resources, the identifier is generated as usual, with a longer freshness period, and the validation after expiration usually hits 304.In addition, pay attention to the fact that a major version upgrade of the server or framework may change the calculation method of identifiers, causing all old values to not match overnight, and it is advisable to arrange for the release impact within the expected range.
Common questions
Question: Should this identification be used at the same time as judging by modification time?Answer: The two sets are independent verification methods that can coexist. If there is an old value with an identifier, compare by version; if there is a time condition, compare by time; both complement each other rather than replace each other.
Question: If a 304 without content is received, does it mean the request failed?Answer: No. 304 is a normal result of successful verification, meaning that the version in the cache is still usable, and the client will use the local copy immediately. No content means it is exactly to save the repeated transmission.
Question: Why do we never see responses without content on our site?Answer: It is usually a tag that changes every time. Check if there are fluctuations like rendering time mixed into the input, and whether the calculation methods of each node in the cluster are consistent. A revisit will only be successful if the input is stable and the content does not change.
Question: If the content is changed but the client does not receive the new version for a long time, is it related to this mechanism?Answer: It is possible. If the content changes but a new identifier value is not generated, the revisit comparison will still be consistent, and the client will continue to use the old cache. It is necessary to change the value when the content of the resource at the same address changes, and this step cannot be omitted.