Disable cache for local development in Drupal 8

Viktor Šulák
2 min readApr 12, 2020

--

Are you tired of constant clearing of render caches during Drupal development? Well, there is a solution that might save you some time. You can completely disable render cache for your local development.

To do this, you will need local settings file settings.local.php that can be found or created in sites/default folder. There is already example file out-of-the-box when you install a Drupal 8 site. This file must be included from settings.php by uncommenting following lines:

sites/default/settings.php

if (file_exists($app_root . ‘/’ . $site_path . ‘/settings.local.php’)) {
include $app_root . ‘/’ . $site_path . ‘/settings.local.php’;
}

You must also include development.services.yml inside local file to enable development services of underlaying Symfony framework. One without another is unusable. Comments in the settings.local.php file below explain basic local development perks.

sites/default/settings.local.php

<?php// Include development services file
$settings[‘container_yamls’][] = DRUPAL_ROOT . ‘/sites/development.services.yml’;
// Make error logging more verbose
$config[‘system.logging’][‘error_level’] = ‘verbose’;
// Disable minification of CSS and JS
$config[‘system.performance’][‘css’][‘preprocess’] = FALSE;
$config[‘system.performance’][‘js’][‘preprocess’] = FALSE;
// Disable caches
$settings[‘cache’][‘bins’][‘render’] = ‘cache.backend.null’;
$settings[‘cache’][‘bins’][‘page’] = ‘cache.backend.null’;
$settings[‘cache’][‘bins’][‘dynamic_page_cache’] = ‘cache.backend.null’;
$settings[‘cache’][‘default’] = ‘cache.backend.null’;
// Allow access to rebuild.php
$settings[‘rebuild_access’] = TRUE;
// Do not let Drupal change permissions on files
$settings[‘skip_permissions_hardening’] = TRUE;
// Allow localhost to be trusted host
$settings[‘trusted_host_patterns’] = [
‘^localhost$’,
‘^localhost:.*$’
];

If you use null cache without including this services file, you will get a nasty error. You will also get an error if you will use —-no-dev flag when installing Drupal using Composer and try to use development services.

sites/development.services.yml

parameters:  # Allow debugging of cache headers
http.response.debug_cacheability_headers: true
twig.config: # Add twig debug comments into rendered html
debug: true
# Live reload twig files on change
auto_reload: true
# Disable twig cache
cache: false
services: # Include null cache used in settings.local.php
cache.backend.null:
class: Drupal\Core\Cache\NullBackendFactory

When you have applied these local settings and cleared render caches, you won’t need to clear them anymore. Page load will be significantly slower than loading cached pages, but it is quicker comparing to deleting render cache and waiting for the first page load.

Warning: do not use this in production!!!

--

--

Viktor Šulák

Software engineer, traveller, photographer, motorcycle rider. Interested in fiddling with new technologies.