-
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
Blocks: Further improve assets handling for blocks #33542
Comments
Support for multiple style assets per type landed in Gutenberg: #32510 |
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 |
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. |
@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? |
I noticed that the Core enqueues the files here. This check only does not enqueue the script if the block has a render callback. There are only two core blocks, file and navigation, which have a view script but also a render callback. So core doesn't seem to be affected directly. I think that this might be caused by the REST API preloading which is also rendering blocks and thus the Edit: I checked the debug backtrace and it's indeed the preloading: Debug backtrace for WP_Block::render()
Edit 2: A possible fix for this in WordPress/wordpress-develop#2312. I think this will speed-up the the editor for many users which have shortcodes or even blocks with complex frontend scripts. |
Great debugging. I think that makes sense. I initially thought we could tweak the rendering to avoid processing assets: However, what you proposed should have a similar result.
Still, it makes me wonder if we need to render blocks while preloading API requests? Do we need to send rendered HTML for blocks in the first place? Maybe we could look into some improvements in that aspect, too. |
I thought about that too but I didn't want to put any further logic into the render method which is most of the time not necessary.
Some blocks may display the actual content of a post and then it would make sense to have the rendered content instead of some HTML markup. This also not only applies to the post editor but also to the site and widget editors. I have moved this issue to Trac: https://core.trac.wordpress.org/ticket/55151 |
|
I opened https://core.trac.wordpress.org/ticket/54018, which has a PR. I'm not sure if it's best as a ticket on Trac or issue here. |
Thank you for opening a ticket on Trac. Let's discuss the details there because we need to apply changes in WordPress core anyway. Once we have a working solution we can see if we can use hooks to bring the same functionality to the Gutenberg plugin. I’m not aware of any prior discussion about changing the default flag in |
Just seeking clarification on how we currently define dependencies for styles and scripts in block.json. In the handbook it mentions how to specify an object to do this:
This is related to supporting multiple styles and scripts in block.json but I'm not sure this has been implemented yet? Also, are there plans to add support for "viewStyle" in block.json? This would allow you to add style definitions for the frontend only. I currently have a block which has certain markup that is displayed only on the frontend, so I need a way to add frontend styles. |
Actually, it's very good timing you asked this question. There is a following line in the docs that explains further the usage:
Now that we discuss support for multiple files per asset type and a way to provide some customization to how they are enqueued, @ocean90 proposed we could embed that configuration also inside
There was no need so far so we ruled it out together with @aristath for v1. However, we'd love to hear more about the use case you have, @dgwyer. We assumed that styles are a bit different than scripts and you can achieve all goals throug |
Regarding array support for For example, for As for frontend only styles, I've come across the need for this on multiple occasions. Most recently I've been developing a simple slider block that doesn't render the final slider in the editor, it just allows you to add inner blocks to the slider block container. Only on the frontend do styles render the actual slider. I had a similar case for a simple FAQ block too. Plus, there are other times where you may need to just add a few extra styles here and there, which are relevant to the frontend only. At the moment there is no way to separate out frontend styles only. It's a little restrictive at times. |
It's a known limitation that we had to workaround in WordPress core, too: Once we allow passing additional configuration for individual assets, having a way to provide a list of dependencies is a great idea, too. The only question would be whether we are fine redeclaring all dependencies manually, or we create a way to inject extra dependencies with some special entry like "editorScript": [ {
"path": "file:./index.js",
"dependencies": [ "...", "jquery" ]
} ]
Thank you for sharing your examples. It's very helpful to learn about how people use the current API. I would love to see other people leaving their feedback as well. I'm also curious what @aristath thinks about it, as he spent a ton of time improving the existing handling for styles. |
I've had a hard time tracking down why If so, I could see it being worth adding a note in the |
It was modeled after two existing core blocks that enqueue
gutenberg/packages/block-library/src/file/index.php Lines 17 to 20 in d99a1f3
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 "viewScript": [
{
"path": "file:./view.js",
"strategy": "render_callback"
}
] This could be a way to give the control to
At least for the whole WordPress 5.9 cycle, we should improve the documentation to make it harder to miss. |
Using something like the examples described above would make sense... "viewScript": [
{
"path": "file:./view.js",
"strategy": "render_callback"
}
] ^ That seems like a nice format, and one we can extend in the future. |
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. |
@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 |
I have a patch WordPress/wordpress-develop#3108 ready targeting WordPress core that adds multiple assets support for individual script types in |
Follow-up #54337, [52069]. Part of WordPress/gutenberg#41236. More details in WordPress/gutenberg#33542. Allow passing more than one script per block for `editorScript`, `script`, and `viewScript` fields in the `block.json` metadata file. This aligns with the previously added changes for `style` and `editorStyle` fields. This change impacts the `WP_Block_Type` class and the REST API endpoint for block types. To ensure backward compatibiliy old names were soft deprecated in favor of new fields that work with array values and have `_handles` suffix. Props zieladam, dlh, timothyblynjacobs, aristath, bernhard-reiter. Fixes #56408. git-svn-id: https://develop.svn.wordpress.org/trunk@54155 602fd350-edb4-49c9-b593-d223f7449a82
Follow-up #54337, [52069]. Part of WordPress/gutenberg#41236. More details in WordPress/gutenberg#33542. Allow passing more than one script per block for `editorScript`, `script`, and `viewScript` fields in the `block.json` metadata file. This aligns with the previously added changes for `style` and `editorStyle` fields. This change impacts the `WP_Block_Type` class and the REST API endpoint for block types. To ensure backward compatibiliy old names were soft deprecated in favor of new fields that work with array values and have `_handles` suffix. Props zieladam, dlh, timothyblynjacobs, aristath, bernhard-reiter. Fixes #56408. Built from https://develop.svn.wordpress.org/trunk@54155 git-svn-id: http://core.svn.wordpress.org/trunk@53714 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Follow-up #54337, [52069]. Part of WordPress/gutenberg#41236. More details in WordPress/gutenberg#33542. Allow passing more than one script per block for `editorScript`, `script`, and `viewScript` fields in the `block.json` metadata file. This aligns with the previously added changes for `style` and `editorStyle` fields. This change impacts the `WP_Block_Type` class and the REST API endpoint for block types. To ensure backward compatibiliy old names were soft deprecated in favor of new fields that work with array values and have `_handles` suffix. Props zieladam, dlh, timothyblynjacobs, aristath, bernhard-reiter. Fixes #56408. Built from https://develop.svn.wordpress.org/trunk@54155 git-svn-id: https://core.svn.wordpress.org/trunk@53714 1a063a9b-81f0-0310-95a4-ce76da25c4cd
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. |
Follow-up #54337, [52069]. Part of WordPress/gutenberg#41236. More details in WordPress/gutenberg#33542. Allow passing more than one script per block for `editorScript`, `script`, and `viewScript` fields in the `block.json` metadata file. This aligns with the previously added changes for `style` and `editorStyle` fields. This change impacts the `WP_Block_Type` class and the REST API endpoint for block types. To ensure backward compatibiliy old names were soft deprecated in favor of new fields that work with array values and have `_handles` suffix. Props zieladam, dlh, timothyblynjacobs, aristath, bernhard-reiter. Fixes #56408. Built from https://develop.svn.wordpress.org/trunk@54155
Follow-up #54337, [52069]. Part of WordPress/gutenberg#41236. More details in WordPress/gutenberg#33542. Allow passing more than one script per block for `editorScript`, `script`, and `viewScript` fields in the `block.json` metadata file. This aligns with the previously added changes for `style` and `editorStyle` fields. This change impacts the `WP_Block_Type` class and the REST API endpoint for block types. To ensure backward compatibiliy old names were soft deprecated in favor of new fields that work with array values and have `_handles` suffix. Props zieladam, dlh, timothyblynjacobs, aristath, bernhard-reiter. Fixes #56408. git-svn-id: https://develop.svn.wordpress.org/trunk@54155 602fd350-edb4-49c9-b593-d223f7449a82
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:
Based on the feedback received during the implementation and after the dev notes were published we created some follow-up tasks.
Planned tasks
editorStyle
andstyle
editorScript
,script
, andviewScript
viewScript
- fronted only script support for the block typeviewStyle
- frontend only multiple styles support for the block typeviewScript
inblock.json
Blocks: Add view script to block schema and docs #36175block.json
block.json
The text was updated successfully, but these errors were encountered: