Endless redirect on paths containing question marks
You can try to manually add the query parameters to the redirect URL.

Add this code:

$query = [];
parse_str($current_uri, $query);


just below this code:

$current_uri = \Drupal::request()-&gt;getRequestUri();


And then change this line:

$url = Url::fromRoute('entity.node.canonical', ['node' =&gt; $nid]);


To this:

$url = Url::fromRoute('entity.node.canonical', ['node' =&gt; $nid], ['query' =&gt; $query]);