diff --git a/core/lib/Drupal/Core/Asset/AssetResolver.php b/core/lib/Drupal/Core/Asset/AssetResolver.php
index e434511..2c483c0 100644
--- a/core/lib/Drupal/Core/Asset/AssetResolver.php
+++ b/core/lib/Drupal/Core/Asset/AssetResolver.php
@@ -221,7 +221,7 @@ public function getJsAssets(AttachedAssetsInterface $assets, $optimize) {
     // Add the theme name to the cache key since themes may implement
     // hook_js_alter(). Additionally add the current language to support
     // translation of JavaScript files.
-    $cid = 'js:' . $theme_info->getName() . ':' . $this->languageManager->getCurrentLanguage()->getId() . ':' .  Crypt::hashBase64(serialize($assets));
+    $cid = 'js:' . $theme_info->getName() . ':' . $this->languageManager->getCurrentLanguage()->getId() . ':' .  Crypt::hashBase64(serialize($assets)) . (int) $optimize;
 
     if ($cached = $this->cache->get($cid)) {
       list($js_assets_header, $js_assets_footer, $settings, $settings_in_header) = $cached->data;
diff --git a/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
index 50ff584..26fef4e 100644
--- a/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
+++ b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
@@ -164,7 +164,7 @@ protected function processAssetLibraries(array $attached, array $placeholders) {
     // Print scripts - if any are present.
     if (isset($placeholders['scripts']) || isset($placeholders['scripts_bottom'])) {
       // Optimize JS if necessary, but only during normal site operation.
-      $optimize_js = !defined('MAINTENANCE_MODE') && $this->config->get('js.preprocess');
+      $optimize_js = !defined('MAINTENANCE_MODE') && !\Drupal::state()->get('system.maintenance_mode') && $this->config->get('js.preprocess');
       list($js_assets_header, $js_assets_footer) = $this->assetResolver->getJsAssets($assets, $optimize_js);
       $variables['scripts'] = $this->jsCollectionRenderer->render($js_assets_header);
       $variables['scripts_bottom'] = $this->jsCollectionRenderer->render($js_assets_footer);
diff --git a/core/modules/system/src/Controller/DbUpdateController.php b/core/modules/system/src/Controller/DbUpdateController.php
index f6b1227..c2e195e 100644
--- a/core/modules/system/src/Controller/DbUpdateController.php
+++ b/core/modules/system/src/Controller/DbUpdateController.php
@@ -544,13 +544,13 @@ protected function updateTasksList($active = NULL) {
    *   The current request object.
    */
   protected function triggerBatch(Request $request) {
-    // During the update, bring the site offline so that schema changes do not
-    // affect visiting users.
-    $maintenance_mode = $this->config('system.maintenance')->get('enabled');
-    if (isset($maintenance_mode)) {
-      $_SESSION['maintenance_mode'] = $maintenance_mode;
-    }
-    if (empty($_SESSION['maintenance_mode'])) {
+    $maintenance_mode = $this->state->get('system.maintenance_mode', FALSE);
+    // Store the current maintenance mode status in the session so that it can
+    // be restored at the end of the batch.
+    $_SESSION['maintenance_mode'] = $maintenance_mode;
+    // During the update, always put the site into maintenance mode so that
+    // in-progress schema changes do not affect visiting users.
+    if (empty($maintenance_mode)) {
       $this->state->set('system.maintenance_mode', TRUE);
     }
 
@@ -630,11 +630,11 @@ public static function batchFinished($success, $results, $operations) {
     $_SESSION['updates_remaining'] = $operations;
 
     // Now that the update is done, we can put the site back online if it was
-    // previously in maintenance mode.
-    if (isset($_SESSION['maintenance_mode'])) {
+    // previously not in maintenance mode.
+    if (empty($_SESSION['maintenance_mode'])) {
       \Drupal::state()->set('system.maintenance_mode', FALSE);
-      unset($_SESSION['maintenance_mode']);
     }
+    unset($_SESSION['maintenance_mode']);
   }
 
   /**
diff --git a/core/modules/system/src/Tests/System/SiteMaintenanceTest.php b/core/modules/system/src/Tests/System/SiteMaintenanceTest.php
index 2aba280..54ab8b7 100644
--- a/core/modules/system/src/Tests/System/SiteMaintenanceTest.php
+++ b/core/modules/system/src/Tests/System/SiteMaintenanceTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\system\Tests\System;
 
+use Drupal\Core\Url;
 use Drupal\simpletest\WebTestBase;
 
 /**
@@ -30,6 +31,7 @@ protected function setUp() {
 
     // Configure 'node' as front page.
     $this->config('system.site')->set('page.front', '/node')->save();
+    $this->config('system.performance')->set('js.preprocess', 1)->save();
 
     // Create a user allowed to access site in maintenance mode.
     $this->user = $this->drupalCreateUser(array('access site in maintenance mode'));
@@ -42,6 +44,10 @@ protected function setUp() {
    * Verify site maintenance mode functionality.
    */
   protected function testSiteMaintenance() {
+    $this->drupalGet(Url::fromRoute('user.page'));
+    // JS should be aggregated, so drupal.js is not in the page source.
+    $links = $this->xpath('//script[contains(@src, :href)]', array(':href' => '/core/misc/drupal.js'));
+    $this->assertFalse(isset($links[0]), 'script /core/misc/drupal.js not in page');
     // Turn on maintenance mode.
     $edit = array(
       'maintenance_mode' => 1,
@@ -52,7 +58,10 @@ protected function testSiteMaintenance() {
     $user_message = t('Operating in maintenance mode.');
     $offline_message = t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => $this->config('system.site')->get('name')));
 
-    $this->drupalGet('');
+    $this->drupalGet(Url::fromRoute('user.page'));
+    // JS should not be aggregated, so drupal.js is expected in the page source.
+    $links = $this->xpath('//script[contains(@src, :href)]', array(':href' => '/core/misc/drupal.js'));
+    $this->assertTrue(isset($links[0]), 'script /core/misc/drupal.js in page');
     $this->assertRaw($admin_message, 'Found the site maintenance mode message.');
 
     // Logout and verify that offline message is displayed.
diff --git a/core/modules/system/src/Tests/Update/UpdateScriptTest.php b/core/modules/system/src/Tests/Update/UpdateScriptTest.php
index 9951650..d3e9d5e 100644
--- a/core/modules/system/src/Tests/Update/UpdateScriptTest.php
+++ b/core/modules/system/src/Tests/Update/UpdateScriptTest.php
@@ -173,6 +173,55 @@ function testNoUpdateFunctionality() {
    * Tests update.php after performing a successful update.
    */
   function testSuccessfulUpdateFunctionality() {
+    $initial_maintenance_mode = $this->container->get('state')->get('system.maintenance_mode');
+    $this->assertFalse($initial_maintenance_mode, 'Site is not in maintenance mode.');
+    $this->updateScriptTest($initial_maintenance_mode);
+    $final_maintenance_mode = $this->container->get('state')->get('system.maintenance_mode');
+    $this->assertEqual($final_maintenance_mode, $initial_maintenance_mode, 'Maintenance mode should not have changed after database updates.');
+
+    // Reset the static cache to ensure we have the most current setting.
+    $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
+    $this->assertEqual($schema_version, 8001, 'update_script_test schema version is 8001 after updating.');
+
+    // Set the installed schema version to one less than the current update.
+    drupal_set_installed_schema_version('update_script_test', $schema_version - 1);
+    $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
+    $this->assertEqual($schema_version, 8000, 'update_script_test schema version overridden to 8000.');
+
+    // Click through update.php with 'access administration pages' and
+    // 'access site reports' permissions.
+    $admin_user = $this->drupalCreateUser(array('administer software updates', 'access administration pages', 'access site reports', 'access site in maintenance mode'));
+    $this->drupalLogin($admin_user);
+    $this->drupalGet($this->updateUrl, array('external' => TRUE));
+    $this->clickLink(t('Continue'));
+    $this->clickLink(t('Apply pending updates'));
+    $this->assertText('Updates were attempted.');
+    $this->assertLink('logged');
+    $this->assertLink('Administration pages');
+    $this->assertNoLinkByHref('update.php', 1);
+    $this->clickLink('Administration pages');
+    $this->assertResponse(200);
+  }
+
+  /**
+   * Tests update.php while in maintenance mode.
+   */
+  function testMaintenanceModelUpdateFunctionality() {
+    $this->container->get('state')
+      ->set('system.maintenance_mode', TRUE);
+    $initial_maintenance_mode = $this->container->get('state')
+      ->get('system.maintenance_mode');
+    $this->assertTrue($initial_maintenance_mode, 'Site is in maintenance mode.');
+    $this->updateScriptTest($initial_maintenance_mode);
+    $final_maintenance_mode = $this->container->get('state')
+      ->get('system.maintenance_mode');
+    $this->assertEqual($final_maintenance_mode, $initial_maintenance_mode, 'Maintenance mode should not have changed after database updates.');
+  }
+
+  /**
+   * Helper function to run updates via the browser.
+   */
+  protected function updateScriptTest($maintenance_mode) {
     $schema_version = drupal_get_installed_schema_version('update_script_test');
     $this->assertEqual($schema_version, 8001, 'update_script_test is initially installed with schema version 8001.');
 
@@ -183,6 +232,12 @@ function testSuccessfulUpdateFunctionality() {
 
     // Click through update.php with 'administer software updates' permission.
     $this->drupalLogin($this->updateUser);
+    if ($maintenance_mode) {
+      $this->assertText('Operating in maintenance mode.');
+    }
+    else {
+      $this->assertNoText('Operating in maintenance mode.');
+    }
     $this->drupalGet($this->updateUrl, array('external' => TRUE));
     $this->clickLink(t('Continue'));
     $this->clickLink(t('Apply pending updates'));
@@ -204,29 +259,6 @@ function testSuccessfulUpdateFunctionality() {
     // Verify the front page can be visited following the upgrade.
     $this->clickLink('Front page');
     $this->assertResponse(200);
-
-    // Reset the static cache to ensure we have the most current setting.
-    $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
-    $this->assertEqual($schema_version, 8001, 'update_script_test schema version is 8001 after updating.');
-
-    // Set the installed schema version to one less than the current update.
-    drupal_set_installed_schema_version('update_script_test', $schema_version - 1);
-    $schema_version = drupal_get_installed_schema_version('update_script_test', TRUE);
-    $this->assertEqual($schema_version, 8000, 'update_script_test schema version overridden to 8000.');
-
-    // Click through update.php with 'access administration pages' and
-    // 'access site reports' permissions.
-    $admin_user = $this->drupalCreateUser(array('administer software updates', 'access administration pages', 'access site reports', 'access site in maintenance mode'));
-    $this->drupalLogin($admin_user);
-    $this->drupalGet($this->updateUrl, array('external' => TRUE));
-    $this->clickLink(t('Continue'));
-    $this->clickLink(t('Apply pending updates'));
-    $this->assertText('Updates were attempted.');
-    $this->assertLink('logged');
-    $this->assertLink('Administration pages');
-    $this->assertNoLinkByHref('update.php', 1);
-    $this->clickLink('Administration pages');
-    $this->assertResponse(200);
   }
 
   /**
