-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[HttpKernel] Refine Vary header check to skip special handling of 'Accept-Language' when it's the only entry and '_vary_by_language' is true
in CacheAttributeListener
#61368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This comment has been minimized.
This comment has been minimized.
9eec426
to
04f4315
Compare
If we want to avoid changing the priority, we can add a condition to check whether the request has the _vary_by_language attribute set to true, and the response's Vary header contains only Accept-Language. |
let's do this, so that this won't interfere with app's listeners |
8240e2d
to
8085b7e
Compare
CacheAttributeListener
to ensure Vary header is not skippedCacheAttributeListener
8085b7e
to
4a186a5
Compare
CacheAttributeListener
true
in CacheAttributeListener
4a186a5
to
85c3d45
Compare
status: needs work There is a problem, i will try to resolve that problem tomorrow |
85c3d45
to
34293e7
Compare
status: needs review The problem was resolved by sending the 'Accept-Language' header at the end |
Many thanks @santysisi. |
Thanks for the review! 🙌 Regarding I tested it locally, and it seems that when the response includes |
I found in RFC9111 section4.1 the following sentence: May I suggest to add 2 test cases. yield 'vary * (no append) — vary_by_language=true' => [
'responseVary' => ['*'],
'cacheVary' => ['X-Foo'],
'varyByLanguage' => true,
'expectedVary' => ['*'],
];
yield 'vary * (no append) — vary_by_language=false' => [
'responseVary' => ['*'],
'cacheVary' => ['X-Foo'],
'varyByLanguage' => false,
'expectedVary' => ['*'],
]; Edit: and |
Hey, thanks a lot for your suggestions! The change has been made Edit: I'm not sure if it's necessary to add the modification to the |
34293e7
to
ce29227
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. No additional comment.
Many thanks!
This should also fix: #58663 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just some nitpicking
src/Symfony/Component/HttpKernel/Tests/EventListener/CacheAttributeListenerTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/EventListener/CacheAttributeListener.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/EventListener/CacheAttributeListener.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/EventListener/CacheAttributeListener.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/Tests/EventListener/CacheAttributeListenerTest.php
Outdated
Show resolved
Hide resolved
ce29227
to
da81baf
Compare
Hi! Thanks a lot for your suggestions. I’ve made the changes you recommended. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, I'm just wondering about these.
src/Symfony/Component/HttpKernel/EventListener/CacheAttributeListener.php
Outdated
Show resolved
Hide resolved
} | ||
|
||
if (($vary = $response->getVary()) && \in_array('Accept-Language', $vary, true)) { | ||
// Ensure 'Accept-Language' is included at the end of the Vary header |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary BTW?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is necessary because when you have multiple requests with different headers listed in the Vary response header, if Accept-Language is not included in the final response, the cache will not be used for subsequent requests. Each request will be treated as unique, and the previously cached response won't apply.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my question was about the ordering: why do we need to reorder?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, my previous comment was incorrect. What I meant to say is:
The issue occurs if Accept-Language
is not included at the end of the Vary header in the response , not if Accept-Language
is missing from the response itself.
I haven't tested this with many servers yet, but I noticed this behavior using the Symfony local server (started via symfony server:start
). I'm not sure whether it's an issue with Symfony itself (I don't think so), or something related to the local web server.
I'll try to test with other servers over the weekend and will update here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you have some reproducer I could run with the Symfony local server?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I’ll give it a try as soon as possible 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in case you're up to having a look: ping @philpichet ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! I hope you're doing well 😊
I tried to reproduce the error yesterday and today, but I haven’t been able to 🤔
I’m not sure if it was something specific to my computer or environment at the time, but I'm now using the same machine and the same reproducer and everything is working fine.
I’m really not sure what happened. Sorry for the confusion 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@philpichet did some changes in #61518 on the topic, this might have fixed it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, got it! 😄 Thanks for the explanation, and big thanks to @philpichet for the fix ❤️
da81baf
to
ff32a6e
Compare
Hi Nicolas, thanks a lot for your suggestions! ❤️ |
ff32a6e
to
9537e1c
Compare
…cept-Language' when it's the only entry and '_vary_by_language' is `true` in `CacheAttributeListener`
9537e1c
to
fa2bcbe
Compare
Thank you @santysisi. |
Summary
Refine Vary header check to skip special handling of 'Accept-Language' when it's the only entry and '_vary_by_language' is true in CacheAttributeListener
Reason
Previously, the
CacheAttributeListener
was executed after theResponseListener
. When theset_locale_from_accept_language
option is enabled inframework.yaml
, theResponseListener
sets a Vary header on the response. BecauseCacheAttributeListener
checks if a Vary header already exists before modifying it, its logic was being bypassed if it ran after theResponseListener
.References