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

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon