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!
$build['#cache']['max-age'] = 0is declared but never used.