MutantMahesh's Blog

Seeking to know.

Apr 20, 2021 - 1 minute read - Smart Content Cloudflare Drupal-Cache

Exclude Smart Content Block from Cloudflare cache

Setup:

  1. A Drupal 8 website configured with smart_content module along with cahcing enabled
  2. Drupal website using Cloudflare for caching, Make sure caching rule at Cloudflare is enabled.

Caching Rules setup at Cloudflare

  1. For www.example.com/ajax/smart_content/config_entity/* Cache Level -> Bypass
  2. For www.example.com/* Cache Level -> Cache Everything
  3. The second rule should be below the rule one, in priority order.

Disable cache for smart content ajax route

namespace Drupal\mymodule\Routing;

 use Drupal\Core\Routing\RouteSubscriberBase;
 use Symfony\Component\Routing\RouteCollection;

 class RouteSubscriber extends RouteSubscriberBase {

/**
 * {@inheritdoc}
 */
protected function alterRoutes(RouteCollection $collection) {
  // Find the route you need ...
  if ($route = $collection->get('smart_content.reaction')) {
    $options = $route->getOptions();
    $options['no_cache'] = TRUE;
    $route->setOptions($options);
  }
 }
}