7

This week, I noticed an interesting tweet (linked by Ben Marks in the comment below) that was saying something like this:

Please make your Magento 2 modules compatible with Varnish as it is supported out of the box by Magento 2 CE

AFAIK, one thing to do is the implementation of the getIdentities method in the block classes (see here: What is IdentityInterface in Magento2)

Apart from that, is there anything else that needs to be done to make a Magento 2 module Varnish compatible ?

2

1 Answer 1

11

It depends on what your module is rendering. It boils down to do not render private, customer specific data server side.
The exception being on completely un-cached pages (for example the customer account dashboad).

In summary, I think it boils down to approaching module creation with a client-side first mindset. It is best to assume that GET and HEAD requests will never reach Magento, and as such relying on plugins or observers or other server side tools only for customizations is not a good approach.

Some details I'd look for to check if a module will work well with Varnish:

If the block isPrivateScope() method returns true, then it can render private data, but only during the Ajax request and not during the regular page rendering.
Otherwise the private data will end up in the full page cache.

The right way to render private data is using client side rendering with the private content sections customer-data requirejs module from the Mage_Customer module.

This can be done using a UI component or a regular custom requirejs module.
How to do this isn't part of the question so I'll won't go into details here.

Implementing the IdentityInterface is only important so the cache gets flushed at the right time. Depending on what your module is rendering that might not even be necessary.

Another thing I consider important for a module to be Varnish compatible is to not add a block with cacheable="false" to any existing page. Adding new non-cached pages might be okay, depending on the type of page.

4
  • 1
    Ok thanks for that I was expecting more "work" but I'm happy with that =) Commented Nov 18, 2016 at 8:58
  • 1
    too fast...... :)
    – Keyur Shah
    Commented Nov 18, 2016 at 8:59
  • I am still not sure on the fate of isPrivateScope() according to devdocs.magento.com/guides/v2.1/config-guide/cache/… its deprecated Commented Nov 18, 2016 at 11:12
  • Yeah, it works and is used by the core somewhere. I guess the docs state that without detailing because of the somewhat surprising behavior and that it's kind of ugly to have to habe to distinguish between cached/non-cached content in toHtml(). They haven't even marked it as deprecated in the code.
    – Vinai
    Commented Nov 18, 2016 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.