Categories
Web blog

Disable cache and enable Twig debug in Drupal 8

On production Drupal sites, caching should always remain enabled for best performance. However, during development, caching should be disabled. This tutorial shows how to disable cache in Drupal 8, and also enable debugging for development.

Enable cache in settings

First, copy sites/example.settings.local.php to sites/default/settings.local.php:

// uncomment this line 
$settings['cache']['bins']['render'] = 'cache.backend.null';

Enable local settings file

In settings.php:

// uncomment this line
if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) {
  include $app_root . '/' . $site_path . '/settings.local.php';
}

Now, clear all cache to pick up changes to settings.

Troubleshooting: You have requested a non-existent service “cache.backend.null”

If this error is displayed after clearing the cache, load /core/rebuild.php in your browser.

If that still doesn’t work, install Drush and run `drush cache-rebuild` command.

Twig config

Add twig.config parameters to sites/development.services.yml. These parameter keys can be copied from sites/default/default.services.yml.

parameters:
  http.response.debug_cacheability_headers: true
  twig.config:
    debug: true
    auto_reload: true
    cache: false
services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory

Again, clear the cache for changes to take effect. You should now see debug information in the page source. Also, changes to templates shouldn’t require cache to be cleared in development any more.

Leave a Reply

Your email address will not be published. Required fields are marked *