Make WordPress Core

Opened 3 weeks ago

Closed 3 days ago

#65929 closed defect (bug) (fixed)

Block Supports: Bail early in wp_render_block_states_support() when a block has no style attribute

Reported by: mukesh27 Owned by: westonruter
Priority: normal Milestone: 7.1.1
Component: Editor Version: 7.1
Severity: normal Keywords: has-patch gutenberg-merge fixed-major dev-reviewed
Cc: Focuses: performance

Description

wp_render_block_states_support() is hooked on render_block, so it runs once per block on every front-end request. Before it can determine that a block has no state styles, it resolves the block type from the registry, calls wp_get_global_settings( array( 'viewport' ) ), and computes responsive media queries:

$block_type               = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
$supported_pseudo_states  = WP_Theme_JSON::VALID_BLOCK_PSEUDO_SELECTORS[ $block_name ] ?? array();
$style                    = $block['attrs']['style'] ?? array();
$css_rules                = array();
$viewport_settings        = wp_get_global_settings( array( 'viewport' ) );
$responsive_media_queries = WP_Theme_JSON::get_viewport_media_queries( $viewport_settings );

Most blocks on a page carry no style attribute, so all of this is discarded.

The viewport lookup is the costly part. Core's theme.json defines no viewport key, and wp_get_global_settings() falls back to _wp_array_get( $settings, $path, $settings ), so a missing path returns the entire settings array. sanitize_viewport_settings() then finds no mobile/tablet members, discards it, and rebuilds DEFAULT_VIEWPORT_BREAKPOINTS — two preg_match() calls and two media-query strings — for every block, on every request.

Every CSS rule this function can produce is keyed off $style, and the function already returns $block_content unchanged when $css_rules ends up empty. So resolving $style first and returning early when it is absent skips the above without changing behaviour.

Change History (12)

#1 @mukesh27
3 weeks ago

  • Owner set to mukesh27
  • Status newassigned

This ticket was mentioned in PR #13051 on WordPress/wordpress-develop by @mukesh27.


3 weeks ago
#2

  • Keywords has-patch added; needs-patch removed

Trac ticket: https://core.trac.wordpress.org/ticket/65929

## Use of AI Tools
Yes. For drafting the details.

#3 @wildworks
2 weeks ago

  • Milestone Awaiting Review7.1.1

Since Block State is a new feature in 7.1, this ticket can be addressed in a minor release.

This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.


2 weeks ago

#5 @wildworks
5 days ago

  • Keywords gutenberg-merge added

This ticket was mentioned in Slack in #core by adrianduffell. View the logs.


4 days ago

#7 @adamsilverstein
4 days ago

@westonruter - do want to own this one and get it committed before the RC next week?

#8 @westonruter
4 days ago

  • Owner changed from mukesh27 to westonruter
  • Status assignedreviewing

Yeah, I'll take it.

#9 @westonruter
4 days ago

  • Resolutionfixed
  • Status reviewingclosed

In 63474:

Editor: Skip state styles for blocks without a style attribute.

The wp_render_block_states_support() function is hooked on render_block, so it runs once per block on every front-end request. Before it could determine that a block has no state styles, it resolved the block type from the registry, called wp_get_global_settings(), and computed responsive media queries; all of this work is discarded for the overwhelming majority of blocks, which carry no style attribute at all.

Every CSS rule the function can produce is keyed off the block's style attribute, and the function already returns the content unchanged when no rules are generated. Resolving style first and returning early when it is absent is therefore behavior-preserving.

Developed in https://github.com/WordPress/wordpress-develop/pull/13051 and https://github.com/WordPress/gutenberg/pull/81908.
Follow-up to r62453, r62671.

Props mukesh27, westonruter, adamsilverstein, wildworks.
Fixes #65929.

#10 @westonruter
4 days ago

  • Keywords fixed-major dev-feedback added
  • Resolution fixed
  • Status closedreopened

@adamsilverstein Re-opening for 7.1 merge consideration.

#11 @adamsilverstein
3 days ago

  • Keywords dev-reviewed added; dev-feedback removed

+1 to backporting this for 7.1.1.

#12 @westonruter
3 days ago

  • Resolutionfixed
  • Status reopenedclosed

In 63489:

Editor: Skip state styles for blocks without a style attribute.

The wp_render_block_states_support() function is hooked on render_block, so it runs once per block on every front-end request. Before it could determine that a block has no state styles, it resolved the block type from the registry, called wp_get_global_settings(), and computed responsive media queries; all of this work is discarded for the overwhelming majority of blocks, which carry no style attribute at all.

Every CSS rule the function can produce is keyed off the block's style attribute, and the function already returns the content unchanged when no rules are generated. Resolving style first and returning early when it is absent is therefore behavior-preserving.

Developed in https://github.com/WordPress/wordpress-develop/pull/13051 and https://github.com/WordPress/gutenberg/pull/81908.
Follow-up to r62453, r62671.

Reviewed by adamsilverstein.
Merges r63474 to the 7.1 branch.

Props mukesh27, westonruter, adamsilverstein, wildworks.
Fixes #65929.

Note: See TracTickets for help on using tickets.