perf: cache block metadata to avoid parsing block.json on every request - #101
perf: cache block metadata to avoid parsing block.json on every request#101mvdhoek1 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
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.phpmetadata 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.phpfile 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.
There was a problem hiding this comment.
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
| * @return string[] Block folder names. | ||
| */ | ||
| public function isDynamicBlock($blockName): bool | ||
| private function getBlockNames(string $blocksPath): array |
There was a problem hiding this comment.
Deze hele functie kan weg als je het op deze manier registreerd: https://github.com/WordPress/gutenberg/blob/trunk/docs/getting-started/fundamentals/registration-of-a-block.md#registering-a-metadata-collection-separately-wordpress-67
Alles in registerBlocks houden
1b489a9 to
95f7302
Compare
There was a problem hiding this comment.
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.jsvia apostbuildhook, but the repo actually generates it viawp-scripts ... --blocks-manifest(and there is nopostbuildscript orbin/generate-blocks-manifest.js). Please align the PR description (or add the missing generator/hook) so future maintainers know the real source ofbuild/blocks-manifest.php.
"build": "wp-scripts build --blocks-manifest",
| wp_register_block_metadata_collection($blocksPath, $manifestPath); | ||
| } | ||
|
|
||
| $blockNames = array_map('basename', array_filter(glob($blocksPath . '*', GLOB_ONLYDIR))); |
95f7302 to
ae605c0
Compare
There was a problem hiding this comment.
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 likecollapse-item,tabs-item, etc.), but the computed class name/namespace segment contains-, which can never be a valid PHP class. This causes repeatedclass_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.jsand a postbuild hook, but the repository doesn’t contain abin/directory/script andpackage.jsononly adds the--blocks-manifestflag. 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"
},
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 buildvia 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.