You are here

public function SearchApiSortsBlock::getCacheMaxAge in Search API sorts 8

The maximum age for which this object may be cached.

Return value

int The maximum time in seconds that this object may be cached.

Overrides ContextAwarePluginBase::getCacheMaxAge

File

src/Plugin/Block/SearchApiSortsBlock.php, line 128

Class

SearchApiSortsBlock
Exposes a search api sorts rendered as a block.

Namespace

Drupal\search_api_sorts\Plugin\Block

Code

public function getCacheMaxAge() {

  // A search api sorts block cannot be cached, because it must always match
  // the current search results, and Search API gets those search results from
  // a data source that can be external to Drupal. Therefore it is impossible
  // to guarantee that the search results are in sync with the data managed by
  // Drupal. Consequently, it is not possible to cache the search results at
  // all. If the search results cannot be cached, then neither can the search
  // api sorts, because they must always match.
  // Fortunately, search api sorts blocks are rendered using a lazy builder
  // (like all blocks in Drupal), which means their rendering can be deferred
  // (unlike the search results, which are the main content of the page, and
  // deferring their rendering would mean sending an empty page to the user).
  // This means that search api sorts blocks can be rendered and sent *after*
  // the initial page was loaded, by installing the BigPipe (big_pipe) module.
  //
  // When BigPipe is enabled, the search results will appear first, and then
  // each search api sorts block will appear one-by-one, in DOM order.
  // See https://www.drupal.org/project/big_pipe.
  //
  // In a future version of search api sorts API, this could be refined, but
  // due to the reliance on external data sources, it will be very difficult
  // if not impossible to improve this significantly.
  return 0;
}