diff --git a/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php b/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php
index 0a1b993..dfc568c 100644
--- a/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php
+++ b/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php
@@ -68,7 +68,9 @@ public function renderResponse(array $main_content, Request $request, RouteMatch
     $response->addCommand(new InsertCommand(NULL, $html));
     $status_messages = array('#type' => 'status_messages');
     $output = $this->drupalRenderRoot($status_messages);
-    if (!empty($output)) {
+    $output_trimmed = trim($output);
+    if ($output_trimmed != '') {
+      // There are messages so output them exactly as the theme renders them.
       $response->addCommand(new PrependCommand(NULL, $output));
     }
     return $response;
diff --git a/core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php b/core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php
index 9dfc690..ed944ae 100644
--- a/core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php
+++ b/core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php
@@ -11,6 +11,7 @@
 use Drupal\Tests\UnitTestCase;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Drupal\Core\Render\ElementInfoManagerInterface;
 
 /**
  * @coversDefaultClass \Drupal\Core\Render\MainContent\AjaxRenderer
@@ -33,6 +34,13 @@ class AjaxRendererTest extends UnitTestCase {
   protected $renderer;
 
   /**
+   * The element info manager.
+   *
+   * @var \Drupal\Core\Render\ElementInfoManagerInterface
+   */
+  protected $element_info_manager;
+
+  /**
    * {@inheritdoc}
    */
   protected function setUp() {
@@ -45,6 +53,7 @@ protected function setUp() {
         '#commands' => array(),
         '#error' => NULL,
       ]);
+    $this->element_info_manager = $element_info_manager;
     $this->ajaxRenderer = new TestAjaxRenderer($element_info_manager);
 
     $this->renderer = $this->getMockBuilder('Drupal\Core\Render\Renderer')
@@ -78,11 +87,58 @@ public function testRenderWithFragmentObject() {
     $this->assertEquals('status_messages', $commands[1]['data']);
   }
 
+  /**
+   * Tests that messages are not added to the commands array when we have an
+   * empty message.
+   *
+   * @covers ::renderResponse
+   */
+  public function testRenderWithFragmentObjectAndMessage() {
+    $main_content = ['#markup' => 'example content'];
+    $request = new Request();
+    $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
+
+    // This makes ['#type' => 'status_messages'] return just a new line.
+    $this->ajaxRenderer = new TestAjaxRenderer($this->element_info_manager, TRUE);
+    /** @var \Drupal\Core\Ajax\AjaxResponse $result */
+    $result = $this->ajaxRenderer->renderResponse($main_content, $request, $route_match);
+
+    $this->assertInstanceOf('Drupal\Core\Ajax\AjaxResponse', $result);
+
+    $commands = $result->getCommands();
+    $this->assertEquals('insert', $commands[0]['command']);
+    $this->assertEquals('example content', $commands[0]['data']);
+
+    // Make sure that status messages are not prepended if there are no
+    // messages.
+    $this->assertEquals(1, count($commands));
+  }
+
 }
 
 class TestAjaxRenderer extends AjaxRenderer {
 
   /**
+   * Boolean indicating if the messages will be empty or not.
+   *
+   * @var bool
+   */
+  protected $emptyMessages;
+
+  /**
+   * Constructs a new AjaxRenderer instance.
+   *
+   * @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info_manager
+   *   The element info manager.
+   * @param bool $emptyMessages
+   *   Boolean indicating if the messages will be empty or not.
+   */
+  public function __construct(ElementInfoManagerInterface $element_info_manager, $emptyMessages = FALSE) {
+    parent::__construct($element_info_manager);
+    $this->emptyMessages = $emptyMessages;
+  }
+
+  /**
    * {@inheritdoc}
    */
   protected function drupalRenderRoot(&$elements, $is_root_call = FALSE) {
@@ -91,6 +147,9 @@ protected function drupalRenderRoot(&$elements, $is_root_call = FALSE) {
       return $elements['#markup'];
     }
     elseif (isset($elements['#type'])) {
+      if ($this->emptyMessages) {
+        return "\n";
+      }
       return $elements['#type'];
     }
     else {
