We have a considerably performance-savvy view that we want cached, but it just won't work.
Stops working when cache is enabled.
To reproduce:
- Create a node type 'foo' and product type 'bar'.
- Create in node type 'foo' field of type "Commerce product reference" to type 'bar'.
- Create a view page on path '/mypath' consisting of node displays of type 'foo'
- Enable views caching 5min/5min
- Add to cart first time
- Next try to "Add to cart" another product related of same node (another product variation in Commerce Backoffice terminology).
Is it by design that the cart buttons now stops working?
Ive looked around for views issues as well which could result in this, but alas, without result.
Views 3.8
Any clues?
Thanks!
Comments
Comment #1
rszrama CreditAttribution: rszrama commentedThe issue here is that Views is then caching form HTML which will be invalid for anyone besides the initial user who generated the cache. It will even be invalid for that initial user after he or she submits the form once, because it will require a new token on subsequent submits. Unfortunately, for the Add to Cart form, you just can't cache it like that. You could cache it if you were using Add to Cart links instead of forms, but that might result in other issues web crawlers generating carts - I'm not sure if the Rules Links module can prevent that.
Comment #2
Exploratus CreditAttribution: Exploratus commentedSo we cant cache a view for a store using add to cart buttons? Seems rather limiting doesn't it?
Comment #3
farald CreditAttribution: farald commentedYes it is rather limiting, if you have front page with lots of cart buttons. We worked around the issue by serving buttons as links. Have a look at https://drupal.org/sandbox/farald/1945900
Comment #4
periksson CreditAttribution: periksson commentedThis might be outside the scope of this issue.
Is it possible to cache "everything else" in the view - entities, nodes etc to increase performance, leaving the view without the time-based cache?
We are looking to a solution to increase performance, so any suggestions, modules are greatly appreciated.
Best
Eric
Comment #5
periksson CreditAttribution: periksson commentedHi,
Would the Views Per-User Cache solve this issue?
https://www.drupal.org/project/views_puc
Best Eric
Comment #6
ofry CreditAttribution: ofry commentedNow I can suggest only cache query results, not the HTML code :(
Comment #7
ofry CreditAttribution: ofry commentedComment #8
ofry CreditAttribution: ofry commentedComment #9
bojanz CreditAttribution: bojanz commentedThis is a limitation of the Views form API, not something we control or can change at this point.
Comment #10
periksson CreditAttribution: periksson commentedMy recommendation is to unset the form token of the form. The security impact (CSRF) is low from our perspective.
Then cache the view as usual.
Comment #11
kopeboy CreditAttribution: kopeboy commentedIf I disable the form token as in #6 will any Drupal Commerce functionality be affected?
ie. is the token only related to security? In which way?
My particular concern is about multiple anonymous user carts functionality (not a developer).
Comment #12
kopeboy CreditAttribution: kopeboy commentedComment #13
gdana CreditAttribution: gdana commentedSorry for this, eventually realised my comment and question had nothing to do with the issue posted.
Comment #14
rszrama CreditAttribution: rszrama commentedI agree w/ Bojan and disagree with security implications; I'd never intentionally make form submission less secure.
Comment #15
matsjacobsson CreditAttribution: matsjacobsson commentedHi, Hopefully some kind soul can clarify this for me...
1. Is it approved to use query caching (not HTML) as ofry #6 suggested?
2. I'm also wondering if distinct or aggregation will affect this issue in any way?
Thanks!
/Mats
Comment #16
xandeadx CreditAttribution: xandeadx commentedhttps://www.drupal.org/sandbox/xandeadx/2540000
Comment #17
andyg8 CreditAttribution: andyg8 commentedThat was causing our site's Add to cart button to fail if used on a 2nd product in a view (even a non-Ajax view). We found we could still cache if we used Views, caching > time based > cache raw query results (1 hour) but NOT cache rendered output.