Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Theme JSON: cached resolved URIs #7596

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Oct 21, 2024

Syncing WordPress/gutenberg#66155

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

What?

Implement "pretty basic caching" for resolved theme URIs in WP_Theme_JSON_Resolver::get_resolved_theme_uris.

Why?

The intention to improve performance, even if slightly.

WP_Theme_JSON_Resolver::get_resolved_theme_uris, and WP_Theme_JSON_Resolver::resolve_theme_file_uris which calls it, is fired multiple times in a single session to:

  1. Generate styles
  2. Deliver resolved URIs in a global styles controller response.

Here are some preliminary, but pretty inconclusive performance results (higher response time is worse):

Uncached This PR (cached)
uncached cached

I'd expect results will become more pronounced as other relative paths are introduced, e.g., fonts, and more blocks support background images.

Cached data is not be stored persistently across page loads.

How?

Simple object caching.

Testing Instructions

PHP CI tests should pass.

Create a theme.json with background image paths defined for several blocks.

Ensure these appear as expected in the editor and frontend.

In the theme.json file, replace one or several paths with new asset paths. Check that the styles have been updated and the editor/frontend looks good.

Here is some test JSON based on current assets in TT5:

{
	"$schema": "../../schemas/json/theme.json",
	"version": 3,
	"settings": {
		"appearanceTools": true
	},
	"styles": {
		"background": {
			"backgroundImage": {
				"url": "file:./assets/images/category-anthuriums.webp"
			}
		},
		"blocks": {
			"core/group": {
				"background": {
					"backgroundImage": {
						"url": "file:./assets/images/category-cactus.webp"
					}
				}
			},
			"core/post-content": {
				"background": {
					"backgroundImage": {
						"url": "file:./assets/images/hero-podcast.webp"
					}
				}
			},
			"core/quote": {
				"background": {
					"backgroundImage": {
						"url": "file:./assets/images/category-sunflowers.webp"
					}
				}
			},
			"core/pullquote": {
				"background": {
					"backgroundImage": {
						"url": "file:./assets/images/dallas-creek-square.webp"
					}
				}
			}
		}
	}
}

Theme JSON: cached resolved URIs
Copy link

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 ramonopoly.

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

@ramonjd ramonjd self-assigned this Oct 21, 2024
Copy link

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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • 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.

@@ -860,7 +871,13 @@ public static function get_resolved_theme_uris( $theme_json ) {
return $resolved_theme_uris;
}

$theme_json_data = $theme_json->get_raw_data();
$theme_json_data = $theme_json->get_raw_data();
$resolved_theme_uris_cache_key = md5( wp_json_encode( $theme_json_data ) );
Copy link
Member Author

Choose a reason for hiding this comment

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

Might be premature.

Looks like there's a small impact on performance on TT4

https://github.com/WordPress/wordpress-develop/actions/runs/11432958553?pr=7596

I'll run again as the tests vary between commits, e.g., https://github.com/WordPress/wordpress-develop/actions/runs/11432437736?pr=7596

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

Successfully merging this pull request may close these issues.

1 participant