Many people may tell you this is what NOT to do, but for exotic and incomprehensible reasons, TrustedRedirectResponse can cause your controller to throw the error about metadata leaking "somewhere". I checked all around my desk and on the floor, and I couldn't find any metadata :) This can happen even if you disable caching for your route in every ...
You can try to manually add the query parameters to the redirect URL.
Add this code:
$query = ;
just below this code:
$current_uri = \Drupal::request()->getRequestUri();
And then change this line:
$url = Url::fromRoute('entity.node.canonical', ['node' => $nid]);
$url = Url::fromRoute('entity.node....