I can't use a normal
RedirectResponse - Drupal prevent's it when it detects I'm redirecting to an external site. Instead I need to use a
TrustedRedirectResponse, but this is a cacheable response - which I don't want.
Disable caching in responses
You can build the cacheable metadata and put max-age = 0 in it:
$build = [ '#cache' => [ 'max-age' => 0, ], ]; $cache_metadata = \Drupal\Core\Cache\CacheableMetadata::createFromRenderArray($build); $response->addCacheableDependency($cache_metadata);
If you want to put this in one line:
$response ->addCacheableDependency((new \Drupal\Core\Cache\CacheableMetadata())->setCacheMaxAge(0));
Disable caching for anonymous requests
Also you have to disable the page cache for anonymous requests
This is necessary no matter if you use a normal redirect response or one with drupal caching.