Drupal 8 – Disable Cache in a Devel Environment

If you've developed in Drupal before you're familiar with the classic question "have you cleared the cache?". If you haven't developed in Drupal before, let me ask you this: – Have you cleared the cache?

To do any kind of decent development you need a non-cached environment, so that changes you make to the code are reflected upon reloading in the browser. In this second article of the Drupal 8 Learning Series we'll look into how to configure your cool new D8 project for development.

Out of the box Drupal 8 comes with the maximum possible amount of caching enabled. The reasoning behind this is that a site should be as performant as possible with the default settings. This is all good and well. But usually you start by doing some development and then it's not so great if the changes you make in code are not reflected when you refresh.  It can be quite tricky to disentangle the web of cache settings if you're new to Drupal 8. So let's have a look at how to set up a local development environment with no cache.

1. Edit settings.php

The first step to "uncache" your D8 site is to uncomment a few lines in settings.php

if( file_exists(__DIR__.’settings.local.php’) ) {
    include __DIR__ . ’settings.local.php’;
}

In more recent versions of Drupal 8 this will be:

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

What these line of codes do is to include a file called settings.local.php. In a standard installation this file will be located in sites/default/settings.local.php.

 Copy sites/example.settings.local.php to sites/default/settings.local.php

This new file will provide overrides to the regular settings.php file and in a local development environment caching will be disabled in these settings. To make Drupal register (read) this new settings.local.php file you need to clear the cache with Configuration -> Performance -> Clear all caches or drush cr.

2. Use development.services.yml

Using the sites/default/settings.local.php file will also include and use sites/development.services.yml.

This file should already exist in you Drupal 8 project out of the box. It will override the configuration in sites/default/services.yml. Personally I find it confusing that the sites/development.services.yml override lives one directory level above sites/default/services.yml. But it does. 

The important part of the sites/development.services.yml is the declaration of 
$settings['cache']['bins']['render'] = 'cache.backend.null';
which will return a "null" cache. Which means no cache.  

# Local development services.
#
# To activate this feature, follow the instructions at the top of the
# 'example.settings.local.php' file, which sits next to this file.
services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory

3. Debugging Twig

So now we have a Drupal 8 site that doesn't cache everything by carving it into rock. But we still need to debug twig and stop caching template files. You also need to do this in order to get theme hook suggestions. No problem: just add the "parameters" below to your sites/development.services.yml

# Local development services.
#
# To activate this feature, follow the instructions at the top of the
# 'example.settings.local.php' file, which sits next to this file.
services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory

# Enable Twig debugging.
parameters:
  twig.config:
    debug: true
    auto_reload: true
    cache: false

(We'll be discussing the theme hook suggestions in a future article of Drupal 8 Learning Series. Stay tuned.)

4. Don't trip up on browser cache

Sometimes when you configure your local development environment like I've described above you still have to empty cache "manually" between every page load. Why? What is wrong? Most likely it's the browser cache settings. Make sure you've disabled browser caching. In Chrome this is how:

Open settings in Google Chrome
Open settings in Google Chrome
Disable caching in Google Chrome
Disable caching in Google Chrome


Read more on this topic here:
https://www.drupal.org/node/2598914
https://knpuniversity.com/screencast/drupal8-under-the-hood/debugging#play