Magento 2, Individually Excluding Blocks From Cache

Magento’s view is built by a collection of blocks. Some blocks are cached to boost performance. Some blocks are left out of cache because they’re unique to a session or dynamic. Magento 2 does a good job of sorting out most of that for you. Eventually there will come a time when customization interferes and you find yourself excluding blocks from cache.

Explanation

One of the more common areas I find getting stuck in cache is the welcome message. The welcome message on most of my builds has the first and last name of the user currently logged in, as well as a log out and my account link. These links are swapped out with other links that a user not logged in sees.

Welcome Message Excluding Blocks

Magento 1’s hole punching mentality was a large undertaking with lots of code and a module. Cache exclusion in Magento 2 is easily solved thanks to a new way of thinking.

Controlling Blocks and Cache

<block class="Magento\Framework\View\Element\Template" name="my-account" template="Magento_Theme::html/myaccount.phtml" cacheable="false">
    <arguments>
        <argument name="label" xsi:type="string" translate="true">My Account</argument>
    </arguments>
</block>

This is my default.xml layout file inside the Magento_Theme folder of my custom theme. Take note of line 1 all the way to the end. This is where you can tell Magento 2 to exclude the block from cache. You can add this element to any block in any layout file.

Why exclude individual blocks?

Excluding individual blocks from cache is a must for the dynamic and personal aspects of your web site. If Jack is logging in and sees you welcome Martha, you’re only going to cause them to leave. Also imagine Jack trying to order a new case for his phone and seeing an incorrect listing of Martha’s charger in the mini-cart.

Performance is also another thing to think about. Sure, you could exclude an entire page from cache, but then you’re opening yourself up to slow page loads. Slow page loads have negative SEO effects. Even if you’re not concerned with SEO, slow pages are a bad user experience and will detour shoppers. By individually excluding blocks from cache, you keep the rest of the page loading fast while accomplishing your goals.

Another reason to exclude only the individual blocks necessary is debugging and updating the site’s code. If you’re working on performance tuning your page, and have the entire thing excluded from cache, you’re not going to easily be able to go remedy the more taxing parts of the page. You’ll need to put debugging and other logging in place all over the place. You will ultimately spend more time figuring out which piece of the page is causing the most trouble.

What should I think about?

Excluding blocks can be risky. You could cause horrible performance issues, or break the site entirely. Giving it some thought is always a good decision. Ask yourself some key questions about what you’re doing.

Will this cause the page to slow down too much? Is this necessary?

Can I had a TTL setting in my cache settings rather than exclude the block?

What you don’t want to do is exclude something that is massively taxing your database or other aspects of page loading. Think about performance and a good experience at the same time.

Another thing to consider is what type of cache server you’re using. If you’re using Magento’s basic full page cache, you should be able to just exclude things and put TTL settings in your blocks. If you’re using Varnish, you won’t be able to do that and will want to take a more delicate approach.

Make a plan before you start. We’ve taken the approach of researching every aspect of all pages that contain the block that we’re excluding from cache. You don’t want to put an exclusion in if that block is going to be a problem for some pages.

Conclusion

It’s good architecture to only exclude the individual blocks. You gain performance from caching the rest of the page while keeping things dynamic and personal in other areas. There are always good reasons to exclude certain blocks from cache. Make a well thought out plan and stick to it. You’ll have a good user experience, a well operating web site and good performance.

Written by Dan
Welcome to my blog! Here you'll find my collection of bible study lessons, book reviews, and other posts I feel inspired to write. I am a Christian currently serving at First Baptist Church of Northville in Northville, MI.