Skip to content

Create parsed PHP version of core's theme.json at build time rather than during runtime #45616

@felixarntz

Description

@felixarntz

What problem does this address?

See https://core.trac.wordpress.org/ticket/56945 and WordPress/wordpress-develop#3536 (comment):

  • Parsing theme.json is a rather expensive process. Despite ongoing optimization efforts (e.g. https://core.trac.wordpress.org/ticket/56974), this can only get us so far. Especially for classic themes that do not even provide a theme.json, parsing WordPress core's theme.json is an unnecessary overhead.
  • As discussed in Fix wp_head performance regression for classic themes wordpress-develop#3536, for classic themes it is still necessary to use some of the data from core's theme.json, however given that no other theme.json needs to be parsed in that context, there would be a notable performance win (e.g. ~28% faster wp_head execution) from not even having to parse any theme.json file at runtime.

What is your proposed solution?

  • Instead of parsing the built-in theme.json during runtime in PHP, it should be parsed into a ready-to-use theme-json.php file as part of the Gutenberg / WP core build process.
  • The build process should render a PHP file returning an array with all the data from theme.json, including the relevant strings (based on theme-i18n.json) being wrapped in _x() calls.
  • This way, Gutenberg and WordPress core can simply require that PHP file rather than having to run the expensive logic to parse theme.json over and over again.
    • Potentially, to simplify working with theme.json during development, logic could be provided to still use the original theme.json decoding logic e.g. if WP_DEBUG is enabled or something like that.

This idea originally was proposed in WordPress/wordpress-develop#3536, but was then moved out of there so that the rest of the PR could be merged to make it into the WordPress 6.1.1 release. A previous version of the PR includes an example for that theme-json.php file (manually created using var_export(), please ignore the WPCS violations in there) - I'm linking this here just for reference, of course this file would only make sense to be generated automatically for a reasonable implementation, as proposed above.

Potentially, we could take this even further by making the build tool to create a PHP file from theme.json available to external theme developers as well. This way, theme developers could optionally adopt that tool and provide an already parsed theme-json.php for their own theme, so that the performance issue can be addressed more holistically.

Metadata

Metadata

Assignees

Labels

[Feature] Site EditorRelated to the overarching Site Editor (formerly "full site editing")[Type] EnhancementA suggestion for improvement.[Type] PerformanceRelated to performance efforts

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions