Skip to content

Blocks: Further improve assets handling for blocks #33542

Closed

Description

@gziolo

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

Activity

  1. added
    [Feature] Block API
    [Package] Block editor
    [Status] In Progress
    on Jul 19, 2021
  2. added
    [Type] Tracking Issue
    on Jul 19, 2021
  3. changed the title [-]Block Editor: Further improve assets handling for blocks[/-] [+]Blocks: Further improve assets handling for blocks[/+] on Jul 19, 2021
  4. Mamaduka commented on Jul 20, 2021

    @Mamaduka
    Member

    Hi, @gziolo

    Is this the correct PR for the viewScript - #32814?

  5. gziolo commented on Jul 20, 2021

    @gziolo
    MemberAuthor

    Is this the correct PR for the viewScript - #32814?

    There was another PR - #32977. It's all based on the work started by @vcanales in #30341 :)

  6. gziolo commented on Oct 7, 2021

    @gziolo
    MemberAuthor

    Support for multiple style assets per type landed in Gutenberg: #32510

  7. moved this to Todo in WordPresson Jan 19, 2022
  8. rmorse commented on Jan 21, 2022

    @rmorse
    Contributor

    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

  9. fabiankaegy commented on Jan 21, 2022

    @fabiankaegy
    Member

    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.

  10. gziolo commented on Jan 21, 2022

    @gziolo
    MemberAuthor

    @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. 11 remaining items

  12. jeremyfelt commented on Feb 24, 2022

    @jeremyfelt
    Member

    I've had a hard time tracking down why viewScript is ignored when render_callback is used. Is that a necessary condition?

    If so, I could see it being worth adding a note in the viewScript schema 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 😅).

  13. gziolo commented on Feb 24, 2022

    @gziolo
    MemberAuthor

    I've had a hard time tracking down why viewScript is ignored when render_callback is used. Is that a necessary condition?

    It was modeled after two existing core blocks that enqueue viewScript conditionally in render_callback. It would be great to revisit now that plugins start using this API with their custom blocks.

    render_callback for the File block:

    $should_load_view_script = ! empty( $attributes['displayPreview'] ) && ! wp_script_is( 'wp-block-file-view' );
    if ( $should_load_view_script ) {
    wp_enqueue_script( 'wp-block-file-view' );
    }

    render_callback for the Navigation block"

    $has_old_responsive_attribute = ! empty( $attributes['isResponsive'] ) && $attributes['isResponsive'];
    $is_responsive_menu = isset( $attributes['overlayMenu'] ) && 'never' !== $attributes['overlayMenu'] || $has_old_responsive_attribute;
    $should_load_view_script = ! wp_script_is( 'wp-block-navigation-view' ) && ( $is_responsive_menu || $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] );
    if ( $should_load_view_script ) {
    wp_enqueue_script( 'wp-block-navigation-view' );
    }

    I'm curious whether we could define some strategies for loading viewScript in block.json as another config option in the proposal from @ocean90 in https://core.trac.wordpress.org/ticket/54018#comment:5, example:

    "viewScript": [
        {
          "path": "file:./view.js",
          "strategy": "render_callback"
        }
    ]

    This could be a way to give the control to render_callback for core blocks, but change the default always to enqueue the view scripts otherwise.

    If so, I could see it being worth adding a note in the viewScript schema 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 😅).

    At least for the whole WordPress 5.9 cycle, we should improve the documentation to make it harder to miss.

  14. aristath commented on Mar 23, 2022

    @aristath
    Member

    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.
    path and handle would be the obvious default keys that we'd initially have to implement (handle in case we define the handle of an already loaded script instead of a path), and then we can add things like strategy etc.
    Though tbh, I think strategy might 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 😅

  15. pluginslab commented on Mar 31, 2022

    @pluginslab

    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.

    CleanShot 2022-03-31 at 14 38 13@2x

    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.

  16. gziolo commented on Aug 19, 2022

    @gziolo
    MemberAuthor

    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.

    @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.json file.

  17. gziolo commented on Aug 19, 2022

    @gziolo
    MemberAuthor

    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, and viewScript. It's also tracked in https://core.trac.wordpress.org/ticket/56408.

  18. gziolo commented on Sep 14, 2022

    @gziolo
    MemberAuthor

    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.

    Add viewStyle - frontend only multiple styles support for the block type

    I consider this one nice to have, and it's being tracked in #41236, so I'm going to close this issue as resolved.

  19. removed
    [Status] In Progress
    on Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

[Feature] Block API[Package] Block editor[Type] Tracking Issue

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions