WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.47k stars 4.18k forks source link

Performance: Is Block Pattern registration being invoked unnecessarily? #23301

Open bobbingwide opened 4 years ago

bobbingwide commented 4 years ago

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:

  1. Install and activate Gutenberg
  2. Visit your site
  3. 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

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:

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

youknowriad commented 3 months ago

I think a lot of work has been done around pattern performance. So I wonder if this issue is relevant today.