Issue #3437839 by catch, Luke.Leber, thejimbirch: Only send libraries with aggregate URLs that have the aggregate type included

merge-requests/7020/head^2
Alex Pott 2024-04-23 11:05:25 +01:00
parent 79eed4acc5
commit 22707d5cd0
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
2 changed files with 49 additions and 41 deletions

View File

@ -143,27 +143,30 @@ class AssetResolver implements AssetResolverInterface {
'preprocess' => TRUE, 'preprocess' => TRUE,
]; ];
foreach ($libraries_to_load as $library) { foreach ($libraries_to_load as $key => $library) {
[$extension, $name] = explode('/', $library, 2); [$extension, $name] = explode('/', $library, 2);
$definition = $this->libraryDiscovery->getLibraryByName($extension, $name); $definition = $this->libraryDiscovery->getLibraryByName($extension, $name);
if (isset($definition['css'])) { if (empty($definition['css'])) {
foreach ($definition['css'] as $options) { unset($libraries_to_load[$key]);
$options += $default_options; continue;
// Copy the asset library license information to each file. }
$options['license'] = $definition['license'];
// Files with a query string cannot be preprocessed. foreach ($definition['css'] as $options) {
if ($options['type'] === 'file' && $options['preprocess'] && str_contains($options['data'], '?')) { $options += $default_options;
$options['preprocess'] = FALSE; // Copy the asset library license information to each file.
} $options['license'] = $definition['license'];
// Always add a tiny value to the weight, to conserve the insertion // Files with a query string cannot be preprocessed.
// order. if ($options['type'] === 'file' && $options['preprocess'] && str_contains($options['data'], '?')) {
$options['weight'] += count($css) / 30000; $options['preprocess'] = FALSE;
// CSS files are being keyed by the full path.
$css[$options['data']] = $options;
} }
// Always add a tiny value to the weight, to conserve the insertion
// order.
$options['weight'] += count($css) / 30000;
// CSS files are being keyed by the full path.
$css[$options['data']] = $options;
} }
} }
@ -176,7 +179,7 @@ class AssetResolver implements AssetResolverInterface {
uasort($css, [static::class, 'sort']); uasort($css, [static::class, 'sort']);
if ($optimize) { if ($optimize) {
$css = \Drupal::service('asset.css.collection_optimizer')->optimize($css, $libraries_to_load, $language); $css = \Drupal::service('asset.css.collection_optimizer')->optimize($css, array_values($libraries_to_load), $language);
} }
} }
$this->cache->set($cid, $css, CacheBackendInterface::CACHE_PERMANENT, ['library_info']); $this->cache->set($cid, $css, CacheBackendInterface::CACHE_PERMANENT, ['library_info']);
@ -240,14 +243,21 @@ class AssetResolver implements AssetResolverInterface {
]; ];
// Collect all libraries that contain JS assets and are in the header. // Collect all libraries that contain JS assets and are in the header.
// Also remove any libraries with no JavaScript from the libraries to
// load.
$header_js_libraries = []; $header_js_libraries = [];
foreach ($libraries_to_load as $library) { foreach ($libraries_to_load as $key => $library) {
[$extension, $name] = explode('/', $library, 2); [$extension, $name] = explode('/', $library, 2);
$definition = $this->libraryDiscovery->getLibraryByName($extension, $name); $definition = $this->libraryDiscovery->getLibraryByName($extension, $name);
if (isset($definition['js']) && !empty($definition['header'])) { if (empty($definition['js'])) {
unset($libraries_to_load[$key]);
continue;
}
if (!empty($definition['header'])) {
$header_js_libraries[] = $library; $header_js_libraries[] = $library;
} }
} }
$libraries_to_load = array_values($libraries_to_load);
// The current list of header JS libraries are only those libraries that // The current list of header JS libraries are only those libraries that
// are in the header, but their dependencies must also be loaded for them // are in the header, but their dependencies must also be loaded for them
// to function correctly, so update the list with those. // to function correctly, so update the list with those.
@ -256,28 +266,26 @@ class AssetResolver implements AssetResolverInterface {
foreach ($libraries_to_load as $library) { foreach ($libraries_to_load as $library) {
[$extension, $name] = explode('/', $library, 2); [$extension, $name] = explode('/', $library, 2);
$definition = $this->libraryDiscovery->getLibraryByName($extension, $name); $definition = $this->libraryDiscovery->getLibraryByName($extension, $name);
if (isset($definition['js'])) { foreach ($definition['js'] as $options) {
foreach ($definition['js'] as $options) { $options += $default_options;
$options += $default_options; // Copy the asset library license information to each file.
// Copy the asset library license information to each file. $options['license'] = $definition['license'];
$options['license'] = $definition['license'];
// 'scope' is a calculated option, based on which libraries are // 'scope' is a calculated option, based on which libraries are
// marked to be loaded from the header (see above). // marked to be loaded from the header (see above).
$options['scope'] = in_array($library, $header_js_libraries) ? 'header' : 'footer'; $options['scope'] = in_array($library, $header_js_libraries) ? 'header' : 'footer';
// Preprocess can only be set if caching is enabled and no // Preprocess can only be set if caching is enabled and no
// attributes are set. // attributes are set.
$options['preprocess'] = $options['cache'] && empty($options['attributes']) ? $options['preprocess'] : FALSE; $options['preprocess'] = $options['cache'] && empty($options['attributes']) ? $options['preprocess'] : FALSE;
// Always add a tiny value to the weight, to conserve the insertion // Always add a tiny value to the weight, to conserve the insertion
// order. // order.
$options['weight'] += count($javascript) / 30000; $options['weight'] += count($javascript) / 30000;
// Local and external files must keep their name as the associative // Local and external files must keep their name as the associative
// key so the same JavaScript file is not added twice. // key so the same JavaScript file is not added twice.
$javascript[$options['data']] = $options; $javascript[$options['data']] = $options;
}
} }
} }

View File

@ -26,8 +26,8 @@ class AssetAggregationAcrossPagesTest extends PerformanceTestBase {
$performance_data = $this->doRequests(); $performance_data = $this->doRequests();
$this->assertSame(4, $performance_data->getStylesheetCount()); $this->assertSame(4, $performance_data->getStylesheetCount());
$this->assertLessThan(82500, $performance_data->getStylesheetBytes()); $this->assertLessThan(82500, $performance_data->getStylesheetBytes());
$this->assertSame(2, $performance_data->getScriptCount()); $this->assertSame(1, $performance_data->getScriptCount());
$this->assertLessThan(14500, $performance_data->getScriptBytes()); $this->assertLessThan(7500, $performance_data->getScriptBytes());
} }
/** /**
@ -40,8 +40,8 @@ class AssetAggregationAcrossPagesTest extends PerformanceTestBase {
$performance_data = $this->doRequests(); $performance_data = $this->doRequests();
$this->assertSame(4, $performance_data->getStylesheetCount()); $this->assertSame(4, $performance_data->getStylesheetCount());
$this->assertLessThan(89500, $performance_data->getStylesheetBytes()); $this->assertLessThan(89500, $performance_data->getStylesheetBytes());
$this->assertSame(2, $performance_data->getScriptCount()); $this->assertSame(1, $performance_data->getScriptCount());
$this->assertLessThan(250000, $performance_data->getScriptBytes()); $this->assertLessThan(125500, $performance_data->getScriptBytes());
} }
/** /**