From 0833df8d62388c628dd000540a7658b56a9928f6 Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Thu, 28 Sep 2023 15:03:31 +1000 Subject: [PATCH] Work around Drupal 10.1 CSS/JS aggregation issues. --- .../src/EventSubscriber/QuantApi.php | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/modules/quant_api/src/EventSubscriber/QuantApi.php b/modules/quant_api/src/EventSubscriber/QuantApi.php index a305c5b7..d75f1d3e 100644 --- a/modules/quant_api/src/EventSubscriber/QuantApi.php +++ b/modules/quant_api/src/EventSubscriber/QuantApi.php @@ -108,6 +108,7 @@ public function onRedirect(QuantRedirectEvent $event) { */ public function onOutput(QuantEvent $event) { + $config = \Drupal::config('quant.settings'); $path = $event->getLocation(); $content = $event->getContents(); $meta = $event->getMetadata(); @@ -177,14 +178,46 @@ public function onOutput(QuantEvent $event) { $fileOnDisk = str_replace('/system/files', $privatePath, $file); } + // Check if the file has changed. if (isset($item['existing_md5'])) { if (file_exists($fileOnDisk) && md5_file($fileOnDisk) == $item['existing_md5']) { continue; } } - // If the file exists we send it directly to quant otherwise we add it - // to the queue to generate assets on the next run. + // In Drupal 10.1, work around CSS/JS aggregation issues. + // Only process internal CSS/JS files. + $check_file = preg_match('/\.(css|js)$/', $file) && (strpos($item['original_path'], "http") !== 0); + if (!file_exists($fileOnDisk) && $check_file) { + + // Do an HTTP request for the full file path to generate the file. + // The `original_path` has the necessary query parameters. + $local_server = $config->get('local_server') ?: 'http://localhost'; + $url = $local_server . $item['original_path']; + + // Set headers. + $headers['Host'] = $config->get('host_domain') ?: $_SERVER['SERVER_NAME']; + + // Handle basic authentication. Note, this will not work via Drush/CLI. + $auth = !empty($_SERVER['PHP_AUTH_USER']) ? [ + $_SERVER['PHP_AUTH_USER'], + $_SERVER['PHP_AUTH_PW'], + ] : []; + + $response = \Drupal::httpClient()->get($url, [ + 'http_errors' => FALSE, + 'headers' => $headers, + 'auth' => $auth, + 'allow_redirects' => FALSE, + 'verify' => boolval($config->get('ssl_cert_verify')), + ]); + if ($response->getStatusCode() != 200) { + $this->logger->error("Error retrieving file for route: $url"); + } + } + + // If the file exists, send it directly to Quant; otherwise, add it to the + // queue to generate assets on the next run. if (file_exists($fileOnDisk)) { $this->eventDispatcher->dispatch(new QuantFileEvent($fileOnDisk, $item['full_path'] ?? $file), QuantFileEvent::OUTPUT); } @@ -204,7 +237,7 @@ public function onOutput(QuantEvent $event) { $xpath = new \DOMXPath($document); $xpath_selectors = []; - $links_config = \Drupal::config('quant.settings')->get('xpath_selectors'); + $links_config = $config->get('xpath_selectors'); foreach (explode(PHP_EOL, $links_config) as $links_line) { $xpath_selectors[] = trim($links_line);