Skip to content

CacheAttributeListener overrides custom Vary headers when set_locale_from_accept_language is enabled #61229

@lulu1121

Description

@lulu1121

Symfony version(s) affected

>=7.4

Description

The Vary header(s), defined with the @Cache attributes are ignored when used in conjunction with the feature set_locale_from_accept_language.
However, the documentation doesn't mention any impact on Vary response header enabling this feature:

If this option is set to true, the Request locale will automatically be set to the value of the Accept-Language HTTP header.

When the _locale request attribute is passed, the Accept-Language header is ignored.

The possible cause is at this line, looks like the Symfony\Component\HttpKernel\EventListener\CacheAttributeListener listener overwrites the Vary header(s) passed by @Cache attributes, instead of adding to it — which may break expected behavior when custom Vary headers are required.

How to reproduce

./src/Controller/TestController.php

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\Cache;

class TestController
{
    #[Cache(vary: ['X-Custom-Header'], public: true)]
    public function __invoke(): Response
    {
        return new Response('Hello world');
    }
}

./config/packages/framework.yaml

framework:
    set_locale_from_accept_language: true
curl --location 'http://localhost:8000/test'

Vary response header: Accept-Language

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy