WSOD when redirecting based on cookie I have a site where I set a cookie when the user is visiting a specific content-type:

function THEME_preprocess_node(&amp;$variables) {
  if (isset($variables['node']) &amp;&amp; $variables['node']-&gt;getType() == 'MYTYPE') {
    // Store cookie with id of active node.
    user_cookie_save(['THEME_location' =&gt; $variables['node']-&gt;get('nid')-&gt;getString()]);
  }
}


Then if the user again is going to the frontpage, I would like to redirect him to the node referenced from the above cookie:

function THEME_preprocess(&amp;$variables, $hook) {
  $variables['is_front'] = \Drupal::service('path.matcher')-&gt;isFrontPage();

  // Redirect the user to the selected node.
  $cookies = \Drupal::request()-&gt;cookies-&gt;all();
  if ($variables['is_front'] &amp;&amp; isset($cookies['Drupal_visitor_THEME_location'])) {
    $nid = $cookies['Drupal_visitor_THEME_location'];
    $response = new RedirectResponse(\Drupal::url('entity.node.canonical', ['node' =&gt; $nid]));
    $response-&gt;send();
  }
}


The above works fine most of the time, but sometimes when loggin out from the backend and visiting the frontpage I only get a white screen and the page is completly dead.
I know this is very vague but I cannot seem to reproduce this error by performing some specific steps. It just happens once in a while. If I then clear the cache everything seems to work as expected again? Can someone see what I am doing wrong here?
You should not be using theme_preprocess to perform any processing the results of which cannot be cached, only manipulation of variables to be passed to the final theme processing function or template.

Doing a redirection in a theme_preprocess will cause the result to be empty and this is what leads to the WSOD, I believe.

Instead the preferred method is to use an event subscriber, as described on this page: https://www.drupal.org/node/2013014


  Register your Event Subscriber in your mymodule.services.yml and tag it as such:


services:
  mymodule_event_subscriber:
    class: Drupal\mymodule\EventSubscriber\MymoduleSubscriber
    tags:
      - {name: event_subscriber}



  Your MymoduleSubscriber should be placed in folder structure of modules/src/EventSubscriber/MymoduleSubscriber.php and should implement the EventSubscriberInterface interface and in its getSubscribedEvents() point to the method that should be executed:


namespace Drupal\mymodule\EventSubscriber;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MymoduleSubscriber implements EventSubscriberInterface {

  public function checkForRedirection(GetResponseEvent $event) {
    if ($event-&gt;getRequest()-&gt;query-&gt;get('redirect-me')) {
      $event-&gt;setResponse(new RedirectResponse('http://example.com/'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('checkForRedirection');
    return $events;
  }

}