diff --git a/core/includes/common.inc b/core/includes/common.inc index 08ce2abbc08..353a9b51d8c 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -3518,7 +3518,13 @@ function drupal_build_css_cache($css) { $data = ''; $uri = ''; $map = variable_get('drupal_css_cache_files', array()); - $key = hash('sha256', serialize($css)); + // Create a new array so that only the file names are used to create the hash. + // This prevents new aggregates from being created unnecessarily. + $css_data = array(); + foreach ($css as $css_file) { + $css_data[] = $css_file['data']; + } + $key = hash('sha256', serialize($css_data)); if (isset($map[$key])) { $uri = $map[$key]; } @@ -5020,7 +5026,13 @@ function drupal_build_js_cache($files) { $contents = ''; $uri = ''; $map = variable_get('drupal_js_cache_files', array()); - $key = hash('sha256', serialize($files)); + // Create a new array so that only the file names are used to create the hash. + // This prevents new aggregates from being created unnecessarily. + $js_data = array(); + foreach ($files as $file) { + $js_data[] = $file['data']; + } + $key = hash('sha256', serialize($js_data)); if (isset($map[$key])) { $uri = $map[$key]; } diff --git a/core/modules/system/tests/common.test b/core/modules/system/tests/common.test index 0380b6ebb44..f0ac957ef22 100644 --- a/core/modules/system/tests/common.test +++ b/core/modules/system/tests/common.test @@ -1423,6 +1423,48 @@ class CommonJavaScriptTestCase extends DrupalWebTestCase { $this->assertTrue(strpos($javascript, $expected) > 0, t('JavaScript is aggregated in the expected groups and order.')); } + /** + * Tests JavaScript aggregation when files are added to a different scope. + */ + function testAggregationOrder() { + // Enable JavaScript aggregation. + config('system.performance')->set('preprocess_js', 1)->save(); + drupal_static_reset('drupal_add_js'); + + // Add two JavaScript files to the current request and build the cache. + drupal_add_js('core/misc/ajax.js'); + drupal_add_js('core/misc/autocomplete.js'); + + $js_items = drupal_add_js(); + drupal_build_js_cache(array( + 'core/misc/ajax.js' => $js_items['core/misc/ajax.js'], + 'core/misc/autocomplete.js' => $js_items['core/misc/autocomplete.js'] + )); + + // Store the expected key for the first item in the cache. + $cache = array_keys(variable_get('drupal_js_cache_files', array())); + $expected_key = $cache[0]; + + // Reset variables and add a file in a different scope first. + variable_del('drupal_js_cache_files'); + drupal_static_reset('drupal_add_js'); + drupal_add_js('some/custom/javascript_file.js', array('scope' => 'footer')); + drupal_add_js('core/misc/ajax.js'); + drupal_add_js('core/misc/autocomplete.js'); + + // Rebuild the cache. + $js_items = drupal_add_js(); + drupal_build_js_cache(array( + 'core/misc/ajax.js' => $js_items['core/misc/ajax.js'], + 'core/misc/autocomplete.js' => $js_items['core/misc/autocomplete.js'] + )); + + // Compare the expected key for the first file to the current one. + $cache = array_keys(variable_get('drupal_js_cache_files', array())); + $key = $cache[0]; + $this->assertEqual($key, $expected_key, 'JavaScript aggregation is not affected by ordering in different scopes.'); + } + /** * Test JavaScript ordering. */