Warning: Undefined array key media in CssOptimizer->processFile() after update from 1.19.0 to 1.20.0

Created on 4 January 2024, 11 months ago
Updated 1 February 2024, 10 months ago

Problem/Motivation

Warning: Undefined array key media in CssOptimizer->processFile() after update from 1.19.0 to 1.20.0.

Complete warning:

Message	Warning: Undefined array key "media" in Drupal\Core\Asset\CssOptimizer->processFile() (line 83 of /var/www/html/docroot/core/lib/Drupal/Core/Asset/CssOptimizer.php) #0 /var/www/html/docroot/core/includes/bootstrap.inc(164): _drupal_error_handler_real() #1 /var/www/html/docroot/core/lib/Drupal/Core/Asset/CssOptimizer.php(83): _drupal_error_handler() #2 /var/www/html/docroot/core/lib/Drupal/Core/Asset/CssOptimizer.php(49): Drupal\Core\Asset\CssOptimizer->processFile() #3 /var/www/html/docroot/modules/contrib/critical_css/src/Asset/CriticalCssProvider.php(186): Drupal\Core\Asset\CssOptimizer->optimize() #4 /var/www/html/docroot/modules/contrib/critical_css/src/Asset/CriticalCssProvider.php(386): Drupal\critical_css\Asset\CriticalCssProvider->getCriticalCss() #5 /var/www/html/docroot/modules/contrib/critical_css/src/Asset/CssCollectionRenderer.php(196): Drupal\critical_css\Asset\CriticalCssProvider->getMatchedFilePath() #6 /var/www/html/docroot/modules/contrib/critical_css/src/Asset/CssCollectionRenderer.php(118): Drupal\critical_css\Asset\CssCollectionRenderer->getDebugInfo() #7 /var/www/html/docroot/modules/contrib/critical_css/src/Asset/CssCollectionRenderer.php(82): Drupal\critical_css\Asset\CssCollectionRenderer->getCriticalCssAsset() #8 /var/www/html/docroot/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php(320): Drupal\critical_css\Asset\CssCollectionRenderer->render() #9 /var/www/html/docroot/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php(164): Drupal\Core\Render\HtmlResponseAttachmentsProcessor->processAssetLibraries() #10 /var/www/html/docroot/core/lib/Drupal/Core/EventSubscriber/HtmlResponseSubscriber.php(45): Drupal\Core\Render\HtmlResponseAttachmentsProcessor->processAttachments() #11 [internal function]: Drupal\Core\EventSubscriber\HtmlResponseSubscriber->onRespond() #12 /var/www/html/docroot/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func() #13 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(214): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() #14 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(202): Symfony\Component\HttpKernel\HttpKernel->filterResponse() #15 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw() #16 /var/www/html/docroot/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle() #17 /var/www/html/docroot/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle() #18 /var/www/html/vendor/asm89/stack-cors/src/Cors.php(53): Drupal\Core\StackMiddleware\KernelPreHandle->handle() #19 /var/www/html/docroot/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Asm89\Stack\Cors->handle() #20 /var/www/html/docroot/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() #21 /var/www/html/docroot/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() #22 /var/www/html/docroot/modules/contrib/http_headers_cleaner/src/Middleware/HttpHeadersCleanerMiddleware.php(55): Drupal\Core\StackMiddleware\AjaxPageState->handle() #23 /var/www/html/docroot/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\http_headers_cleaner\Middleware\HttpHeadersCleanerMiddleware->handle() #24 /var/www/html/docroot/core/lib/Drupal/Core/DrupalKernel.php(704): Drupal\Core\StackMiddleware\StackedHttpKernel->handle() #25 /var/www/html/docroot/index.php(19): Drupal\Core\DrupalKernel->handle() #26 @main.

Steps to reproduce

Create new node and visit it as anonymous.

Pending to debug to know more about the error.

The problem seems to come from \Drupal\critical_css\Asset\CriticalCssProvider::getCriticalCss, the media type is not defined.

        $this->criticalCss = $this->performanceConfig->get('css.preprocess') ?
          $this->optimizer->optimize([
            'type' => 'file',
            'preprocess' => TRUE,
            'data' => $filePath,
          ]) :
          trim(file_get_contents($filePath));

And the CSS Optimizer fails \Drupal\Core\Asset\CssOptimizer::processFile, expects $css_asset['media'] to be defined, but is not, si only defined, type, preprocess and data.

    if ($css_asset['media'] !== 'print' && $css_asset['media'] !== 'all') {
      $contents = '@media ' . $css_asset['media'] . '{' . $contents . '}' . "\n";
    }

The problem seems to come from commit https://git.drupalcode.org/project/critical_css/-/commit/daa8387908009fe... and issue https://www.drupal.org/i/3380253

Proposed solution

The media should be also be defined.

Example of config from core https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/lib/Drupal/...

    $default_options = [
      'type' => 'file',
      'group' => CSS_AGGREGATE_DEFAULT,
      'weight' => 0,
      'media' => 'all',
      'preprocess' => TRUE,
    ];
🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024