Why would a translation be sent to someone in another language if the site negotiated the language in the request header and cached it?
After the site with language negotiation in the request header adds caching, the translation is sent to visitors in another language, and the cause is almost always the same: the cache key only contains the address and method, without the language as an influencing factor.MDN defines Vary as exactly this: The response header describes that part of the request information, other than the method and URL, that affects the response content;The Vary header ensures that responses are cached based on the listed header fields, the most common use is to construct cache keys during content negotiation. Without this declaration, the intermediate layer will treat the translation received by the first visitor as the standard answer for the address.
The default cache key only considers the address and method
The basis for determining whether the cache hit is the cache key. By default, it consists of the request method and target address, plus some fragments of the path at most.
Content negotiation destroyed this premise. The same address, because the value of Accept-Language in the request header is different, the server generates completely different text. From the perspective of caching, it is two requests with the same key, so the second one directly uses the response of the first one.
The characteristics of this kind of problem are very stable: the local direct connection to the source station is normal, and problems occur after connecting to the cache or acceleration node; after clearing the cache once, it is normal for a short time, and then it starts to occur again; people with different browser settings see the same language. If any two of these three occur, it should be suspected of the cache key first, rather than the translation logic.
What is missing when negotiating language
What is missing is not the translation ability, but 'putting influencing factors into the response'.
When the server decides which language to return, it reads the request header; but the response itself does not tell the downstream 'my content depends on this header'. This is a one-way information loss: the program knows, but the cache does not know.
Vary is the mechanism to fill in this information. After writing it as a header containing language-related information, the cache will store copies according to the different values of this field, and the hit rate will decrease, but it will not send the wrong one.
There is a more thorough way to take a value: When Vary takes an asterisk, it means that there are other factors affecting the generation of the response, which is equivalent to the response being uncachable. This approach is honest but costly, and is generally only used when it is indeed impossible to determine the influencing factors.
It is especially important to pay attention to the consistency of the value. All responses of the same URL should use the same Vary value, including unmodified responses and default or error responses. Writing it only on successful pages and not on error pages will cause two declared objects to exist in the cache, making the behavior more difficult to predict than not writing it.
How should it be supplemented in the response header?
Write in three steps when landing.
The first step, for any address generating the text based on the language header, a language-related statement should be added. If negotiation of compression method is also done, both should be listed together, because the reason for hitting the wrong version separately is the same.
The second step, fix the negotiation rules. The language matching should have a clear order, such as precise language first, then language family next, and finally default language, rather than 'which header comes first'. When the rules are unstable, even if the cache is correct, the same user may get different translations twice when visiting twice.
The third step is to provide an explicit entry for optional languages. Relying on the request header for judgment, users actually cannot actively choose; providing a clickable language switch and allowing the selection result to fall on an addressable URL is easier to be cached and correctly processed by the crawling party.
Two ways to arrange multilingual websites
The site supports multilingual site configuration and switching, and supports full-page HTML translation. In terms of implementation, there are usually two routes, and their relationship with the cache is different.
One is based on header negotiation, where the same address returns different language versions. Its advantages are short addresses and no need to expand the structure; however, the cost is the cache issues discussed in this article, as well as the ambiguity caused by the same address corresponding to multiple contents for the crawling party.
The other is to divide addresses by language, for example, using subdirectories or subdomains to separate different languages. Its address corresponds to the content one by one, and the cache almost never makes a mistake; however, the cost is that the site structure becomes larger, and the maintenance amount of the translation content increases accordingly.
The ability to generate translations for full pages by the system also introduces a third scenario: the translation may be at the same address as the original page or at a separate address, depending on the implementation. When deploying, it is important to confirm which route the translated product takes - if the translation is written as a static file and placed at the same address first, the priority of caching and the file layer will override the negotiation logic, making it easy to misjudge the direction during troubleshooting.
There is a significant difference between self-built websites and hosting environments on this point: when deploying on your own, you can control the negotiation rules and cache layer configuration at the same time; in the hosting scenario, it is often only possible to change the site side and cannot affect the cache key strategy of the middle layer, so it is more inclined to adopt the separate addressing scheme at this time.
Verification sequence
Do this in order and you can locate the problem within a few minutes.
First, directly connect to the source station and request the same address in two different languages separately, confirm that the responses are indeed different, and both responses carry statements of language influence. Then, repeat the same two requests through the cache layer to confirm that each one hits its own copy rather than the same one. Finally, check the unmodified response and the error response to ensure that their statements are consistent with the normal response.
If the second step appears with a string version, the handling methods are arranged in order of priority: supplement declaration, change the address to non-cachable, or migrate to a separate addressing scheme. The impact on hit rate increases in turn, but the certainty also increases in turn.
Common questions
What should be done if the hit rate decreases after adding the declaration?This is the inevitable cost of the correct behavior. You can place resources unrelated to language (styles, images) separately, so that they can continue to enjoy a high hit rate.
What language does the search engine capture?The request header of the crawling party usually tends to be the default language. If the site is aimed at a multi-language market, separate addressing is more controllable.
Can the language preference in Cookie replace the request header?But it can participate in the judgment, but it will make the cache problem more serious: the response differences brought by personalized credentials also need to be handled at the cache and declaration level.
Why does it sometimes work after a refresh?Intermittent performance may occur when the cache object expires or is replaced, which cannot be taken as evidence that the problem has been resolved, and should still be reviewed in order of verification.
Does the content generated by full-page translation need to be handled separately?It needs to be confirmed whether it is on the same address as the original page. If it is on the same address, it should take the negotiation declaration route; if it is on a separate address, it should verify the new address cache and site map coverage.