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

use X-Requested-With header in hash so that xhr and standard requests… #1341

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from

Conversation

andrewkett
Copy link
Contributor

Some Magento extensions serve different results for a single URL depending on whether the request is XHR or a standard browser request. For example the XHR may return a JSON object whereas the standard request returns an HTML page. For obvious reasons this could cause issues if either result is cached and served for the other request.

I ran into an example of this using a third party layered navigation extension. The filtered products were loaded in with ajax and injected into the page, the browser URL was then updated with the history API to reflect the new page that was loaded. If the user then refreshed the page they would be served a JSON object that was originally served by the ajax request.

This wouldn't be an issue if we were able to cache ajax requests separately. Prototype and jQuery both set X-Requested-With headers with requests made through their respective Ajax tools, if this header is set we can add it to the cache hash meaning that they will be cached differently. I have implemented this solution in my development environment and it has resolved my issue, I don't believe it will affect existing functionality so it could be worth adding it to the default VCL to avoid similar problems.

This pull requests contains fixes for varnish versions 2, 3 and 4 vcl. Unfortunately I only have a working version of v4 set up so I haven't been able to test the versions 2 and 3 vcl files.

… can serve different results and still be varnished.
# on ajax requests so we can add that to the hash so that both variations
# are served seperately
if (req.http.X-Requested-With) {
std.log("hash_data xhr request - X-Requested-With: " + req.http.X-Requested-With);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should leave this here? Maybe wrap it with a debug condition might be better,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I am not 100% understanding what you are referring too. Just the log or the whole condition?

For the log I was just kind of following the format used in the rest of the vcl. Happy to wrap it in some kind of debug condition but might need some instruction on what you need exactly.

I could also wrap the whole thing in a condition controlled by an admin setting similar to some of the other features. That way we can turn the whole thing on/off. I guess it kind of depends if the new hash will affect other installs in any way.

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

Successfully merging this pull request may close these issues.

2 participants