n Disable cache for a specific page in drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

Code snippet that can use to disable cache for a specific page in drupal 8.

Disable cache for a specific content type

Example:

<?php

use Drupal\node\NodeInterface;

/**
 * @param array $build
 * @param NodeInterface $node
 * @param $display
 * @param $view_mode
 */
function mymodule_node_view(array &$build, NodeInterface $node, $display, $view_mode) {
  $type = $node->getType();
  if ($type == 'article') {
    $build['#cache']['max-age'] = 0;
   \Drupal::service('page_cache_kill_switch')->trigger();
  }
}

Add 'no_cache' route option

Example:

In a route definition (in a *.routing.yml file):

mymodule.route:
   path: '/some/path'
   defaults:
     _controller: '\Drupal\Some\Controller::response()'
   options:
     no_cache: TRUE

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook