Blocks: Cache core block stylesheet file sizes. - #13225
Conversation
Store stylesheet sizes in the core block CSS transient and reuse them when inlining styles so wp_maybe_inline_styles() can avoid repeated wp_filesize() calls.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
Pull request overview
This PR optimizes core block stylesheet handling by caching CSS file sizes (alongside paths) and reusing them during wp_maybe_inline_styles() so repeated wp_filesize() calls can be avoided.
Changes:
- Cache core block CSS file sizes in the
wp_core_block_css_filestransient and attachfile_sizeto registered core block style handles. - Update
wp_maybe_inline_styles()to use a providedfile_sizeand cache computed sizes per-handle when missing. - Add PHPUnit coverage asserting
file_sizeis present for core block style handles and thatwp_maybe_inline_styles()honors a provided size.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
src/wp-includes/blocks/index.php |
Cache CSS file sizes in the core blocks transient and add file_size to registered style handle data (incl. RTL). |
src/wp-includes/script-loader.php |
Use and cache file_size in wp_maybe_inline_styles() to reduce repeated filesystem size checks. |
tests/phpunit/tests/dependencies/styles.php |
Add a test verifying wp_maybe_inline_styles() does not call wp_filesize() when file_size is provided. |
tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php |
Assert file_size exists and is non-zero for registered core block style handles. |
tests/phpunit/tests/template.php |
Add file_size test data to stylesheet registration helper. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| $file_size = filesize( $path ); | ||
| if ( 0 === $file_size ) { | ||
| file_put_contents( $path, "/* CSS for $handle */" ); | ||
| } | ||
| wp_style_add_data( $handle, 'path', $path ); | ||
| wp_style_add_data( $handle, 'file_size', $file_size ); |
| $found_files = glob( wp_normalize_path( BLOCKS_PATH . '**/**.css' ) ); | ||
|
|
||
| // Normalize BLOCKS_PATH prior to substitution for Windows environments. | ||
| $normalized_blocks_path = wp_normalize_path( BLOCKS_PATH ); |
| if ( ! $files ) { | ||
| $files = glob( wp_normalize_path( BLOCKS_PATH . '**/**.css' ) ); | ||
| $found_files = glob( wp_normalize_path( BLOCKS_PATH . '**/**.css' ) ); | ||
|
|
||
| // Normalize BLOCKS_PATH prior to substitution for Windows environments. | ||
| $normalized_blocks_path = wp_normalize_path( BLOCKS_PATH ); | ||
|
|
||
| $files = array_map( | ||
| static function ( $file ) use ( $normalized_blocks_path ) { | ||
| return str_replace( $normalized_blocks_path, '', $file ); | ||
| }, | ||
| $files | ||
| ); | ||
| $files = array(); | ||
| $sizes = array(); | ||
|
|
||
| foreach ( $found_files as $found_file ) { | ||
| $relative_path = str_replace( $normalized_blocks_path, '', $found_file ); | ||
| $files[ $relative_path ] = $relative_path; | ||
| $sizes[ $relative_path ] = wp_filesize( $found_file ); | ||
| } |
| /** | ||
| * @ticket 58394 | ||
| * | ||
| * @covers ::wp_maybe_inline_styles | ||
| */ |
adamsilverstein
left a comment
There was a problem hiding this comment.
Code looks good to me.
This feels helpful, but I'm not that confident about it. Is there a way to measure the impact of this in terms of reduced calls or improved performance? When does this change kick in? Adding some additional details to the PR or trac ticket description would be helpful.
|
One thought: as per the discussion in slack, on hosts where So, if caching does get implemented, would there be any sensible way to only cache in transient if Or am I missing how/when this mechanism gets called/used and caching would just be a win in all circumstances? Edit: nevermind - i see now that the transient caching is already used, so this is really just moving the |
Store stylesheet sizes in the core block CSS transient and reuse them when inlining styles so wp_maybe_inline_styles() can avoid repeated wp_filesize() calls.
Trac ticket: https://core.trac.wordpress.org/ticket/59596
Use of AI Tools
AI assistance: Yes
Tool(s): GitHub Copilot
Model(s): Fable
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.