Search code examples
magentomagento2

Magento2: How to make page non-cacheable in cms


I have added below code to load custom template file from a cms page.

{{block class="Test\PointHistory\Block\Index" template="Test_PointHistory::index.phtml"}}

I want to get the latest data every time I reload this cms page but it always returns data from full page cache.

Can anyone look into this and suggest me?


Solution

  • I found a way to solve this issue. The steps I took to resolve this issue are as follows:

    1. Create a new page layout that inherits Magento's default page layout. Example

      app/design/frontend/<Vendor>/<theme>/Magento_Theme/page_layout/1column-disabled-fpc.xml`
      
      <?xml version="1.0"?>
      
      <layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
          <update handle="1column"/>
          <referenceContainer name="page.wrapper">
              <container name="one-column-disabled-cache" as="one-column-disabled-cache" htmlTag="div" htmlClass="disabled-fpc" />
          </referenceContainer>
      </layout>
      
      
    2. Create file

      app/design/frontend/<Vendor>/<theme>/Magento_Theme/layout/default.xml`
      
      <?xml version="1.0"?>
      
      <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
          <body>
              <referenceContainer name="one-column-disabled-cache">
                  <block name="disabled-fpc" cacheable="false"/>
              </referenceContainer>
          </body>
      </page>
      
      
    3. Create file

      app/design/frontend/<Vendor>/<theme>/Magento_Theme/layouts.xml
      
      <?xml version="1.0" encoding="UTF-8"?>
      
      <page_layouts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/PageLayout/etc/layouts.xsd">
          <layout id="1column-disabled-fpc">
              <label translate="true">1 column (Disabled FPC)</label>
          </layout>
      </page_layouts>
      
      
    4. Go to the Admin and change the layout of the CMS page to 1 column (Disabled FPC)

      enter image description here