I've implemented a conditional redirect on my site via an event subscriber and returning a redirect response. It's working but, I believe due to the page_cache
module is getting cached for anonymous users.
This is basically what I'm using to set the redirect response:
public function checkRedirectStatus(GetResponseEvent $event) {
if(shouldIRedirect()) {
$response = new RedirectResponse('/redirect/path');
$event->setResponse($response);
}
}
The above gets cached whether or not it returns true unless I add \Drupal::service('page_cache_kill_switch')->trigger();
.
As this is a fairly high-traffic page I'm concerned about bypassing cache for anonymous users. Are there other options I'm not aware of?
If this is on the REQUEST
kernel event, how far-reaching is page_cache_kill_switch
?
Thanks for your help!
page_cache_kill_switch
? This depends on how often you trigger it. You need it for all pages where theoretically the redirect could happen. If this is the entire site, then all your content is dynamic and the internal page cache doesn't make any sense and you can un-install it. In this case use a cacheable redirect response, see the answer of @sonfd, so that you can take full advantage of the dynamic page cache.