Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around Drupal 10.1 CSS/JS aggregation issues. #190

Merged
merged 4 commits into from
Sep 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions modules/quant_api/src/EventSubscriber/QuantApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -177,14 +178,40 @@ 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 = (str_ends_with($file, 'css') || str_ends_with($file, 'js')) && !str_starts_with($item['original_path'], 'http');
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 the headers.
$headers['Host'] = $config->get('host_domain') ?: $_SERVER['SERVER_NAME'];

// If using basic auth, the credentials must already be in the host.
$response = \Drupal::httpClient()->get($url, [
'http_errors' => FALSE,
'headers' => $headers,
'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);
}
Expand All @@ -204,7 +231,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);
Expand Down
Loading