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/theme/wpAddGlobalStylesForBlocks.php

    r59256 r61008  
    288288     * @ticket 56915
    289289     * @ticket 61165
     290     *
     291     * @covers ::wp_add_global_styles_for_blocks
    290292     */
    291293    public function test_blocks_inline_styles_get_rendered() {
     294        // Override wp_load_classic_theme_block_styles_on_demand().
     295        add_filter( 'should_load_block_assets_on_demand', '__return_false' ); // Needed for the .wp-block-post-featured-image assertion below.
     296
    292297        $this->set_up_third_party_block();
    293298        wp_register_style( 'global-styles', false, array(), true, true );
     
    312317     * @ticket 57868
    313318     * @ticket 61165
     319     *
     320     * @covers ::wp_add_global_styles_for_blocks
    314321     */
    315322    public function test_third_party_blocks_inline_styles_for_elements_get_rendered_when_per_block() {
    316323        $this->set_up_third_party_block();
    317324        add_filter( 'should_load_separate_core_block_assets', '__return_true' );
     325        $this->assertTrue( wp_should_load_separate_core_block_assets(), 'Core assets are expected to load separately' );
    318326
    319327        wp_register_style( 'global-styles', false, array(), true, true );
Note: See TracChangeset for help on using the changeset viewer.