Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Create A Non-cacheable Block in Magento 2

Vinh Jacker | 11-11-2024

How to Create A Non-cacheable Block in Magento 2

Non-cacheable blocks in Magento 2 play a crucial role in shaping the caching behavior of web pages. Since these blocks are not cached, they are re-rendered on each request, ensuring that the latest information is always displayed to the user. Consequently, they are particularly useful for elements like shopping cart summaries, customer-specific content, or any other data that needs to be updated frequently.

In this guide, we’ll explore 2 effective methods to create a non-cacheable block in Magento 2. They consist of using a custom block class and any existing block. This will ensure that your dynamic content stays up-to-date for your customers. Let’s get started!

How to Create A Non-cacheable Block in Magento 2

In Magento 2, you can create a non-cacheable block by extending the \Magento\Framework\View\Element\Template class and setting the cacheable attribute to false.

Here’s a basic 2 methods to achieve this:

Method 1: Use Custom Block Class

1 . Create your custom block class, which will extend the \Magento\Framework\View\Element\Template class.

For example, name the custom block class NonCacheableBlock


<?php
namespace Vendor\Module\Block;

class NonCacheableBlock extends \Magento\Framework\View\Element\Template
{
    /**
     * @return bool
     */
    protected function _isScopePrivate()
    {
        return true;
    }

    /**
     * @return $this
     */
    protected function _prepareLayout()
    {
        $this->addData([
            'cache_lifetime' => false,
            'cache_tags' => [],
            'cache_key' => $this->getCacheKey()
        ]);
        return parent::_prepareLayout();
    }
}
  1. Create a template file in app/code/Vendor/Module/view/frontend/templates/noncacheableblock.phtml ```

This is a non-cacheable block


3. Create layout XML file located in ``app/code/Vendor/Module/view/frontend/layout/default.xml``

<?xml version=”1.0”?>

4. Clear the cache after adding the block. Then check the content of the page

### Method 2: Use Any Existing Block

You can apply ``cacheable=”false” `` attribute directly in your layout XML file to disable cache for the block.

Run the code below :

<?xml version=”1.0”?>

``` In conclusion Creating non-cacheable blocks in Magento 2 allows you to display dynamic content that requires real-time updates without being affected by the caching mechanism. By following the steps outlined in this guide, you can make sure that your e-commerce website delivers a personalized and seamless experience for your customers.

Table of content
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services