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.
- Measure performance on 0 ms network latency, 100 ms, 200 ms, 500 ms, 1000 ms.
- 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.
- Repeat the performance tests.
- Decide.
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | 3224261-14.patch | 11.85 KB | wim leers |
Issue fork ckeditor5-3224261
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:
- 3224261-front-end-performance-concatenate
changes, plain diff MR !114
Comments
Comment #2
nod_opened #3227837: Optimize aggregation grouping to try out a few things.
Comment #3
wim leers🥳
Comment #5
nod_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.
Comment #6
wim leersHah! True :)
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? 😬)
Comment #7
nod_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.
Comment #8
nod_All right, fixed some library definition and made a more generic way of setting the aggregation group
Comment #9
nod_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:
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.
Comment #10
nod_code requires core patch from #3232810: [PP-1] Allow setting aggregation groups for js files in library definitions
Comment #11
nod_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.
Comment #12
wim leers\Drupal\ckeditor5\Plugin\CKEditor5PluginManager::getEnabledLibraries()but does not check the dependencies of the returned libraries. See more detail in my merge request review.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, andfile_put_contents("public://ckeditor5/$format_id.js", FILE_APPEND)them. And of course, setpreprocess: falseat that point.Once #3232810 is solved, we can remove that work-around.
Comment #13
wim leersWe'll need to redo the MR from scratch as we move from the
ckeditor5project to thedrupalproject.Doing that … but as part of doing that, realized we should do #3249698: Simplify CKEditor 5 asset library metadata.
Comment #14
wim leersPort complete.
Comment #16
wim leersTransfering reviews on the old MR, and updating them in the process. This allows me to close the old MR.
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/ckeditor5in 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.(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.imagewill be listed, but notcore/ckeditor5.image.That means that the auto-generated
drupal.format.FORMAT_IDasset libraries will only list those "top-level" asset libaries, pluscore/ckeditor5, because that is explicitly merged in.We should either:
core/ckeditor5in a special way.Comment #19
catchI 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: falseso 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.
Comment #20
wim leersSounds 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 and
stable blocker.Comment #21
catchOK I've opened #3264512: Enable aggregation for CKEditor 5 assets, postponing on that issue.
Comment #22
wim leers#3264512: Enable aggregation for CKEditor 5 assets landed! But this still needs #3232810: [PP-1] Allow setting aggregation groups for js files in library definitions. So keeping
[PP-1]prefix; should've been[PP-2].Comment #23
wim leersD'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.
Comment #24
catchPP-2 was right after all, it's just a different 2.
Comment #25
wim leersHah! RIGHT! That's why I went back-and-forth and still confused myself :D
Comment #27
wim leersThings are moving in core on this front! #2258313: Add license information to aggregated assets just landed and #3302755: On-the-fly JavaScript minification is now unblocked.
Unfortunately no movement yet on #3232810: [PP-1] Allow setting aggregation groups for js files in library definitions or #1945262: Introduce "before" and "after" for conditional ordering in library definitions, which are the blockers for this issue.
Comment #30
catchWe 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.
Comment #31
catch