1

How can I disable caching for the front page, programmatically?

  • 1
    Assuming the page cache? – mikeytown2 Jul 28 '16 at 16:26
  • yes assuming page cache – Reshma Jul 29 '16 at 5:39
2

I think one way to achieve that is by altering '<front>' route with a RouteSubscriber service.

  mymodule.route_subscriber:
    class: Drupal\mymodule\Routing\RouteSubscriber
    arguments: ['@config.factory']
    tags:
      - { name: event_subscriber }

Then in your RouteSubscriber.php class :

/**
 * Event subscriber.
 */

namespace Drupal\mymodule\Routing;

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

/**
 * Subscribe to KernelEvents::REQUEST events and redirect if user is press.
 */
class RouteSubscriber extends RouteSubscriberBase  {

    protected $configFactory;

    /**
     * RouteSubscriber constructor.
     * @param ConfigFactoryInterface $config_factory
     */
    public function __construct(ConfigFactoryInterface $config_factory) {
        $this->configFactory = $config_factory;
    }

    /**
     * {@inheritdoc}
     */
    public function alterRoutes(RouteCollection $collection) {
        if ($route_front = $collection->get('<front>')) {
            $route_front->setOption('no_cache', TRUE);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.