Problem/Motivation

While discussing #3202664: CKEditor 5's UI language should match Drupal's interface translation with @Reinmar, @lauriii and I brought up the subject of front-end performance.

In Drupal 8's CKEditor 4, we had a single massive JS file of roughly half a megabyte. A few additional JS files would be loaded by certain plugins, at runtime.

In CKEditor 5, we're loading many dozens of small JS files. This is already a net regression in terms of front-end performance. #3202664: CKEditor 5's UI language should match Drupal's interface translation will make that worse.

The obvious solution is concatenating the many individual JS files. This was not possible for CKE4 due to CKE4's architecture. @Reinmar confirms that in CKE5, this actually is possible! 🥳

Steps to reproduce

Observe how CKE 4 vs CKE 5 are loaded in your browser console's network tab.

Proposed resolution

Use #3565258: Support library-specific aggregates to put ckeditor5 libraries into self-contained aggregates.

  1. Measure performance on 0 ms network latency, 100 ms, 200 ms, 500 ms, 1000 ms.
  2. For every unique configuration of CKEditor 5 (i.e. for every "text editor" config entity), concatenate all relevant JS files and write them to disk. Load this generated JS file instead of all the individual JS files.
  3. Repeat the performance tests.
  4. Decide.

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#14 3224261-14.patch11.85 KBwim leers

Issue fork ckeditor5-3224261

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Wim Leers created an issue. See original summary.

nod_’s picture

opened #3227837: Optimize aggregation grouping to try out a few things.

wim leers’s picture

nod_’s picture

Status: Active » Needs review

At a minium I guess we can just let Drupal do it's thing :)

it's not concatenated in it's own file but I'm not sure of the value of separating the ckeditor. Theorically it makes sense but practically not sure it would help much, if ckeditor is on the page, there are probably a bunch of ajax/form script that are added that would also change the aggregate. So aggregate reuse would not be achieved with splitting the ckeditor into it's own thing.

wim leers’s picture

At a minium I guess we can just let Drupal do it's thing :)

Hah! True :)

it's not concatenated in it's own file but I'm not sure of the value of separating the ckeditor. Theorically it makes sense but practically not sure it would help much, if ckeditor is on the page, there are probably a bunch of ajax/form script that are added that would also change the aggregate.

Hm … really? First: ajax/form scripts are MUCH smaller. Second: every ajax/form script combination will result in a different aggregate. But for any one CKE5 configuration (say the Basic HTML vs Full HTML one) will require the same big set of JS to be loaded. If that were to have its own aggregate, we'd avoid re-downloading that, wouldn't we?

I agree that if ajax/form scripts that get loaded change the CKE5 aggregate, doing this is pointless. But isn't the whole point to make CKE5 have its own aggregate, precisely to avoid that? 🤞

(I feel like I'm missing something? 😬)

nod_’s picture

oh yeah, looking at it this way it makes more sense.

Set a specific group to make a separate aggregate. It's "can-of-worm"-y but I didn't want to have to duplicate aggregation logic in the module.

nod_’s picture

All right, fixed some library definition and made a more generic way of setting the aggregation group

nod_’s picture

well well well #1996238-67: Replace hook_library_info() by *.libraries.yml file :)

needs a core patch to allow defining special groups from the library info for JS files :D or we go the way of CSS but we allow arbitrary groups to be defined:

my_library:
  js:
    ckeditor5:
      yolo.js : {}

Kinda scope creep but i don't have a better way so far.

To optimize with a "common" bundle we can do a array_intersect across all filter formats, should be pretty easy to implement.

nod_’s picture

nod_’s picture

We might need to commit the preprocess: false removal to make sure those files can be aggregated at all, even if it's not optimal. The core issue might take a while to get solved.

wim leers’s picture

Status: Needs review » Needs work
  1. AFAICT this is still not guaranteed to work correctly because it does not check library dependencies. It uses \Drupal\ckeditor5\Plugin\CKEditor5PluginManager::getEnabledLibraries() but does not check the dependencies of the returned libraries. See more detail in my merge request review.
  2. I do not think this needs to be hard-blocked on #3232810: [PP-1] Allow setting aggregation groups for js files in library definitions. An alternative solution is using hook_library_info_build() to generate N+1 asset libraries: N asset libraries for the N configured CKEditor 5 text editors (one per text format), plus 1 for the asset libraries that all N+1 need to load. This part is already happening.
    And then, for the temporary work-around: instead of letting the library system do its thing, do file_get_contents() on each of the collected JS files, and file_put_contents("public://ckeditor5/$format_id.js", FILE_APPEND) them. And of course, set preprocess: false at that point.
    Once #3232810 is solved, we can remove that work-around.
wim leers’s picture

Project: CKEditor 5 » Drupal core
Version: 1.0.x-dev » 9.3.x-dev
Component: Code » ckeditor5.module
Assigned: Unassigned » wim leers
Related issues: +#3249698: Simplify CKEditor 5 asset library metadata

We'll need to redo the MR from scratch as we move from the ckeditor5 project to the drupal project.

Doing that … but as part of doing that, realized we should do #3249698: Simplify CKEditor 5 asset library metadata.

wim leers’s picture

Assigned: wim leers » Unassigned
Status: Needs work » Needs review
StatusFileSize
new11.85 KB

Port complete.

Status: Needs review » Needs work

The last submitted patch, 14: 3224261-14.patch, failed testing. View results

wim leers’s picture

Transfering reviews on the old MR, and updating them in the process. This allows me to close the old MR.

  1. +++ b/core/modules/ckeditor5/ckeditor5.libraries.yml
    @@ -37,21 +37,23 @@ drupal.ckeditor5.quickedit-temporary-work-around:
    +    - core/ckeditor5
    ...
    +    - core/ckeditor5
    

    We had a discussion about this on the original MR, at https://git.drupalcode.org/project/ckeditor5/-/merge_requests/114#note_4....

    The first implementation of this gathered all libraries that depend on core/ckeditor5. But that wouldn't have worked for indirect dependencies.

    Still, I think it's great to require that every asset library declared by a CKEditor 5 plugin definition has core/ckeditor5 in its dependency tree. We should create a follow-up issue to validate that, much like #3248188: Plugin definition DX: validate drupal.conditions and other plugin definition validation checks.

  2. +++ b/core/modules/ckeditor5/ckeditor5.module
    @@ -323,6 +323,49 @@ function _add_attachments_to_editor_update_response(array $form, AjaxResponse &$
    +        $ckeditor5_formats_libraries[$editor->id()] = $cke5_plugin_manager->getEnabledLibraries($editor);
    ...
    +  // Declare a library for each configured text format. This is a performance
    +  // optimization to separate the CKEditor 5 JavaScript from the rest of the
    +  // aggregated assets.
    +  foreach ($ckeditor5_formats_libraries as $format_id => $ckeditor5_plugins_libraries) {
    +    $libraries['drupal.format.' . $format_id] = [
    +      'dependencies' => array_merge(['core/ckeditor5'], $ckeditor5_plugins_libraries),
    +    ];
    +  }
    

    (I wrote a different variation of this at https://git.drupalcode.org/project/ckeditor5/-/merge_requests/114#note_4... originally.)

    This will only list those asset libraries that are directly depended upon by all enabled CKEditor 5 plugins. It will not include indirect dependencies. For example: ckeditor5/drupal.ckeditor5.image will be listed, but not core/ckeditor5.image.

    That means that the auto-generated drupal.format.FORMAT_ID asset libraries will only list those "top-level" asset libaries, plus core/ckeditor5, because that is explicitly merged in.

    We should either:

    1. Gather the full dependency tree.
    2. Not treat core/ckeditor5 in a special way.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

I think we should consider splitting this into two stages:

Split #3224261-11: [PP-1] Front-end performance: concatenate CKEditor 5 JS assets for every unique configuration into its own issue - remove preprocess: false so that files get aggregated. This will give us a baseline using core's existing aggregation logic, and it might be enough for ckeditor5 to be marked stable.

We can then do some manual evaluation of how bad the aggregate duplication is with say Claro on a couple of different entity forms vs. Olivero + comment form and similar, and it'll then be easy to compare that against the various solutions proposed here.

wim leers’s picture

Sounds like a good plan, @catch! :)

As I mentioned in #3228464-25: API for contrib projects to load CKEditor translations, I think that after that lands, this becomes Major and stable blocker.

catch’s picture

Title: Front-end performance: concatenate CKEditor 5 JS assets for every unique configuration » [PP-1] Front-end performance: concatenate CKEditor 5 JS assets for every unique configuration

OK I've opened #3264512: Enable aggregation for CKEditor 5 assets, postponing on that issue.

wim leers’s picture

Title: [PP-1] Front-end performance: concatenate CKEditor 5 JS assets for every unique configuration » [PP-2] Front-end performance: concatenate CKEditor 5 JS assets for every unique configuration
Status: Needs work » Postponed
Related issues: +#3264512: Enable aggregation for CKEditor 5 assets, +#3232810: [PP-1] Allow setting aggregation groups for js files in library definitions
wim leers’s picture

Title: [PP-2] Front-end performance: concatenate CKEditor 5 JS assets for every unique configuration » [PP-1] Front-end performance: concatenate CKEditor 5 JS assets for every unique configuration

D'oh 🙈

Related: #3238333-37: Roadmap to CKEditor 5 stable in Drupal 9 — I'm proposing to make this a nice-to-have, something we can do after CKEditor 5 is already stable.

catch’s picture

Title: [PP-1] Front-end performance: concatenate CKEditor 5 JS assets for every unique configuration » [PP-2] Front-end performance: concatenate CKEditor 5 JS assets for every unique configuration

PP-2 was right after all, it's just a different 2.

wim leers’s picture

Hah! RIGHT! That's why I went back-and-forth and still confused myself :D

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

wim leers’s picture

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

Title: [PP-2] Front-end performance: concatenate CKEditor 5 JS assets for every unique configuration » [PP-1] Front-end performance: concatenate CKEditor 5 JS assets for every unique configuration

We can do most of this using #3565258: Support library-specific aggregates.

There are a couple of caveats though which would probably affect other approaches here.

Any ckeditor library that relies on a different core library, such as the media plugin + media dialog, you end up with the dialog library in-between the ckeditor5 libraries. Adding unique_js_aggregate: 'ckeditor5' to all of the ckeditor libraries I was able to take about 500mb off the AssetAggregationAcrossPages node/add performance test though, because the libraries get split into a handful of self-contained libraries which have higher cache hit rates.

Also the issue summary here isn't correct any more, all of these files are aggregated, not served individually, so at the moment we aggregate them along with some other libraries, resulting in a fair bit of duplication across requests.

catch’s picture

Issue summary: View changes

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.