0

We have the following layout for a modified navigation menu which takes quite some time to generated. We noticed, that the menu seems to be generated again for each category page, even it is always the same.

           <block name="topmenu_generic"
                   as="topmenu"
                   template="Magento_Theme::html/header/topmenu.phtml"
                   ttl="3600"
            >
                <arguments>
                    <argument name="cache_lifetime" xsi:type="string">3600</argument>
                </arguments>
                <block name="topmenu_mobile"
                       as="topmenu.mobile"
                       template="Magento_Theme::html/header/menu/mobile.phtml"
                ></block>

                <block name="topmenu_desktop"
                       as="topmenu.desktop"
                       template="Magento_Theme::html/header/menu/desktop.phtml"
                ></block>
            </block>

...


        <referenceBlock name="topmenu_generic">
            <arguments>
                <argument name="cache_lifetime" xsi:type="number">-1</argument>
            </arguments>
            <block class="Magento\Theme\Block\Html\Topmenu" name="topmenu.navigation" template="Magento_Theme::html/header/topmenu/navigation.phtml" ttl="3600">
                <block class="Magento\Cms\Block\Block" name="menu_virtual_categories">
                    <arguments>
                        <argument name="block_id" xsi:type="string">menu_virtual_categories</argument>
                    </arguments>
                </block>
                <block class="Magento\Cms\Block\Block" name="menu_cms_pages">
                    <arguments>
                        <argument name="block_id" xsi:type="string">menu_cms_pages</argument>
                    </arguments>
                </block>
            </block>
            <block class="Magento\Store\Block\Switcher" name="languages.mobile" template="Magento_Theme::html/header/topmenu/languages-mobile.phtml" />
        </referenceBlock>

How can we properly cache the menu on the first generation so it is pulled from the cache when a user accesses a page that is not yet in the full page cache?

The cache key info should be this:

public function getCacheKeyInfo()
{
    return [
        'BLOCK_TPL',
        $this->_storeManager->getStore()->getCode(),
        $this->getTemplateFile(),
        'base_url' => $this->getBaseUrl(),
        'template' => $this->getTemplate()
    ];
}

So this does not depend on the URL path, right?

Bonus question for my understanding: If a parent block is cached / has a generic cache key and so on, are all the children also cached or do I have to modify the cache key and lifetime information for each child block?

1 Answer 1

0

The culprit is

\Magento\Catalog\Plugin\Block\Topmenu::afterGetCacheKeyInfo

public function afterGetCacheKeyInfo(\Magento\Theme\Block\Html\Topmenu $subject, array $result)
{     
    $activeCategory = $this->getCurrentCategory();
    if ($activeCategory) {
        $result[] = Category::CACHE_TAG . '_' . $activeCategory->getId();
    }

    return $result;
}

This clearly causes the menu to be rendered per-category.

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.