Magento Full Page Cache Warmer Extend Cache Warmer and Add Custom URLsFull Page Cache Warmer

« Back to Magento Full Page Cache Warmer

The Cache Warmer will automatically generate a list of your product, category, CMS Page, WordPress blog and Splash page URLs and warm them for you. If you want to add custom URLs, the easiest way to do this is to add them to the Custom URL section of the Cache Warmer configuration in the Magento Admin. This works if you have a small amount of URLs but if you have a larger amount, you will find this method quite slow. For a large number of URLs, you should consider writing a small Magento module and using an event to add the URLs for you.

Add Custom URLs to the Cache Warmer

Let's start by creating the basic module files that we will need.

app/etc/modules/Fishpig_CacheWarmerCustomUrls.xml

<?xml version="1.0"?>
<config>
	<modules>
		<Fishpig_CacheWarmerCustomUrls>
			<active>true</active>
			<codePool>local</codePool>
		</Fishpig_CacheWarmerCustomUrls>
	</modules>
</config>

app/code/local/Fishpig/CacheWarmerCustomUrls/etc/config.xml

<?xml version="1.0"?>
<config>
	<modules>
		<Fishpig_CacheWarmerCustomUrls>
			<version>1.0.0.0</version>
		</Fishpig_CacheWarmerCustomUrls>
	</modules>
	<global>
		<events>
			<cachewarmer_add_urls>
				<observers>
					<cachewarmercustomurls>
						<model>Fishpig_CacheWarmerCustomUrls_Model_Observer</model>
						<method>addCustomUrlsToCacheWarmerObserver</method>
					</cachewarmercustomurls>
				</observers>
			</cachewarmer_add_urls>
		</events>
	</global>
</config>

app/code/local/Fishpig/CacheWarmerCustomUrls/Model/Observer.php

<?php
/**
 * @category Fishpig
 * @package Fishpig_CacheWarmerCustomUrls
 */

class Fishpig_CacheWarmerCustomUrls_Model_Observer
{
	/**
	 * Retrieve the message
	 *
	 * @param Varien_Event_Observer $observer
	 * @return Fishpig_CacheWarmerCustomUrls_Model_Observer
	 */
	public function addCustomUrlsToCacheWarmerObserver(Varien_Event_Observer $observer)
	{
		// Get the transport object. This is used to pass the URLs back to the Cache Warmer
		$transport = $observer->getEvent()->getTransport();
		
		// Get the existing URLs. This stops URLs from other modules being overwritten
		$urls = $transport->getUrls();

		// Add a single URL
		$urls[] = '/custom-url.html';
		
		// Merge an array of URLs
		$urls = array_merge($urls, array(
			'/some/custom/url.html',
			'http://www.example.com/url.html',
			'/another/relative/',
		));
		
		// Pass the new URLs back to the transport
		$transport->setUrls($urls);
		
		return $this;
	}
}

You will need to change the URL generation part to include code to generate a list of the URLs that you require. These could be loaded from another module, from a CSV or using any method that you want. Just ensure that:

  • you first call $transport->getUrls() to get any URLs set by any other modules. You then add any new URLs to this array.
  • You pass the new array of URLs to the Transport via $transport->setUrls($urls)

Magento
Full Page Cache Warmer
Documentation