How to handle cache metadata in a cacheable response?
You have to add the cacheable metadata from the render array to the response:

use Drupal\Core\Cache\CacheableMetadata;

  $rendered = \Drupal::service('renderer')-&gt;renderRoot($build);
  $response = new CacheableResponse($rendered);
  $response-&gt;addCacheableDependency(CacheableMetadata::createFromRenderArray($build));
  return $response;