-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Load block styles, if and only if the block is present on the page #15256
Comments
One thing that might work here is to enqueue scripts (won't work for css I think) as a part of the render_callback logic for a block php side. Since that should only be called when the content contains that block - it should be feasible to enqueue any necessary scripts for that block in the callback. |
Yes please. I've now seen multiple blocks loading their own CSS/JS resources on every page view. Which is convenient for devs, but not sustainable at all for sites. |
I wrote up a post about Conditionally Loading Block Assets but that solution only works on a per block basis, and requires looping through all posts. So if every block does the same thing, and you have many blocks installed it will slow down a large index page. My thought on an ideal solution would be when a block registers an asset using Example register_block_type( 'gutenberg-examples/example-02-stylesheets', array(
'style' => 'gutenberg-examples-02',
'editor_style' => 'gutenberg-examples-02-editor',
'editor_script' => 'gutenberg-examples-02',
) ); |
Here's an example in Jetpack plugin of that mechanism.. It's quite flexible and thus ergonomic for developers. In addition to loading assets, it also intercepts the rendering in the AMP context. |
There's also another way to do it using the |
Can we combine this with the original issue at #5445 ? |
I think this can be closed now with the work done at #25220 I've installed and activated a couple plugins - ootb-openstreetmap and code-syntax-block and the javascript used in those blocks is only being loaded on the front-end when a page/post contains those blocks. |
Is your feature request related to a problem? Please describe.
I've noticed that block assets enqueued using
enqueue_block_assets
will be loaded on every page view, regardless if the block is present on that page.I can see this being a problem if someone loads a lot of plugins because they want numerous block types, not realizing that the additional assets are loaded on every view, regardless if the blocks are included.
The two plugins of mine that this is effecting are:
Describe the solution you'd like
Ideally, the editor automatically only loads the block assets that it needs when they are needed.
Describe alternatives you've considered
For my individual case, I noticed when I have an index page which does not load any post content, just the titles; all the assets are still loaded. I was able to check for
!is_single()
and dequeue the assets by hand. However, this is a hack and doesn't work for single post views that do not include the custom blocks, the additional assets are still loaded though not needed.The text was updated successfully, but these errors were encountered: