Skip to content

perf: cache block metadata to avoid parsing block.json on every request - #101

Open
mvdhoek1 wants to merge 1 commit into
mainfrom
perf/cache-block-metadata
Open

perf: cache block metadata to avoid parsing block.json on every request#101
mvdhoek1 wants to merge 1 commit into
mainfrom
perf/cache-block-metadata

Conversation

@mvdhoek1

@mvdhoek1 mvdhoek1 commented Aug 4, 2026

Copy link
Copy Markdown

Registers a pre-generated blocks-manifest.php via wp_register_block_metadata_collection() so register_block_type() reads metadata from one in-memory array instead of reading and JSON-decoding every block.json from disk on every request.
The manifest is generated by bin/generate-blocks-manifest.js, wired into npm run build via a postbuild hook.

Also drops the per-block glob() check in isDynamicBlock() in favor of the class_exists() check getRenderCallback() already does, removing another 15 filesystem calls per request.

After this change, the block metadata lookup completes in 0.0010 ms, compared to 1.816 ms before.

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 improves Gutenberg block registration performance by using a pre-generated PHP manifest for block metadata, avoiding repeated filesystem reads and JSON parsing of each block.json on every request.

Changes:

  • Register a blocks-manifest.php metadata collection (when available) and derive block folder names from it, falling back to directory scanning for older builds.
  • Simplify block registration by always computing an optional render callback and passing it to register_block_type() when present.
  • Add a post-build step to generate the build/Blocks/blocks-manifest.php file from built block folders.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/PluginServiceProvider.php Registers block metadata collection from a generated manifest and streamlines block registration/render-callback selection.
package.json Adds a postbuild hook to generate the block metadata manifest automatically after builds.
build/Blocks/blocks-manifest.php Generated manifest containing pre-parsed block metadata as a PHP array to avoid per-request JSON parsing.
bin/generate-blocks-manifest.js Node script that scans built blocks and generates the PHP manifest consumed at runtime.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/PluginServiceProvider.php Outdated

@YvetteNikolov YvetteNikolov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Kijk eerst even naar --blocks-manifest hier https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/

Dit genereert een PHP file die de block metadata van alle block.json files in het project zet. Dus je moet in de package.json deze flag toevoegen:

"start": "wp-scripts start --blocks-manifest"
"build": "wp-scripts build --blocks-manifest",

En het ge-outputte PHP bestand gebruiken. bin/generate-blocks-manifest.js en postbuild mogen weg

Comment thread src/PluginServiceProvider.php Outdated
* @return string[] Block folder names.
*/
public function isDynamicBlock($blockName): bool
private function getBlockNames(string $blocksPath): array

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Suppressed comments (1)

package.json:9

  • PR description says the manifest is generated by bin/generate-blocks-manifest.js via a postbuild hook, but the repo actually generates it via wp-scripts ... --blocks-manifest (and there is no postbuild script or bin/generate-blocks-manifest.js). Please align the PR description (or add the missing generator/hook) so future maintainers know the real source of build/blocks-manifest.php.
		"build": "wp-scripts build --blocks-manifest",

Comment thread src/PluginServiceProvider.php Outdated
wp_register_block_metadata_collection($blocksPath, $manifestPath);
}

$blockNames = array_map('basename', array_filter(glob($blocksPath . '*', GLOB_ONLYDIR)));

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (2)

src/PluginServiceProvider.php:123

  • getRenderCallback() is called for every block (including kebab-case names like collapse-item, tabs-item, etc.), but the computed class name/namespace segment contains -, which can never be a valid PHP class. This causes repeated class_exists() autoload attempts for blocks that can’t possibly be dynamic under the current naming scheme, adding avoidable overhead per request.
	public function getRenderCallback(string $blockName): ?callable
	{
		$nameSpacedClass = 'Yard\\Gutenberg\\Blocks\\' . $blockName . '\\' . ucfirst($blockName);

		if (class_exists($nameSpacedClass)) {
			$blockClass = new $nameSpacedClass;

package.json:19

  • PR description mentions generating the manifest via bin/generate-blocks-manifest.js and a postbuild hook, but the repository doesn’t contain a bin/ directory/script and package.json only adds the --blocks-manifest flag. Please align the PR description with the actual implementation (or add the referenced generator + postbuild hook).
	"scripts": {
		"build": "wp-scripts build --blocks-manifest",
		"lint:css": "yard-toolkit lint css -m custom './src/**/*.css'",
		"lint:js": "yard-toolkit lint js -m custom './src/**/*.js'",
		"lint:scss": "yard-toolkit lint scss -m custom './src/**/*.scss'",
		"format:css": "yard-toolkit format css -m custom './src/**/*.css'",
		"format:js": "yard-toolkit format js -m custom './src/**/*.js'",
		"format:scss": "yard-toolkit format scss -m custom './src/**/*.scss'",
		"packages-update": "wp-scripts packages-update",
		"plugin-zip": "wp-scripts plugin-zip",
		"start": "wp-scripts start --blocks-manifest"
	},

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.

3 participants