Problem/Motivation
If you want to return different responses for the same request, depending on values of HTTP headers, resulting response cannot be cached neither in internal nor in external response-level cache.
Examples of such requests:
- Browser language negotiation (
๐
Browser language detection is not cache aware
Needs work
) should have different results depending on visitor's language preferences, sent in "Accept-Language" header.
- Site needs to display local sales office to people depending on their originating country. Many CDN providers (Cloudflare, Akamai etc.) sent this information in HTTP header.
The only existing way to return headers-dependent response is to mark it non-cacheable (use KillSwitch policy). That happens because cacheability metadata that contains "header" context (implemented in HeadersCacheContext class) is only stored in renderable arrays, but not exposed to reverse proxies (or internal page context).
Proposed resolution
Introduce new Vary page response policy, that will tell reverse proxies how to properly cache returned responses. This proposal was originally raised in
๐
Browser language detection is not cache aware
Needs work
, but moved to the new issue, as it solves a bigger problem.
@catch in
https://www.drupal.org/project/drupal/issues/2430335#comment-12842970
๐
Browser language detection is not cache aware
Needs work
argues against usage of vary header in core itself, but with all respect, I cannot agree - Vary is the standard HTTP way of informing proxies about cache contexts of the origin server, especially when the response varies depending on one or more HTTP headers. Vary header is here exactly for this purpose, so it seems natural to use it.
Remaining tasks
- Rewrite the patch from
https://www.drupal.org/project/drupal/issues/2430335#comment-12840473
๐
Browser language detection is not cache aware
Needs work
, leaving only the part that introduces new response policy (and it's test)
- Modify the patch in
๐
Browser language detection is not cache aware
Needs work
to only contain changes needed for browser language negotiation.
User interface changes
- No UI changes
API changes
- New page_cache_vary service that triggers adding Vary header
Release notes snippet
Issue priority
Major since it unblocks major bug
๐
Browser language detection is not cache aware
Needs work