Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Disable cache for a specific page

By editor , 17 September, 2017
Question
  • How to disable cache for a custom page ?
  • How to disable cache for a specific content type ?

Disable cache for a custom page from route declaration.

If you want to disable cache for a custom controller (Custom module), You have no_cache option (YOUR_MODULE.routing.yml).
Example :
File : mymodule.routing.yml

mymodule.myroute:
  path: '/mymodule/mypage'
  defaults:
    _controller: '\Drupal\mymodule\Controller\Pages::mypage'
    _title: 'No cache page'
  requirements:
    _access: 'TRUE'
  options:
    no_cache: 'TRUE'

 

Disable cache for a specific content type

Example:

function yourmodule_node_view(array &$build, NodeInterface $node, $display, $view_mode) {
  if ($node->getType() == 'yourtype' && $view_mode == 'full') {
    $build['#cache']['max-age'] = 0;
  }
}

 

Disable cache from a controller.

Note : This will work in no cache on page level (not working for anonymous users for example).

Set cache max age on rederer array.

$output = [];
$output['#cache']['max-age'] = 0;

 

Tags

  • Drupal 8
  • Code
  • Cache
  • Module
Drupal 8 Cache API
Create a simple module for Drupal 8 - Step 3 - Create Dynamic Page/ Block

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer