Skip to content

Commit

Permalink
Performance: only load concatenated CSS when needed.
Browse files Browse the repository at this point in the history
See #1258

We currently enqueue a `jetpack.css` concatenated CSS file as soon as your site is connected to WordPress.com.
That jetpack.css file is built off a list of stylesheets from individual modules that are not enqueued when the concatenated file is enqueued.

In this commit, we add a new condition: for the file to be enqueued, there has to be at least 2 modules that need it. This will avoid loading the file when it wouldn't even be needed at all.
This requires a change in the implementation: we now check for active modules before we dequeue individual assets / enqueue jetpack.css. This is an additional call to get an option (( new Modules() )->get_active()).
This also means that the new $modules_with_concatenated_css variable will need to be kept updated.

Note that this is only a partial improvement. We still need to address the larger questions around the implementation in today's world, as discussed here:
#1258 (comment)
  • Loading branch information
jeherve committed Nov 14, 2023
1 parent bb1b509 commit a8947a4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

CSS Concatenation: avoid optimizing CSS loading when less than 2 modules that require it are active.
39 changes: 38 additions & 1 deletion projects/plugins/jetpack/class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,35 @@ class Jetpack {
*/
public $xmlrpc_server = null;

/**
* List of Jetpack modules that have CSS that gets concatenated into jetpack.css.
*
* See $concatenated_style_handles for the list of handles,
* and the implode_frontend_css method for more details.
*
* When updating this list, make sure to update $concatenated_style_handles as well.
*
* @var array List of Jetpack modules.
*/
public $modules_with_concatenated_css = array(
'carousel',
'contact-form',
'infinite-scroll',
'likes',
'related-posts',
'sharedaddy',
'shortcodes',
'subscriptions',
'tiled-gallery',
'widgets',
);

/**
* The handles of styles that are concatenated into jetpack.css.
*
* When making changes to that list, you must also update concat_list in tools/webpack.config.css.js.
* When making changes to that list,
* you must also update concat_list in tools/webpack.config.css.js,
* and to $modules_with_concatenated_css if necessary.
*
* @var array The handles of styles that are concatenated into jetpack.css.
*/
Expand Down Expand Up @@ -6231,6 +6256,18 @@ public function implode_frontend_css( $travis_test = false ) {
$do_implode = false;
}

/*
* Only proceed if at least 2 modules with concatenated CSS are active.
* There is no point in serving a big concatenated CSS file
* if there are no features (or only one) that actually need some CSS loaded.
*/
$active_modules = self::get_active_modules();
$modules_with_concatenated_css = $this->modules_with_concatenated_css;
$active_module_with_css_count = count( array_intersect( $active_modules, $modules_with_concatenated_css ) );
if ( $active_module_with_css_count < 2 ) {
$do_implode = false;
}

/**
* Allow CSS to be concatenated into a single jetpack.css file.
*
Expand Down

0 comments on commit a8947a4

Please sign in to comment.