Skip to content
Snippets Groups Projects
Commit aa0ab207 authored by ambient.impact's avatar ambient.impact
Browse files

Issue #3414538: Enabled aggregation for our own JS if core patched:

See core issue #3232810 for explicit JS aggregation group patch.
parent 947b0044
No related branches found
No related tags found
1 merge request!9Issue #3414538: Turbo: Implement additive JavaScript aggregation to prevent multiple evaluation
...@@ -3,25 +3,24 @@ refreshless: ...@@ -3,25 +3,24 @@ refreshless:
theme: theme:
css/turbo.css: {} css/turbo.css: {}
js: js:
js/drupal_settings.js: { attributes: { defer: true }, preprocess: false } # The explicit aggregation group should not be changed or removed as it's
js/behaviours.js: { attributes: { defer: true }, preprocess: false } # used to group our JavaScript into a separate aggregate if/when core
js/scroll.js: { attributes: { defer: true }, preprocess: false } # supports it; if we detect that core supports this, the preprocess: false
js/stylesheet_sorter.js: { attributes: { defer: true }, preprocess: false } # is changed to preprocess: true; if core does not support aggregation
js/refreshless.js: { # groups, the preprocess: false set here is left as-is to prevent our
attributes: { # JavaScript potentially being downloaded and evaluated more than once since
defer: true, # aggregation is not additive by default.
# Causes Turbo to do a full page load if the query string for this #
# changes, e.g. on a cache clear. # @see https://www.drupal.org/project/drupal/issues/3232810
# # Core issue with a patch to enable setting explicit aggregation groups.
# @todo Should this be configurable? #
data-turbo-track: 'reload', # @see https://www.drupal.org/project/refreshless/issues/3414538
}, # RefreshLess issue implementing JavaScript aggregation support.
# This should be excluded from aggregation for now. js/drupal_settings.js: { attributes: { defer: true }, group: refreshless-turbo, preprocess: false }
# js/behaviours.js: { attributes: { defer: true }, group: refreshless-turbo, preprocess: false }
# @see https://www.drupal.org/project/refreshless/issues/3414538 js/scroll.js: { attributes: { defer: true }, group: refreshless-turbo, preprocess: false }
# JavaScript aggregation issue. js/stylesheet_sorter.js: { attributes: { defer: true }, group: refreshless-turbo, preprocess: false }
preprocess: false, js/refreshless.js: { attributes: { defer: true }, group: refreshless-turbo, preprocess: false }
}
header: true header: true
dependencies: dependencies:
- core/drupal - core/drupal
...@@ -81,6 +80,12 @@ js-cookie: ...@@ -81,6 +80,12 @@ js-cookie:
turbo: turbo:
version: 8.0.10 version: 8.0.10
js: js:
# Note that at the time of writing, Turbo does not cope well with being
# aggregated (and/or minified?), so the lack of a group here is intentional
# to prevent our hook picking this up and setting preprocess: true if core
# supports setting explicit aggregation groups.
#
# @see https://www.drupal.org/project/refreshless/issues/3414538
vendor/@hotwired/turbo/dist/turbo.es2017-umd.js: { attributes: { defer: true }, preprocess: false } vendor/@hotwired/turbo/dist/turbo.es2017-umd.js: { attributes: { defer: true }, preprocess: false }
header: true header: true
remote: https://turbo.hotwired.dev/ remote: https://turbo.hotwired.dev/
......
...@@ -76,6 +76,40 @@ class Javascript implements ContainerInjectionInterface { ...@@ -76,6 +76,40 @@ class Javascript implements ContainerInjectionInterface {
} }
#[Alter('js')]
/**
* Enable aggregation for our JavaScript if core supports naming groups.
*
* If core does not support named aggregation groups, the 'group' value will
* never be our string but a numeric value that core sets; if it's the numeric
* value, we don't do anything and allow the preprocess: false in our library
* definition to be used as a fallback.
*
* Note that \hook_library_info_alter() seems to be too early to detect this
* as it'll always have our string group; core seems to override the group
* value sometime between invoking that hook and \hook_js_alter().
*
* @see \hook_js_alter()
*/
public function refreshLessTurboGroupPreprocess(
array &$javascript,
AttachedAssetsInterface $assets, LanguageInterface $language,
): void {
foreach ($javascript as $path => &$values) {
if ($values['group'] !== 'refreshless-turbo') {
continue;
}
// Named aggregation groups are supported, so enable aggregation for all
// files in our group.
$values['preprocess'] = true;
}
}
#[Hook('js_settings_build')] #[Hook('js_settings_build')]
public function outputPageState( public function outputPageState(
array &$settings, AttachedAssetsInterface $assets, array &$settings, AttachedAssetsInterface $assets,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment