Repository navigation
Closed
Labels
[Feature] Block APIAPI that allows to express the block paradigm.[Package] Block editor/packages/block-editor[Type] Tracking IssueTactical breakdown of efforts across the codebase and/or tied to Overview issues.
Description
Description
There is still some room for improvements after WordPress 5.8 release that brings some new exciting features to Block API listed in dev notes:
- https://make.wordpress.org/core/2021/06/23/block-api-enhancements-in-wordpress-5-8/
- https://make.wordpress.org/core/2021/07/01/block-styles-loading-enhancements-in-wordpress-5-8/
Based on the feedback received during the implementation and after the dev notes were published we created some follow-up tasks.
Planned tasks
- Allow more than one stylesheets per block for both
editorStyleandstyle - Allow more than one script per block for
editorScript,script, andviewScript - Add
viewScript- fronted only script support for the block type - Add
viewStyle- frontend only multiple styles support for the block type- Gutenberg
- WP core
- Document new APIs:
-
viewScriptinblock.jsonBlocks: Add view script to block schema and docs #36175 - multiple styles per type allowed in
block.json - multiple scripts per type allowed in
block.json
-
Reacted by Will Skora, Cory Hughart, Amir M. Mohamadi, Luis Herranz and Boyan Bratvanov
Reacted by Ari Stathopoulos, George Mamadashvili, Nik Tsekouras, Amir M. Mohamadi and Ross Morsali
Metadata
Metadata
Labels
[Feature] Block APIAPI that allows to express the block paradigm.[Package] Block editor/packages/block-editor[Type] Tracking IssueTactical breakdown of efforts across the codebase and/or tied to Overview issues.
Activity
[-]Block Editor: Further improve assets handling for blocks[/-][+]Blocks: Further improve assets handling for blocks[/+]Mamaduka commentedon Jul 20, 2021
Hi, @gziolo
Is this the correct PR for the
viewScript- #32814?gziolo commentedon Jul 20, 2021
There was another PR - #32977. It's all based on the work started by @vcanales in #30341 :)
gziolo commentedon Oct 7, 2021
Support for multiple style assets per type landed in Gutenberg: #32510
rmorse commentedon Jan 21, 2022
Just wanted to weigh in on this and add my support for allowing multiple scripts for both view and block.
There are two use cases that spring to mind:
1. Using regular JS libraries + packages (that are not package manager compatible)
This is my current situation - essentially, I have frontend scripts + css, and they expose an api on the window object, allowing you to generate various types of content to place anywhere on a page (using the api).
In admin / my block, rather than rewriting these scripts, I load both the CSS + JS "as is" (with some warnings, but it works) and interact with the api defined on the global window object. I then generate the various content from the api, and place them into the preview area of my block.
I think, while this is quite a specific use case, it also highlights a much broader requirement - as this could apply to any libarary / package which can't be loaded as a dependency via a package manager, or if there are scripts that we might want to share and re-use.
2. Managing admin assets more efficiently
This is not my current use case, but can see how it would be handy. Lets say I wanted to build a component library, that we'll register and share across multiple plugins and admin screens - then it would be handy to require the shared library as and when needed - and only load extra scripts if our block is going to be loaded - we could then expose that as a global that can be required by each plugin (much like gutenberg does with wp.element etc)
I'm sure its hardly a common use case, but I think it would be great to have the flexibility to do this kind of thing
fabiankaegy commentedon Jan 21, 2022
I'm on a similar page as @rmorse here. The most common reason for having to resort to php to register the script or style handle is because I need to add additional scripts to the dependency array.
gziolo commentedon Jan 21, 2022
@rmorse and @fabiankaegy, thank you for the feedback and for reassuring us that this functionality is going to be helpful. @aristath, do you plan to iterate on #36176 during WordPress 6.0 release cycle?
11 remaining items
jeremyfelt commentedon Feb 24, 2022
I've had a hard time tracking down why
viewScriptis ignored whenrender_callbackis used. Is that a necessary condition?If so, I could see it being worth adding a note in the
viewScriptschema documentation and/or its area of the documentation rather than in the Frontend Enqueuing section (Which I missed for way too long today figuring this out 😅).gziolo commentedon Feb 24, 2022
It was modeled after two existing core blocks that enqueue
viewScriptconditionally inrender_callback. It would be great to revisit now that plugins start using this API with their custom blocks.render_callbackfor the File block:gutenberg/packages/block-library/src/file/index.php
Lines 17 to 20 in d99a1f3
render_callbackfor the Navigation block"gutenberg/packages/block-library/src/navigation/index.php
Lines 388 to 393 in d99a1f3
I'm curious whether we could define some strategies for loading
viewScriptinblock.jsonas another config option in the proposal from @ocean90 in https://core.trac.wordpress.org/ticket/54018#comment:5, example:This could be a way to give the control to
render_callbackfor core blocks, but change the default always to enqueue the view scripts otherwise.At least for the whole WordPress 5.9 cycle, we should improve the documentation to make it harder to miss.
aristath commentedon Mar 23, 2022
Using something like the examples described above would make sense...
^ That seems like a nice format, and one we can extend in the future.
pathandhandlewould be the obvious default keys that we'd initially have to implement (handlein case we define the handle of an already loaded script instead of a path), and then we can add things likestrategyetc.Though tbh, I think
strategymight not be the right key in this case... Thinking ahead, strategy for most people will mean something different (see this proposal on the performance repo), and we may want to implement an actual loading strategy in the future 😅pluginslab commentedon Mar 31, 2022
There's a good demand for having more flexibility in enqueueing scripts, for both the editor and view scripts. Specially, the footer options. WooCommerce Blocks requires in some cases that editor scripts are loaded at the footer.
So the solution for now is to go back to PHP and do it "the old way". If the idea is to make it possible so that blocks.json dictates how scripts are loaded ideally, we should add support for that like @ocean90 proposed. I really like that approach, it makes things very clear and easier to implement, I would say.
gziolo commentedon Aug 19, 2022
@adamsilverstein, how is the work going on introducing strategies for loading scripts and styles? Once it's ready, I would be more than happy to look at integrating that with block assets in
block.jsonfile.gziolo commentedon Aug 19, 2022
I have a patch WordPress/wordpress-develop#3108 ready targeting WordPress core that adds multiple assets support for individual script types in
block.json:editorScript,script, andviewScript. It's also tracked in https://core.trac.wordpress.org/ticket/56408.Blocks: Allow registering multiple items for all supported asset types
Blocks: Allow registering multiple items for all supported asset types
Blocks: Allow registering multiple items for all supported asset types
gziolo commentedon Sep 14, 2022
Now that Allow registering multiple items for all supported asset types landed, we have achieved a good level of consistency. The only remaining task now is to document all changes and compile a dev note.
I consider this one nice to have, and it's being tracked in #41236, so I'm going to close this issue as resolved.
Blocks: Allow registering multiple items for all supported asset types
Blocks: Allow registering multiple items for all supported asset types