Skip to content

Blocks: Cache core block stylesheet file sizes. - #13225

Open
spacedmonkey wants to merge 4 commits into
WordPress:trunkfrom
spacedmonkey:file-size-cache
Open

Blocks: Cache core block stylesheet file sizes.#13225
spacedmonkey wants to merge 4 commits into
WordPress:trunkfrom
spacedmonkey:file-size-cache

Conversation

@spacedmonkey

Copy link
Copy Markdown
Member

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.

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.
@github-actions

github-actions Bot commented Aug 21, 2026

Copy link
Copy Markdown

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props spacedmonkey, adamsilverstein, nickchomey.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_files transient and attach file_size to registered core block style handles.
  • Update wp_maybe_inline_styles() to use a provided file_size and cache computed sizes per-handle when missing.
  • Add PHPUnit coverage asserting file_size is present for core block style handles and that wp_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.

Comment on lines +2148 to +2153
$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 );
Comment on lines +76 to 79
$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 );
Comment on lines 75 to +88
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 );
}
Comment on lines +766 to +770
/**
* @ticket 58394
*
* @covers ::wp_maybe_inline_styles
*/

@adamsilverstein adamsilverstein left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@nickchomey

nickchomey commented Sep 1, 2026

Copy link
Copy Markdown

One thought: as per the discussion in slack, on hosts where filesize() check is fast/cached at OS-level, wouldn't reading from a transient actually be (much) slower - especially if they don't have a persistent object cache enabled such that a db query is done? (and, i suspect object cache read would still be slower than filesize() on many systems).

So, if caching does get implemented, would there be any sensible way to only cache in transient if filesize() is actually slow?

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 wp_filesize call to when the cache is set, with the fallback wp_filesize in wp_maybe_inline_styles() . And it stands to reason that even a object cache-free get_transient is probably faster than repopulating the styles (hence the cache was added to begin with).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Sponsor
SponsoredKunjungi sekarang
Promo