esmero / strawberryfield

A Field of strawberries
GNU Lesser General Public License v3.0
10 stars 5 forks source link

Search API Excerpts use wrong Cache tags and thus only deliver when called via AJAX/POST + better parsing #257

Open DiegoPino opened 1 year ago

DiegoPino commented 1 year ago

What?

Excerpts that are shown on Views results and Rendered ADOs (or nodes) use only the URL context for caching, and because of that when not called via POST/AJAX (which bypasses caching) get stuck on basically not displaying anything at all when e.g a URL with the search term is called directly (the view Cache wins there).

How to get around this?

I see two options here:

@alliomeria this relates to one of our partners questions.

DiegoPino commented 1 year ago

See:

/**
 * Implements hook_entity_view().
 */
function search_api_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  $excerpt_component = $display->getComponent('search_api_excerpt');
  if ($excerpt_component !== NULL && isset($build['#search_api_excerpt'])) {
    $build['search_api_excerpt'] = [
      '#type' => 'markup',
      '#markup' => $build['#search_api_excerpt'],
      '#cache' => [
        'contexts' => ['url.query_args'],
      ],
    ];
  }

Another option would be (without code) to test if instead of rendering an Entity on the results we can use the "rendered entity" field and output the excerpt as an additional field. That way the excerpt is generated by the Row plugin in Views instead of the actual hook_entity_view() for nodes/view mode. Of course this does not give is much freedom for placing the excerpt inside an existing layout ... mmm

DiegoPino commented 1 year ago

See also https://www.drupal.org/docs/drupal-apis/render-api/cacheability-of-render-arrays