LogicException: The controller result claims to be providing relevant cache metadata

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.

Some methods of URL generation cause accountability metadata to be rendered too early, resulting in a fatal error. The solution is to generate the URL differently:

<?php
 
    $options
['absolute'] = TRUE;
   
$url = $this->getUrlGenerator()->generateFromRoute('entity.node.canonical', ['node' => $nid], $options, TRUE);
   
$url->addCacheTags(['node_list']);
   
$response = new CacheableRedirectResponse($url->getGeneratedUrl());
   
$response->addCacheableDependency($url);
    return
$response;
?>