0

I have read a couple of similar questions and tried various methods in these tickets to disable any caching for this particular page of the site in Drupal 10 to no avail. These included invalidating cache for the page/block/view, changing the cache age for the templates, adding cache contexts.

I have tried cache exclude and page cache exclusion modules as well, they didn't work.

It is a bit of a complicated setup, bare with me, so in a custom content type, I output a view block, this content type also has some paragraph fields that I use. The idea is that the view makes a small list of items as you land to the page, and outputs also these paragraph fields. Once you use the filters of the view, it changes the template with conditional logic and it displays only the view results.

The problem is, that if you filter a few times, then reset and go back to the landing page. The landing page gets rendered with only the filters on top, no paragraph items nor view results get rendered. (I guess that is because Drupal caches the filtered version at this point, therefore no paragraph items are visible) Sometimes that can happen if you filter a certain result too, but that is more rare, still an issue though.

If I switch off from development the caching entirely, or clear caches, this pice of work works like a wonder. However on Prod, that won't be an option, therefore I need to find a way to invalidate/exclude caching for the components of this page only.

This is my theme functionality:

function mytheme_preprocess_views_view(&$variables) { if ($view->id() === 'news_solr_index') { // Adds a variable to the view so that we know if it is the landing or filtered view. $has_input = $variables['view']->getExposedInput(); if ($has_input) { $variables['has_input'] = TRUE; } else { $variables['has_input'] = FALSE; } $variables['aoi_links'] = mytheme_get_aoi(); $variables['news_type_links'] = mytheme_get_news_type(); // fetch items from the host node's field_featured_articles and exclude them from the main rows. $view = $variables['view']; $filteredResults = []; // Load the parent node $node = Node::load(8261); if ($node->hasField('field_featured_articles') && $has_input == FALSE) { $manual_entries = $node->field_featured_articles->getValue(); } if (empty($variables['#cache']['tags'] )) { $variables['#cache']['tags'] = []; } $variables['#cache']['tags'] = \Drupal\Core\Cache\Cache::mergeTags($variables['#cache']['tags'], $node->getCacheTags()); // Clean up the manual results if (!empty($manual_entries)) { foreach ($manual_entries as $article) { $filteredResults[$article['target_id']] = $article['target_id']; } } // Get the view results and make sure they are unique foreach ($view->result as $id => $result) { $filteredResults[$result->_entity->id()] = $result->_entity->id(); } // Clean values $filteredResults = array_values($filteredResults); // Put values back to the view results $view->result = $filteredResults; $variables['filteredResults'] = array_values($filteredResults); } } 

This is the view template:

{% set classes = [ dom_id ? 'js-view-dom-id-' ~ dom_id, ] %} {% set wrapper_classes = [ has_input ? 'hsnews-searchresults hs-col-5ths-tablet-4' : 'hs-col-5ths-tablet-2', 'columns small-12', ] %} <div{{ attributes.addClass(classes) }}> {{ title_prefix }} {{ title }} {{ title_suffix }} {% if header %} <header> {{ header }} </header> {% endif %} {{ exposed }} {{ attachment_before }} <div class="row"> {% if has_input == false %} <div class="hs-grid"> <div class="columns small-12"> {% if filteredResults[0] is not empty %} {{ drupal_entity('node', filteredResults[0], 'large_featured_article') }} {% endif %} </div> </div> {% endif %} <div class="hs-grid"> {% if has_input == false %} <div class="columns small-12 hs-col-5ths-tablet-2"> {% if filteredResults[1] is not empty %} {{ drupal_entity('node', filteredResults[1], 'featured_article') }} {% endif %} {% if filteredResults[2] is not empty %} {{ drupal_entity('node', filteredResults[2], 'featured_article') }} {% endif %} </div> {% endif %} <div{{ create_attribute().addClass(wrapper_classes) }}> {% if has_input %} {# Calculate the total number of pages #} {% set total_pages = view.total_rows / view.pager.options.items_per_page %} {% if view.query.Keys[0] is not empty %} {% set keyword = ' for ' ~ view.query.Keys[0] %} {% endif %} {# Page 1 of 10 (458 results) #} <h6>Page {{ view.pager.current_page + 1 }} of {{ total_pages|round(0, 'ceil')|number_format }} ({{ view.total_rows }} Results)</h6> <hr> {% endif %} {% if rows -%} {# We have outputed the 3 first items above, so we cut them off on the loop so that they don't repeat #} {% if has_input == false %} {% set filteredResults = filteredResults|slice(3, 8) %} {% for result in filteredResults %} {{ drupal_entity('node', result, 'card_large') }} {% endfor %} {% else %} {# Else output the normal results #} {% for result in view.result %} {{ drupal_entity('node', result, 'card_large') }} {% endfor %} {% endif %} {% elseif empty -%} {{ empty }} {% endif %} {% if has_input %} {{ pager }} {% else %} <div class="more-link"><a href="/news?search=">See All</a></div> {% endif %} {{ attachment_after }} {{ more }} </div> {% if has_input == false %} <div class="columns small-12 hs-col-5ths-tablet-1 no-section-pad"> <h4>Quick Filters</h4> <br> <div class="facets-widget-links"> <h6>News Type</h6> {# NOTE: This filter is statically generated from the theme #} <ul class="facet-inactive item-list__links list-unbulleted list-small"> {% for link,title in news_type_links %} <li class="facet-item"> <a href="/news?{{ link }}" rel="nofollow"> <span class="facet-item__value">{{ title }}</span> </a> </li> {% endfor %} </ul> </div> <br> <div class="facets-widget-links"> <h6>Areas of Interest</h6> <ul class="facet-inactive item-list__links list-unbulleted list-small"> {% for link,title in aoi_links %} <li class="facet-item"> <a href="/news?{{ link }}" rel="nofollow"> <span class="facet-item__value">{{ title }}</span> </a> </li> {% endfor %} </ul> </div> </div> {% endif %} </div> </div> <div class="row"> <div class="hs-grid"> {% if has_input == false %} <div class="columns small-12"> {# Render here the components of the Grid Index News node #} {{ drupal_field('field_components', 'node', 8261) }} </div> {% endif %} {% if footer %} <footer> {{ footer }} </footer> {% endif %} {{ feed_icons }} </div> </div> </div> 

Any help is appreciated!

2
  • The code you posted has errors $build['#cache']['max-age'] = 0 is declared but never used. Commented May 6 at 16:03
  • Does the URL vary between these instances? Because if so, the correct way to handle this would be via cache context, most likely 'url.query_args'. If this is added then the results get cached separately depending on the query. Tags are not the way to go: tags tell you when to invalidate the cache depending on something else being updated. Commented Jun 14 at 18:58

2 Answers 2

4

Don't disable cache, add proper caching information instead:

/** * I don't know where the pre-existing cache info is stored in your case, * debug and correct if $variables['#cache'] is not fitting */ $variables['#cache']['tags'] = \Drupal\Core\Cache\Cache::mergeTags($variables['#cache']['tags'], $node->getCacheTags()); 
4
  • That may have worked, but it seems $variables['#cache']['tags'] is empty inside the mytheme_preprocess_views_view. I wonder if I don't merge tags but just add them. Commented May 7 at 8:35
  • @Alimba Always merge - the Views module may add some default caching at some point in the future, and you'd end up removing it otherwise Commented May 7 at 9:56
  • I have updated the code, based on the comments you provided. I had to add a condition for the $variables['#cache']['tags'] because it wasn't always available and then it would cause the mergeTags to fail. However, still it will not work, same exact issue, like nothing has changed. Commented May 7 at 10:08
  • Another method to merge cacheable metadata of objects \Drupal::service('renderer')->addCacheableDependency($variables, $node); Commented May 7 at 11:59
1

It looks like you are trying to set a max-age cache setting to 0. There is a bug in Drupal core that prevents that from actually working. Patches on that issue do fix it.

3
  • 1
    This is not a bug, this is a known limitation of the Internal Page Cache for anonymous users. It'd be easier if the question had only the code to reproduce the issue, but as it contains \Drupal::service('page_cache_kill_switch')->trigger(); it's unlikely the linked topic is involved. Commented May 7 at 6:54
  • That’s fair. Can someone convert my answer to a comment? Commented May 7 at 11:19
  • I just tried the module plus the patch used for the D10 core, it doesn't seem to be doing anything. Commented May 7 at 12:07

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.