Describe the bug
I was interested in finding out about Block patterns so started looking into the server side code.
I noticed that the 10 existing block patterns in lib/patterns are being registered for every server request, not just when they might be needed.
It would appear to me that the current API for registering, deregistering and rendering block patterns is rather inefficient.
It could benefit benefit from lazy loading, separating the rendering logic from the registration.
If the API is not improved, sooner rather than later, then I envisage significant bloat when plugins and themes start adding their own Block patterns and categories.
To reproduce
Steps to reproduce the behavior:
Install and activate Gutenberg
Visit your site
Notice that the pattern files are loaded by client-assets.php during plugin load.
Gutenberg doesn't even wait until 'plugins_loaded' or 'init'.
Expected behavior
Block patterns should only be loaded when the user has a need for them.
Patterns should only be rendered when actually required.
One solution would be to separate the rendering logic from the registration.
A lazy pattern would return the callback that needs to be invoked to return the content.
During rendering, if the content is null and the callback is set then the callback would be invoked to obtain the content.
The callback could be a file, a function/method or a combinaton ( file and method ).
This would enable the content to be rendered taking into account the context of the invocation.
For example:
a pattern could take into account the current post type. e.g. be structured one way for a post and another way for a page
a pattern could take into account the current user or the current post ID - similar to server side rendering.
There could also be some filtering of the content before it's returned to the client.
Screenshots
If applicable, add screenshots to help explain your problem.
Editor version (please complete the following information):
WordPress version: 5.4.2
Does the website has Gutenberg plugin installed, or is it using the block editor that comes by default? [e.g: "gutenberg plugin", "default"]
If the Gutenberg plugin is installed, which version is it? [e.g., 7.6] 8.3.0
Describe the bug I was interested in finding out about Block patterns so started looking into the server side code.
I noticed that the 10 existing block patterns in
lib/patterns
are being registered for every server request, not just when they might be needed.It would appear to me that the current API for registering, deregistering and rendering block patterns is rather inefficient. It could benefit benefit from lazy loading, separating the rendering logic from the registration.
If the API is not improved, sooner rather than later, then I envisage significant bloat when plugins and themes start adding their own Block patterns and categories.
To reproduce Steps to reproduce the behavior:
client-assets.php
during plugin load. Gutenberg doesn't even wait until 'plugins_loaded' or 'init'.Expected behavior
One solution would be to separate the rendering logic from the registration. A lazy pattern would return the
callback
that needs to be invoked to return thecontent
. During rendering, if the content is null and the callback is set then the callback would be invoked to obtain the content. The callback could be a file, a function/method or a combinaton ( file and method ).This would enable the content to be rendered taking into account the context of the invocation.
For example:
There could also be some filtering of the content before it's returned to the client.
Screenshots If applicable, add screenshots to help explain your problem.
Editor version (please complete the following information):
Additional context