Skip to content

Commit 40bb201

Browse files
committed
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 #10457 Follow-up to [61008], [61076], [60936]. Props westonruter. See #64099, #64150, #64166, #43258. git-svn-id: https://develop.svn.wordpress.org/trunk@61122 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5a9dcbc commit 40bb201

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

src/wp-includes/script-loader.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3577,8 +3577,11 @@ function wp_remove_surrounding_empty_script_tags( $contents ) {
35773577
* Adds hooks to load block styles on demand in classic themes.
35783578
*
35793579
* @since 6.9.0
3580+
*
3581+
* @see _add_default_theme_supports()
35803582
*/
35813583
function wp_load_classic_theme_block_styles_on_demand() {
3584+
// This is not relevant to block themes, as they are opted in to loading separate styles on demand via _add_default_theme_supports().
35823585
if ( wp_is_block_theme() ) {
35833586
return;
35843587
}
@@ -3590,25 +3593,29 @@ function wp_load_classic_theme_block_styles_on_demand() {
35903593
*/
35913594
add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true', 0 );
35923595

3596+
// If a site has opted out of the template enhancement output buffer, then bail.
35933597
if ( ! wp_should_output_buffer_template_for_enhancement() ) {
35943598
return;
35953599
}
35963600

3601+
// The following two filters are added by default for block themes in _add_default_theme_supports().
3602+
35973603
/*
3598-
* If the theme supports block styles, add filters to ensure they are loaded separately and on demand. Without this,
3599-
* if a theme does not want or support block styles, then enabling these filters can result in undesired separate
3600-
* block-specific styles being enqueued, though a theme may also be trying to nullify the wp-block-library
3601-
* stylesheet.
3604+
* Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally,
3605+
* and so that block-specific styles will only be enqueued when they are used on the page.
3606+
* A priority of zero allows for this to be easily overridden by themes which wish to opt out.
36023607
*/
3603-
if ( current_theme_supports( 'wp-block-styles' ) ) {
3604-
/*
3605-
* Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally,
3606-
* and so that block-specific styles will only be enqueued when they are used on the page.
3607-
*/
3608-
add_filter( 'should_load_separate_core_block_assets', '__return_true', 0 );
3608+
add_filter( 'should_load_separate_core_block_assets', '__return_true', 0 );
36093609

3610-
// Also ensure that block assets are loaded on demand (although the default value is from should_load_separate_core_block_assets).
3611-
add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 );
3610+
/*
3611+
* Also ensure that block assets are loaded on demand (although the default value is from should_load_separate_core_block_assets).
3612+
* As above, a priority of zero allows for this to be easily overridden by themes which wish to opt out.
3613+
*/
3614+
add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 );
3615+
3616+
// If a site has explicitly opted out of loading block styles on demand via filters with priorities higher than above, then abort.
3617+
if ( ! wp_should_load_separate_core_block_assets() || ! wp_should_load_block_assets_on_demand() ) {
3618+
return;
36123619
}
36133620

36143621
// Add hooks which require the presence of the output buffer. Ideally the above two filters could be added here, but they run too early.

tests/phpunit/tests/template.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,34 +1393,45 @@ public function test_wp_load_classic_theme_block_styles_on_demand_in_block_theme
13931393
*/
13941394
public function data_wp_load_classic_theme_block_styles_on_demand(): array {
13951395
return array(
1396-
'block_theme' => array(
1396+
'block_theme' => array(
13971397
'theme' => 'block-theme',
13981398
'set_up' => static function () {},
1399-
'expected_on_demand' => false,
1399+
'expected_load_separate' => true,
1400+
'expected_on_demand' => true,
14001401
'expected_buffer_started' => false,
14011402
),
1402-
'classic_theme_with_output_buffer_blocked' => array(
1403+
'classic_theme_with_output_buffer_blocked' => array(
14031404
'theme' => 'default',
14041405
'set_up' => static function () {
14051406
add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_false' );
14061407
},
1408+
'expected_load_separate' => false,
14071409
'expected_on_demand' => false,
14081410
'expected_buffer_started' => false,
14091411
),
1410-
'classic_theme_with_block_styles_support' => array(
1412+
'classic_theme_with_should_load_separate_core_block_assets_opt_out' => array(
14111413
'theme' => 'default',
14121414
'set_up' => static function () {
1413-
add_theme_support( 'wp-block-styles' );
1415+
add_filter( 'should_load_separate_core_block_assets', '__return_false' );
14141416
},
1417+
'expected_load_separate' => false,
14151418
'expected_on_demand' => true,
1416-
'expected_buffer_started' => true,
1419+
'expected_buffer_started' => false,
14171420
),
1418-
'classic_theme_without_block_styles_support' => array(
1421+
'classic_theme_with_should_load_block_assets_on_demand_out_out' => array(
14191422
'theme' => 'default',
14201423
'set_up' => static function () {
1421-
remove_theme_support( 'wp-block-styles' );
1424+
add_filter( 'should_load_block_assets_on_demand', '__return_false' );
14221425
},
1426+
'expected_load_separate' => true,
14231427
'expected_on_demand' => false,
1428+
'expected_buffer_started' => false,
1429+
),
1430+
'classic_theme_without_any_opt_out' => array(
1431+
'theme' => 'default',
1432+
'set_up' => static function () {},
1433+
'expected_load_separate' => true,
1434+
'expected_on_demand' => true,
14241435
'expected_buffer_started' => true,
14251436
),
14261437
);
@@ -1436,7 +1447,7 @@ public function data_wp_load_classic_theme_block_styles_on_demand(): array {
14361447
*
14371448
* @dataProvider data_wp_load_classic_theme_block_styles_on_demand
14381449
*/
1439-
public function test_wp_load_classic_theme_block_styles_on_demand( string $theme, ?Closure $set_up, bool $expected_on_demand, bool $expected_buffer_started ) {
1450+
public function test_wp_load_classic_theme_block_styles_on_demand( string $theme, ?Closure $set_up, bool $expected_load_separate, bool $expected_on_demand, bool $expected_buffer_started ) {
14401451
$this->assertFalse( wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() to return false initially.' );
14411452
$this->assertFalse( wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() to return true' );
14421453
$this->assertFalse( has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ), 'Expected wp_template_enhancement_output_buffer_started action to be added for classic themes.' );
@@ -1447,8 +1458,9 @@ public function test_wp_load_classic_theme_block_styles_on_demand( string $theme
14471458
}
14481459

14491460
wp_load_classic_theme_block_styles_on_demand();
1461+
_add_default_theme_supports();
14501462

1451-
$this->assertSame( $expected_on_demand, wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() return value.' );
1463+
$this->assertSame( $expected_load_separate, wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() return value.' );
14521464
$this->assertSame( $expected_on_demand, wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() return value.' );
14531465
$this->assertSame( $expected_buffer_started, (bool) has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ), 'Expected wp_template_enhancement_output_buffer_started action added status.' );
14541466
}

0 commit comments

Comments
 (0)