diff --git a/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php b/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php
index 31192f3..66bcc4c 100644
--- a/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php
+++ b/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php
@@ -72,9 +72,9 @@ public function renderResponse(array $main_content, Request $request, RouteMatch
     // behavior can be changed with #ajax['method'].
     $response->addCommand(new InsertCommand(NULL, $html));
     $status_messages = array('#type' => 'status_messages');
-    $output = $this->drupalRenderRoot($status_messages);
+    $output = trim($this->drupalRenderRoot($status_messages));
     if (!empty($output)) {
-      $response->addCommand(new PrependCommand(NULL, $output));
+      $response->addCommand(new PrependCommand(NULL, $this->drupalRenderRoot($status_messages)));
     }
     return $response;
   }
