Skip to content
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

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

Open
kavingas opened this issue Jan 7, 2025 · 7 comments
Open

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

kavingas opened this issue Jan 7, 2025 · 7 comments

Comments

@kavingas
Copy link

kavingas commented Jan 7, 2025

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.

@kavingas
Copy link
Author

kavingas commented Jan 7, 2025

PR #731

@rcaril
Copy link
Contributor

rcaril commented Jan 8, 2025

Hi @kavingas ,

Thanks for providing this very detailed issue. Upon review, we aren't sure in which scenarios that API calls from the plugin would lead to a non-cached response. Can you provide some use cases / examples that are being solved for here?

@kavingas
Copy link
Author

Yes it will impact any AJAX call with json response type.
For an exampole
curl 'https://base_url/search/ajax/suggest/?q=red' \ -H 'Accept: application/json, text/javascript, */*; q=0.01' \ -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36' \ -H 'X-Requested-With: XMLHttpRequest'

@rcaril
Copy link
Contributor

rcaril commented Jan 16, 2025

Yes it will impact any AJAX call with json response type. For an exampole curl 'https://base_url/search/ajax/suggest/?q=red' \ -H 'Accept: application/json, text/javascript, */*; q=0.01' \ -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36' \ -H 'X-Requested-With: XMLHttpRequest'

Thanks for this context, we'll review further.

@cchawla8
Copy link

Hey @rcaril , I am following up to check the status on this one as customer is awaiting resolution, and it would help if we have an ETA.

Thank You!

@rcaril
Copy link
Contributor

rcaril commented Feb 3, 2025

Hi @kavingas - we tested the API call above on few of the shops that we maintain - one of them uses Fastly, others use Varnish. We are confident that none of them cache this API ( on the store with Fastly we can even see MISS, MISS in headers, response time isn't decreasing after repeated request) so we are not exactly sure how Vary helps them in this scenario. We don't believe that the results for their website will be any different after with this PR change.

@kavingas
Copy link
Author

kavingas commented Feb 4, 2025

@rcaril Did you try with multiple store views with multiple currency setup ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants