How to cache a custom rest resource when using Url::fromUri 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);
$response-&gt;addCacheableDependency($node);
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!
This is the answer:
Url::fromUri($var)-&gt;toString(TRUE)-&gt;getGeneratedUrl();

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