1

I have Drupal 9. I want to disable cache when query string exists.

This is my code:

function mytheme_preprocess_page(&$variables)
{
  
  if(isset($_SERVER['QUERY_STRING'])) {


    if(!empty($_SERVER['QUERY_STRING']))
    {

      \Drupal::service('page_cache_kill_switch')->trigger();
    }


  }
}

I checked that the table "cache_data" still has new rows with different query string (/pagina-esempio?a=x). I tried with drupal 8 and i remember that rows not inserted.

enter image description here

Maybe i miss something about cache. How can i avoid new rows in this table?

MarioProject
  • 417
  • 4
  • 25
  • I think the kill switch need to be called _earlier_ in the handling of the request, because here in a theme preprocess hook the rendering process has already begun. Instead, you can subscribe to `KernelEvents::REQUEST` and define a specific handler. – EricLavault Dec 15 '21 at 13:33
  • Ok, so i have to learn how to make a listener in drupal. – MarioProject Dec 17 '21 at 09:41
  • I created a listener but still not working, i have new rows in table. – MarioProject Dec 17 '21 at 11:41

0 Answers0