Make WordPress Core


Ignore:
Timestamp:
10/21/2025 05:59:38 AM (3 months ago)
Author:
westonruter
Message:

Script Loader: Load block styles on demand in classic themes via the template enhancement output buffer.

  • This applies in classic themes when a site has not opted out of the template enhancement buffer by filtering wp_should_output_buffer_template_for_enhancement off.
  • Both should_load_separate_core_block_assets and should_load_block_assets_on_demand are filtered on, as otherwise they are only enabled by default in block themes.
  • Any style enqueued after wp_head and printed via print_late_styles() will get hoisted up to be inserted right after the wp-block-library inline style in the HEAD.
  • The result is a >10% benchmarked improvement in LCP for core classic themes due to a ~100KB reduction in the amount of CSS unconditionally being served with every page load.

Developed in https://github.com/WordPress/wordpress-develop/pull/10288

Follow-up to [60936].

Props sjapaget, westonruter, peterwilsoncc, dmsnell, mindctrl.
See #43258.
Fixes #64099.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php

    r57028 r61008  
    1616
    1717    /**
    18      * @var WP_Styles
     18     * @var WP_Styles|null
    1919     */
    20     private $old_wp_styles;
     20    protected $original_wp_styles;
    2121
    2222    /**
     
    3333        parent::set_up();
    3434
    35         $this->old_wp_styles = $GLOBALS['wp_styles'];
     35        global $wp_styles;
     36        $this->original_wp_styles = $wp_styles;
     37        $wp_styles                = null;
     38        wp_styles();
    3639
    3740        $this->includes_url = includes_url();
    3841
    3942        remove_action( 'wp_default_styles', 'wp_default_styles' );
    40 
    41         if ( empty( $GLOBALS['wp_styles'] ) ) {
    42             $GLOBALS['wp_styles'] = null;
    43         }
    4443    }
    4544
    4645    public function tear_down() {
    47         $GLOBALS['wp_styles'] = $this->old_wp_styles;
     46        global $wp_styles;
     47        $wp_styles = $this->original_wp_styles;
    4848
    4949        add_action( 'wp_default_styles', 'wp_default_styles' );
     
    5757     * @dataProvider data_block_data
    5858     *
     59     * @covers ::register_core_block_style_handles
     60     * @covers ::wp_should_load_separate_core_block_assets
     61     *
    5962     * @param string $name   The block name.
    6063     * @param array  $schema The block's schema.
    6164     */
    6265    public function test_wp_should_load_separate_core_block_assets_false( $name, $schema ) {
     66        add_filter( 'should_load_separate_core_block_assets', '__return_false' );
     67        $this->assertFalse( wp_should_load_separate_core_block_assets(), 'Core blocks are not expected to load separate assets' );
    6368        register_core_block_style_handles();
    6469
     
    7984     * @dataProvider data_block_data
    8085     *
     86     * @covers ::register_core_block_style_handles
     87     * @covers ::wp_should_load_separate_core_block_assets
     88     *
    8189     * @param string $name   The block name.
    8290     * @param array  $schema The block's schema.
     
    8492    public function test_wp_should_load_separate_core_block_assets_true( $name, $schema ) {
    8593        add_filter( 'should_load_separate_core_block_assets', '__return_true' );
     94        $this->assertTrue( wp_should_load_separate_core_block_assets(), 'Core assets are expected to load separately' );
    8695        register_core_block_style_handles();
    8796
Note: See TracChangeset for help on using the changeset viewer.