-
Notifications
You must be signed in to change notification settings - Fork 123
Description
Title
Application JSON Requests Do Not Honor Vary
Cookie
Description
While reviewing the behavior of the Fastly VCL in the Magento 2 extension, I noticed that application JSON requests are not honoring the Vary
cookie.
Currently, the logic in [fetch.vcl](https://github.com/fastly/fastly-magento2/blob/master/etc/vcl_snippets/fetch.vcl#L67)
only adds Vary
cookies for text/html
and text/xml
content types. As a result, JSON responses are not varying by cookie values, which can lead to serving cached data that does not match the user's session or preferences.
Steps to Reproduce
- Make an
application/json
request to the Magento API endpoint via Fastly. - Ensure the request includes cookies that should vary the response.
- Observe that the
Vary
header is not set in the response.
Expected Behavior
The Vary
cookie should be applied to application/json
content types, ensuring correct cache variation based on user session or other cookies.
Actual Behavior
The Vary
cookie is omitted for application/json
responses.
Relevant Code Snippet
The logic in [fetch.vcl](https://github.com/fastly/fastly-magento2/blob/master/etc/vcl_snippets/fetch.vcl#L67)
:
if (beresp.http.Content-Type ~ "text/(html|xml)") {
set beresp.http.Vary = "X-Magento-Vary, X-Store-Cookie, Https";
}
This restricts Vary
cookies to only HTML and XML content types.
Proposed Solution
Update the fetch.vcl
logic to include application/json
in the content types that apply the Vary
cookie:
if (beresp.http.Content-Type ~ "text/(html|xml)" || beresp.http.Content-Type ~ "application/json") {
set beresp.http.Vary = "X-Magento-Vary, X-Store-Cookie, Https";
}
Environment
- Magento 2 Version: 2.4.7
- Fastly Module Version: 1.2.223
- Fastly Version: current
Additional Context
This behavior could cause issues when serving cached JSON content for APIs that depend on user-specific data. Adding support for the Vary
cookie in JSON responses would improve consistency and prevent user-specific cache mismatches.