Commit 8534f6b1a8 for wordpress.org
commit 8534f6b1a85df5a7804b2ceb6d8a9f370bd1e65c
Author: Weston Ruter <weston@xwp.co>
Date: Tue Nov 4 07:55:36 2025 +0000
Script Loader: Load block styles on demand in classic themes even when `wp-block-styles` support is absent.
This reverts part of [61076] which made `wp-block-styles` theme support a precondition for opting in to `should_load_separate_core_block_assets` and `should_load_block_assets_on_demand`. This meant that the Twenty Twenty theme (and other themes without this support declared) would not benefit from on-demand block style loading. Nevertheless, even though such themes were not getting block styles loaded on demand, the `wp_load_classic_theme_block_styles_on_demand()` function was proceeding to opt in to the output buffer for hoisting late-printed styles, even though it was unlikely there would then be any. This meant the template enhancement output buffer was being opened for no reason.
Enabling on-demand block style loading is measured to improve FCP and LCP in Twenty Twenty, for example a ~13% improvement over a Fast 4G connection when loading the Sample Page.
Developed in https://github.com/WordPress/wordpress-develop/pull/10457
Follow-up to [61008], [61076], [60936].
Props westonruter.
See #64099, #64150, #64166, #43258.
Built from https://develop.svn.wordpress.org/trunk@61122
git-svn-id: http://core.svn.wordpress.org/trunk@60458 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php
index ac5d71a725..0cabc68dbf 100644
--- a/wp-includes/script-loader.php
+++ b/wp-includes/script-loader.php
@@ -3577,8 +3577,11 @@ function wp_remove_surrounding_empty_script_tags( $contents ) {
* Adds hooks to load block styles on demand in classic themes.
*
* @since 6.9.0
+ *
+ * @see _add_default_theme_supports()
*/
function wp_load_classic_theme_block_styles_on_demand() {
+ // This is not relevant to block themes, as they are opted in to loading separate styles on demand via _add_default_theme_supports().
if ( wp_is_block_theme() ) {
return;
}
@@ -3590,25 +3593,29 @@ function wp_load_classic_theme_block_styles_on_demand() {
*/
add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true', 0 );
+ // If a site has opted out of the template enhancement output buffer, then bail.
if ( ! wp_should_output_buffer_template_for_enhancement() ) {
return;
}
+ // The following two filters are added by default for block themes in _add_default_theme_supports().
+
/*
- * If the theme supports block styles, add filters to ensure they are loaded separately and on demand. Without this,
- * if a theme does not want or support block styles, then enabling these filters can result in undesired separate
- * block-specific styles being enqueued, though a theme may also be trying to nullify the wp-block-library
- * stylesheet.
+ * Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally,
+ * and so that block-specific styles will only be enqueued when they are used on the page.
+ * A priority of zero allows for this to be easily overridden by themes which wish to opt out.
*/
- if ( current_theme_supports( 'wp-block-styles' ) ) {
- /*
- * Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally,
- * and so that block-specific styles will only be enqueued when they are used on the page.
- */
- add_filter( 'should_load_separate_core_block_assets', '__return_true', 0 );
+ add_filter( 'should_load_separate_core_block_assets', '__return_true', 0 );
- // Also ensure that block assets are loaded on demand (although the default value is from should_load_separate_core_block_assets).
- add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 );
+ /*
+ * Also ensure that block assets are loaded on demand (although the default value is from should_load_separate_core_block_assets).
+ * As above, a priority of zero allows for this to be easily overridden by themes which wish to opt out.
+ */
+ add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 );
+
+ // If a site has explicitly opted out of loading block styles on demand via filters with priorities higher than above, then abort.
+ if ( ! wp_should_load_separate_core_block_assets() || ! wp_should_load_block_assets_on_demand() ) {
+ return;
}
// Add hooks which require the presence of the output buffer. Ideally the above two filters could be added here, but they run too early.
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 9693ee76a9..d146ad7425 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '6.9-beta2-61121';
+$wp_version = '6.9-beta2-61122';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.