nelmio/NelmioCorsBundle

Automatic added Vary-header blocks vary-config from SensioFrameworkExtraBundle-annotation

tjveldhuizen opened this issue · 2 comments

After #156 is merged, it's not possible to use the @Cache-annotation with a vary-property anymore. This is because NelmioCorsBundle first sets the Vary-header with value 'Origin' and SensioFrameworkExtraBundle later detects the header already exists, so it ignores the properties from the annotation.

Maybe somebody has a suggestion to fix this?

if (!\in_array('Origin', $response->getVary(), true)) {

https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/7fd1d54c1b27f094a68ae15a99b7fc815857255f/src/EventListener/HttpCacheListener.php#L135

It's probably too late for your issue, but maybe somebody else finds the following helpful.

Are you sure the problem was that it ignores the properties from the @Cache annotation because it already exists? Because it should merge existing "Vary" values into the new one.

I also had an issue with getting the @Cache annotation playing together with this bundle. However, I found the reason to be the order of execution of the listeners and not a pre-existing value. More precisely, both the FrameworkExtraBundle/HttpCacheListener as well the Nelmio/CacheableResponseVaryListener are registered with priority 0, and in my case the Nelmio listener executed before the cache headers were even added. That made it bail out very early because $response->isCacheable() was false.

I was able to solve this by ovverriding the listener service and changing its priority so it runs after the cache headers were set on the response:

    # Override the CacheableResponseVaryListener to change its priority, so it runs after the cache headers have been added.
    nelmio_cors.cacheable_response_vary_listener:
        class: 'Nelmio\CorsBundle\EventListener\CacheableResponseVaryListener'
        tags:
            - { name: 'kernel.event_listener', event: 'kernel.response', method: 'onResponse', priority: -10 }

Hopefully fixed by #179