From d2392d1361f239fab6418cded97c12bd05067d1c Mon Sep 17 00:00:00 2001
From: Lauri Eskola <lauri.eskola@acquia.com>
Date: Tue, 11 Jul 2023 12:03:11 +0300
Subject: [PATCH] Issue #3373328 by catch, keshav.k, Ambient.Impact, longwave:
 ^10.1 CSS aggregation breaks during maintenance mode

(cherry picked from commit b63275fe66f8c0ff0dc9d9ed25abaee0030b76f2)
---
 .../Core/Ajax/AjaxResponseAttachmentsProcessor.php    |  5 +++--
 .../Core/Render/HtmlResponseAttachmentsProcessor.php  |  6 ++++--
 .../src/Functional/System/SiteMaintenanceTest.php     | 11 ++++++++---
 3 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php b/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php
index d7a72b430fbb..8af3dc6d5cfb 100644
--- a/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php
+++ b/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php
@@ -133,10 +133,11 @@ public function processAttachments(AttachmentsInterface $response) {
    */
   protected function buildAttachmentsCommands(AjaxResponse $response, Request $request) {
     $ajax_page_state = $request->get('ajax_page_state');
+    $maintenance_mode = defined('MAINTENANCE_MODE') || \Drupal::state()->get('system.maintenance_mode');
 
     // Aggregate CSS/JS if necessary, but only during normal site operation.
-    $optimize_css = !defined('MAINTENANCE_MODE') && $this->config->get('css.preprocess');
-    $optimize_js = !defined('MAINTENANCE_MODE') && $this->config->get('js.preprocess');
+    $optimize_css = !$maintenance_mode && $this->config->get('css.preprocess');
+    $optimize_js = $maintenance_mode && $this->config->get('js.preprocess');
 
     $attachments = $response->getAttachments();
 
diff --git a/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
index 917648103132..de2ab4ef4b7d 100644
--- a/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
+++ b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
@@ -311,17 +311,19 @@ protected function renderPlaceholders(HtmlResponse $response) {
   protected function processAssetLibraries(AttachedAssetsInterface $assets, array $placeholders) {
     $variables = [];
 
+    $maintenance_mode = defined('MAINTENANCE_MODE') || \Drupal::state()->get('system.maintenance_mode');
+
     // Print styles - if present.
     if (isset($placeholders['styles'])) {
       // Optimize CSS if necessary, but only during normal site operation.
-      $optimize_css = !defined('MAINTENANCE_MODE') && $this->config->get('css.preprocess');
+      $optimize_css = !$maintenance_mode && $this->config->get('css.preprocess');
       $variables['styles'] = $this->cssCollectionRenderer->render($this->assetResolver->getCssAssets($assets, $optimize_css, $this->languageManager->getCurrentLanguage()));
     }
 
     // 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') && !\Drupal::state()->get('system.maintenance_mode') && $this->config->get('js.preprocess');
+      $optimize_js = !$maintenance_mode && $this->config->get('js.preprocess');
       [$js_assets_header, $js_assets_footer] = $this->assetResolver->getJsAssets($assets, $optimize_js, $this->languageManager->getCurrentLanguage());
       $variables['scripts'] = $this->jsCollectionRenderer->render($js_assets_header);
       $variables['scripts_bottom'] = $this->jsCollectionRenderer->render($js_assets_footer);
diff --git a/core/modules/system/tests/src/Functional/System/SiteMaintenanceTest.php b/core/modules/system/tests/src/Functional/System/SiteMaintenanceTest.php
index def93ffec18c..8de91e27dbd9 100644
--- a/core/modules/system/tests/src/Functional/System/SiteMaintenanceTest.php
+++ b/core/modules/system/tests/src/Functional/System/SiteMaintenanceTest.php
@@ -47,7 +47,10 @@ protected function setUp(): void {
 
     // Configure 'node' as front page.
     $this->config('system.site')->set('page.front', '/node')->save();
-    $this->config('system.performance')->set('js.preprocess', 1)->save();
+    $this->config('system.performance')
+      ->set('js.preprocess', 1)
+      ->set('css.preprocess', 1)
+      ->save();
 
     // Create a user allowed to access site in maintenance mode.
     $this->user = $this->drupalCreateUser(['access site in maintenance mode']);
@@ -73,8 +76,9 @@ public function testSiteMaintenance() {
     $this->assertSession()->linkByHrefExists(Url::fromRoute('user.login')->toString());
 
     $this->drupalGet(Url::fromRoute('user.page'));
-    // JS should be aggregated, so drupal.js is not in the page source.
+    // Aggregation should be enabled, individual assets should not be rendered.
     $this->assertSession()->elementNotExists('xpath', '//script[contains(@src, "/core/misc/drupal.js")]');
+    $this->assertSession()->elementNotExists('xpath', '//link[contains(@href, "/core/modules/system/css/components/align.module.css")]');
     // Turn on maintenance mode.
     $edit = [
       'maintenance_mode' => 1,
@@ -87,8 +91,9 @@ public function testSiteMaintenance() {
     $offline_message = $this->config('system.site')->get('name') . ' is currently under maintenance. We should be back shortly. Thank you for your patience.';
 
     $this->drupalGet(Url::fromRoute('user.page'));
-    // JS should not be aggregated, so drupal.js is expected in the page source.
+    // Aggregation should be disabled, individual assets should be rendered.
     $this->assertSession()->elementExists('xpath', '//script[contains(@src, "/core/misc/drupal.js")]');
+    $this->assertSession()->elementExists('xpath', '//link[contains(@href, "/core/modules/system/css/components/align.module.css")]');
     $this->assertSession()->pageTextContains($admin_message);
     $this->assertSession()->linkExists('Go online.');
     $this->assertSession()->linkByHrefExists(Url::fromRoute('system.site_maintenance_mode')->toString());
-- 
GitLab