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/register.php

    r59938 r61008  
    1212
    1313    /**
     14     * @var WP_Scripts|null
     15     */
     16    protected $original_wp_scripts;
     17
     18    /**
     19     * @var WP_Styles|null
     20     */
     21    protected $original_wp_styles;
     22
     23    /**
    1424     * ID for a test post.
    1525     *
     
    4656     */
    4757    public function render_stub() {}
     58
     59    /**
     60     * Set up.
     61     */
     62    public function set_up() {
     63        parent::set_up();
     64
     65        global $wp_scripts, $wp_styles;
     66        $this->original_wp_scripts = $wp_scripts;
     67        $this->original_wp_styles  = $wp_styles;
     68        $wp_scripts                = null;
     69        $wp_styles                 = null;
     70        wp_scripts();
     71        wp_styles();
     72    }
    4873
    4974    /**
     
    6792            }
    6893        }
     94
     95        global $wp_scripts, $wp_styles;
     96        $wp_scripts = $this->original_wp_scripts;
     97        $wp_styles  = $this->original_wp_styles;
    6998
    7099        parent::tear_down();
     
    10041033        );
    10051034
     1035        // Register the styles not included in the metadata above.
     1036        $metadata = array(
     1037            'file'      => DIR_TESTDATA . '/blocks/notice/block.json',
     1038            'name'      => 'tests/notice',
     1039            'style'     => 'file:./block.css',
     1040            'viewStyle' => 'file:./block-view.css',
     1041        );
     1042        $this->assertSame( 'tests-notice-style', register_block_style_handle( $metadata, 'style' ), 'Style handle is expected to be tests-notice-style' );
     1043        $this->assertSame( 'tests-notice-view-style', register_block_style_handle( $metadata, 'viewStyle' ), 'View style handle is expected to be tests-notice-view-style' );
     1044        $this->assertTrue( wp_style_is( 'tests-notice-style', 'registered' ), 'Expected "tests-notice-style" style to be registered.' );
     1045        $this->assertTrue( wp_style_is( 'tests-notice-view-style', 'registered' ), 'Expected "tests-notice-view-style" style to be registered.' );
     1046
    10061047        $this->assertInstanceOf( 'WP_Block_Type', $result );
    10071048        $this->assertSame( 2, $result->api_version );
     
    11221163        $this->assertSame(
    11231164            wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block.css' ) ),
    1124             wp_normalize_path( wp_styles()->get_data( 'tests-test-block-style', 'path' ) )
     1165            wp_normalize_path( wp_styles()->get_data( 'tests-notice-style', 'path' ) )
    11251166        );
    11261167
     
    11281169        $this->assertSame(
    11291170            wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block-view.css' ) ),
    1130             wp_normalize_path( wp_styles()->get_data( 'tests-test-block-view-style', 'path' ) ),
     1171            wp_normalize_path( wp_styles()->get_data( 'tests-notice-view-style', 'path' ) ),
    11311172            'viewStyle asset path is not correct'
    11321173        );
Note: See TracChangeset for help on using the changeset viewer.