If you're here to provide feedback: please read the issue summary and then jump to #49!
Problem/Motivation
- This is a blocker for #2368797: [PP-2] Optimize ajaxPageState to keep Drupal 8 sites fast on high-latency networks. There, we want to make Drupal sites as fast as possible, and the changes there especially have a big effect on high-latency (mobile) connections. We want to do that by only using libraries for "AJAX page state", which allows us to send far less metadata. But if we want to do that, we should no longer allow attaching individual CSS and JS assets (that's what #2382533: [PP-1] Attach assets only via the asset library system is for). But if we don't allow them, then we can no longer attach JS settings, because we currently consider JS settings as regular JS assets.
- Also, the DX of adding JS settings is absolutely terrible, awful, fragile. You must add a JS asset with a very specific array structure that is extremely easy to get wrong. (Wim Leers has personally run into this problem dozens and dozens of times in the >8 years he uses Drupal.)
Proposed resolution
We want to do that by only using libraries for "AJAX page state", which allows us to send far less metadata. But if we want to do that, we should no longer allow attaching individual CSS and JS assets (that's what #2382533: [PP-1] Attach assets only via the asset library system is for). But if we don't allow them, then we can no longer attach JS settings, because we currently consider JS settings as regular JS assets.
Hence this issue proposes a new asset type to deal with that, and at the same time vastly improve the DX of attaching JS assets:
- Before
$build = array(
…,
'#attached' => array(
'js' => array(
array(
'type' => 'setting',
'data' => array('ckeditor' => array(
'hiddenCKEditorConfig' => $config,
)),
),
),
),
),
);- After
$build = [
…,
'#attached' => [
'drupalSettings' => [
'ckeditor' => ['hiddenCKEditorConfig' => $config],
]
],
];
Remaining tasks
Review.
User interface changes
None.
API changes
- Vastly improved DX for attaching JS settings: no longer
$build['#attached']['js'][] = ['data' => ['mysetting' => 'foo'], 'type' => 'setting']
, but$build['#attached']['drupalSettings']['mysetting'] = 'foo';
, hence matching how JS settings are used on the front-end. - Accompanying asset type alter hook:
hook_js_settings_alter()
. - Removes
drupal_merge_js_settings()
.
Beta phase evaluation
Issue priority | Critical because it blocks #2368797: [PP-2] Optimize ajaxPageState to keep Drupal 8 sites fast on high-latency networks. |
---|---|
Prioritized changes | The main goal of this issue is performance (in the blocked issue), but at the same time vastly improving the DX. The principal JS maintainer, a core committer, Fabianx, myself, and hence surely many others, still struggle after many years with adding JS settings. The current DX is *that* atrocious. |
Disruption | Disruptive for contributed and custom modules/themes because it requires JS settings to be attached in a different way: a much simpler way. The long-term benefits are considered to outweigh the API change this late in the game. See #27 and #28. |