Make WordPress Core

Opened 6 weeks ago

Last modified 13 days ago

#66200 assigned enhancement

Position block support: derive container class from styles so identical rules are deduplicated

Reported by: mukesh27 Owned by: mukesh27
Priority: normal Milestone: 7.3
Component: Editor Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: performance

Description

wp_render_position_support() builds its container class from a per-request counter:

<?php
$class_name = wp_unique_id( 'wp-container-' );

Every block instance with position support therefore gets its own class and its own rule in the block-supports style engine store, even when the resulting CSS is byte-for-byte identical.

The other block supports that emit a scoped rule derive the class from the style inputs, so identical styles collapse into one rule:

Support Class strategy
layout.php wp_unique_id_from_values( $hash_input, 'wp-container-' )
custom-css.php wp_unique_id_from_values( $parsed_block, ... )
states.php md5() of the rules
settings.php md5( serialize( $block ) )
position.php wp_unique_id() (counter)

wp_render_layout_support_flag() documents why it hashes: to keep class names the same for blocks with the same definition, and stable across paginations such as the Query block's enhanced pagination. Position uses the same wp-container- prefix but doesn't get that behaviour.

Impact

With 200 Group blocks that all use position: sticky; top: 0px:

Rules in store Generated CSS
trunk 200 22,092 bytes
patched 1 116 bytes

This CSS is inlined into every front-end response, so the duplication adds page weight on every request, on top of the render cost.

Change History (7)

#1 @mukesh27
6 weeks ago

  • Owner set to mukesh27
  • Status new → assigned

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


6 weeks ago
#2

  • Keywords has-patch added; needs-patch removed

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude
Model(s): Opus 5.5
Used for: Initial code skeleton; final implementation was reviewed and edited by me.

#3 @mukesh27
6 weeks ago

  • Keywords has-unit-tests added

#4 @therssoftware
6 weeks ago

## Test Report: Trac #66200 / PR #13792

Ticket: https://core.trac.wordpress.org/ticket/66200
Pull Request: ​https://github.com/WordPress/wordpress-develop/pull/13792
Tested by: @therssoftware (GitHub: @robelsust)
Patch tested: PR #13792 commit 43399cf (mukeshpanchal27:trac-66200)

---

### Environment

  • WordPress: 7.2-alpha-63166-src
  • PHP: 8.3.14
  • PHPUnit: 9.6.37
  • Database: MariaDB / MySQL
  • OS: macOS (Darwin 24.3.0 arm64)
  • Active Theme: Block theme (block-theme-child-with-fluid-typography / twentytwentyfour)

---

### Summary of Testing

I tested PR #13792 which replaces sequential wp_unique_id('wp-container-') IDs in wp_render_position_support() with content-hashed IDs derived via wp_unique_id_from_values( $position_hash_input, 'wp-container-' ).

#### 1. Identical Sticky Blocks Deduplication

  • Test: Rendered 50 identical sticky blocks (style.position = { type: 'sticky', top: '0px' }).
  • Result:
    • All 50 blocks received the exact same hashed class name (wp-container-c1e6563f is-position-sticky).
    • The style engine stylesheet in context block-supports contained exactly 1 CSS rule: `css .wp-container-c1e6563f{top:calc(0px + var(--wp-admin--admin-bar--position-offset, 0px));position:sticky;z-index:10;} `
    • Performance / Output: Massive reduction in duplicated inline CSS (from ~5,800 bytes down to 116 bytes for 50 blocks, a 98% CSS reduction).

#### 2. Differing Position Styles Separation

  • Test: Rendered blocks with different position values (top: 0px vs top: 32px).
  • Result:
    • Distinct class names generated (wp-container-c1e6563f vs wp-container-00205f69).
    • No hash collisions; each distinct configuration correctly received its own CSS declaration.

#### 3. Responsive / Viewport State Deduplication

  • Test: Tested blocks with responsive overrides (e.g., base top: 0px with @mobile top: 10px vs @mobile top: 20px).
  • Result:
    • Blocks sharing identical base + viewport configurations shared the same container class.
    • Blocks differing only at a specific breakpoint correctly produced separate classes.
    • Deduplicated both base rules and media query rules without redundant duplicate @media declarations.

#### 4. Automated Tests & Coding Standards

  • Position Support Tests: vendor/bin/phpunit tests/phpunit/tests/block-supports/wpRenderPositionSupport.php -> OK (11 tests, 22 assertions)
  • Block Supports Suite Regression: vendor/bin/phpunit --filter Block_Supports -> OK (431 tests, 586 assertions)
  • Coding Standards: vendor/bin/phpcs --standard=WordPress-Core src/wp-includes/block-supports/position.php tests/phpunit/tests/block-supports/wpRenderPositionSupport.php -> 0 errors, 0 warnings

---

### Conclusion
PR #13792 works as expected, successfully deduplicating position container classes and style engine rules while preventing collisions for differing base and responsive styles. Ready for merge.

---

### AI Disclosure
AI assistance: Yes
Tool(s): Gemini
Model(s): Gemini 2.5 Pro
Used for: Environment execution, automated test verification, and test report drafting. All tests and outputs were manually reviewed and verified.

#5 @mukesh27
6 weeks ago

Thanks @therssoftware for testing!

#6 @im3dabasia1
3 weeks ago

I’m going through the 7.2 tickets ahead of beta 1.

Thank you for the work on this, @mukesh27. With beta 1 only a few days away, is 7.2 still a realistic target?

#7 @wildworks
13 days ago

  • Milestone 7.2 → 7.3

I believe the behavior reported here has remained unchanged since 6.2, when the position block support was introduced. Additionally, as this is an enhancement, it may be too late to address it in 7.2.

Note: See TracTickets for help on using tickets.