I have a custom rest resource that is returning data using new ResourceResponse():

$node = Node::load($nid);
$data = load_data('node', $node);
$response = new ResourceResponse($data);
return $response;

This works fine, but if I use the function Url::fromUri into the load_data function I get the error:

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.

I can get it to work if I use ModifiedResourceResponse instead of ResourceResponse, but as far as I understand this will disable all caching. As this resource might end up getting intensive I want to make sure it is cached - how can I do this?

Many thanks in advance!

  • If you can, try Url::fromUri()->toString()
    – anonymous
    Jan 22, 2021 at 5:03
  • Thanks, I am already using Url::fromUri()->toString() in my code
    – Chris
    Jan 22, 2021 at 5:27

1 Answer 1


This is the answer:


Found here: https://www.lullabot.com/articles/early-rendering-a-lesson-in-debugging-drupal-8

  • 1
    The error message goes away then but this doesn't cache the response correctly. As the error message has shown there is cache metadata generated by rendering the URL. This is now returned by toString(TRUE) in a cacheable object which you need to add to the response $response->addCacheableDependency();.
    – 4uk4
    Jan 22, 2021 at 7:34
  • 1
    It might be a good idea to collect the cache metadata in a separate object which you pass to sub functions processing the response data. See drupal.stackexchange.com/questions/232287/…
    – 4uk4
    Jan 22, 2021 at 7:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.