Magento – Static Blocks shown crazy in frontend

block-cachece-1.9.2.0

There are new problems with static blocks in 1.9.2

there is one more problem with CMS blocks caching, that is not fixed.

If you are using secure urls and {{media}} tags in your CMS blocks,
you will receive "Insecure content warning" message from browser, as
Magento serves insecure links from cache.

To resolve it, you need to add one more cache info tag, as

(int)Mage::app()->getStore()->isCurrentlySecure(),

Does anyone know where to add this code-snippet?

KNOWN BUG: http://www.magentocommerce.com/bug-tracking/issue/index/id/870

Best Answer

By default, Magento CMS Block will not be cached. So it would be nice if we cache CMS static blocks. This is what this extension does (one of my free extension). Feel free to use it.

Basically this extension listens to the event core_block_abstract_to_html_before and enable cacheing for CMS Blocks. This is what observer does

public function enableCmsBlockCaching(Varien_Event_Observer $observer)
{
    $block = $observer->getBlock();

    if ($block instanceof Mage_Cms_Block_Widget_Block
        || $block instanceof Mage_Cms_Block_Block
    ) {

        $cacheKeyData = array(
            Mage_Cms_Model_Block::CACHE_TAG,
            $block->getBlockId(),
            Mage::app()->getStore()->getId(),
            intval(Mage::app()->getStore()->isCurrentlySecure())
        );
        $block->setCacheKey(implode('_', $cacheKeyData));

        $block->setCacheTags(array(
            Mage_Core_Model_Store::CACHE_TAG,
            Mage_Cms_Model_Block::CACHE_TAG,
            (string)$block->getBlockId()
        ));

        $block->setCacheLifetime(false);
    }
    return $this;
}

What this Observer Does : It first retrieve each static blocks from the layout and then apply cache. The unique cache key is generated for each static blocks. This unique key is made up of

  • CMS cache tag
  • Static Block Identifier
  • Current Store Used
  • Secure Url or Not

So the cache key will be unique in most of the circumstances and you already saw that for secure and not secure urls, cache key will be different. That's it.

Note : As @AdershKatri pointed out, you should also have look on this thread

If you have any doubts, let me know.

Related Topic