0

I need to disable the cache for a specific page, or get the cache to ignore query params. I have tried this, but it does not disable the cache for me https://www.drupal.org/project/page_cache_query_ignore

After trying multiple ways to disable the cache for a specific page or even the whole site I have nothing that works.

I have tried all of these in settings.php

$settings['cache']['default'] = 'cache.backend.null';
$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';
$settings['cache']['bins']['page'] = 'cache.backend.null';
$settings['cache']['bins']['entity'] = 'cache.backend.null';

There is also talk of using \Drupal::service('page_cache_kill_switch')->trigger(); but I am unsure where to place this to disable the cache for a regular page.

This is for the search page, that uses query params for the search query. Drupal 8 is caching the page so each visit to the search after the first search always shows the results for the first search.

The header for Drupal dynamic cache is always a HIT

X-Drupal-Dynamic-Cache: HIT

  • 2
    When you say you want cache to ignore query params, do you mean that you want the page to be cached separately for each possible query param? What kind of page is it? Does the whole page depend on the query parameters? or just a piece of content on the page, e.g. the main content area? It sounds like you need to add a url.query_args cache context somewhere, but will need more info to tell you where. – sonfd Aug 12 at 11:43
  • It is the search page if that helps, and there is another page that needs to ignore query params too, this is just a regular page as far as I know. Caching not at all or individually is fine. This is not a module, it just a page with a search on it and another page. – Jake N Aug 12 at 11:57
  • Is the site behind Varnish or similar? – Clive Aug 12 at 12:33
  • Yes, but the issue is on every environment, even local with no caching of that sort – Jake N Aug 12 at 12:42
  • 1
    Definitely sounds like you’ve got something non-standard configured. If manually setting the cache backends to null in settings.php doesn’t stop the caches being used, and you’re positive there’s no external reverse proxy responsible, you probably have some dodgy code in there somewhere. What powers the search page? Search API or core? – Clive Aug 12 at 12:58
1

After a lot of trying and retrying I settled on the following solution

function mythemename_preprocess_page(&$variables)
{
  if(isset($_SERVER['QUERY_STRING'])) {
    $params = \GuzzleHttp\Psr7\parse_query($_SERVER['QUERY_STRING']);
    if(isset($params['keys']) && strlen($params['keys']) > 0 && preg_match('#^/search#', $_SERVER['REQUEST_URI'])) {
      \Drupal::service('page_cache_kill_switch')->trigger();
    }
  }
}

As far as I can tell, this stops the cache from working if the request is on the search page and there is a query parameter present named keys

| improve this answer | |
0

I'd say that if you haven't been able to completely turn off cache then something is still preventing you from fully loading 'fresh' pages on every request, which may mean there's still some parameter deep down that you might consider reconfiguring. I'd suggest to follow this guide to Disable Drupal 8 caching during development on your local environment before going mad.

| improve this answer | |
  • I have followed that and still get X-Drupal-Dynamic-Cache HIT after visiting a page and then refreshing – Jake N Aug 12 at 13:06
  • 1
    If you’ve disabled Drupal’s cache, but everything is still cached, it means you’re either connected to a proxy in front of Drupal, not Drupal, or you have some code in the site causing the problem. If you’re convinced the reverse proxy isn’t it, you should probably contact whoever set up the search, or prepare for a debugging session – Clive Aug 12 at 13:19
  • It's not related to the search as it happens on all pages. Should what I have done in my original post disable the cache? – Jake N Aug 12 at 13:49
  • Assuming you have a development.services.yml defining the null cache backend, and that’s also included via settings.pho, then yes – Clive Aug 12 at 13:52
  • You can of course prove it by truncating all the tables in the database beginning cache_. If that fixes it, the problem is probably in code, otherwise it’s probably an external cache layer – Clive Aug 12 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.