Skip to content

Application JSON Requests Do Not Honor X-Magento-Vary CookieΒ #730

@kavingas

Description

@kavingas

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

  1. Make an application/json request to the Magento API endpoint via Fastly.
  2. Ensure the request includes cookies that should vary the response.
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions