We're using Varnish as Page Cache, but we need to make a Ajax-Request on every page view to load the dynamic Stuff. In our case it's:
- global messages
- mini Cart
- form key
We reduced the amount of ajax request on a page by merging everything to a single ajax request, but my hope would be to avoid any hit on Magento for a varnish cached Page. I have some ideas to accomplish this and hope someone has already experience and give me some tips.
My Ideas for:
Rewrite the Core and save a serialized object with the messages in a cookie. With JS I'am able to read the cookie and render the messages.
quite the same like my idea for global messages. If there is a change in the quote, I save a serialized object with all needed data in a cookie and render the cart with JS. The problem I see is, that the Object could be bigger then the allowed cookie size of ~4000 bytes. A solution could be to reduce the size of displayed products in the mini cart.
The second problem I see is to have the same expire date for the custom cookie and the frontend cookie.
actually I have no idea to solve this, but maybe there is no solution,
Problems I have with ESI:
There are still Magento hits and if I get it right, the page load is slower, because the request have to wait for the ESI Block. But maybe it could be a solution for the form_keys and the global messages, because the rendering ist quite fast.
Another reason I try to avoid ESI for the mini cart is, we do a lot of stuff in the card like adding free products and do custom price calculations. It took on average 150ms to render the mini cart.
I have read more about ESI and I actually don't see the benefit. Technically its the same like a ajax request. In Magento you have to create a controller to return the HTML of the block and include it in your template. Varnish call this URL exactly the same like a user would do. For that reason you have to pass the Varnish cache for this URL and you have a Cache MISS for that URL