I'm getting the following error, which I've seen in several other questions, but as yet I haven't been able to make any of the example solutions work:

LogicException: "The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early.

Most of the other questions seem to revolve around somebody trying to return a node object, or a URL object, which needs to be added to the response as a cacheable dependency. In my case, I'm just extracting some data from fields and building a simple array with it. But it appears that as soon as I execute a query, the problem rears its head, even if I don't actually use any data from the query:

 * Provides a resource for listing available foo data
 * @RestResource(
 *   id = "foos",
 *   label = @Translation("Foos list"),
 *   uri_paths = {
 *     "canonical" = "/api/v1/foos",
 *   }
 * )
class FooListResource extends ResourceBase {

   * Responds to resource GET requests.
   * @return \Drupal\rest\ResourceResponse
  public function get() {
    $data = [
      'data' => $this->get_valid_years(),
      '#cache' => [
        'contexts' => [
    $response = new ResourceResponse($data);
    return $response;

   * Get the nodes and list years
  private function get_valid_years() {
    $years = ['2018', '2019'];
    $query = \Drupal::entityQuery('node')
      ->condition('status', 1)
      ->condition('type', 'foo_type');
    $nids = $query->execute();
    return $years;


I've also tried this with CacheableJsonResponse, with no better luck. I feel I'm missing something incredibly obvious here. Can anyone enlighten me?



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.