Skip to content

Conversation

@t-hamano
Copy link
Contributor

What?

I'm not sure why such complicated logic is used to enqueue the script module in the Accordion block. It should work by just running wp_enqueue_script_module.

Testing Instructions

Insert an Accordion block and confirm that it's interactive on the frontend.

@t-hamano t-hamano added the [Type] Code Quality Issues or PRs that relate to code quality label Sep 19, 2025
@t-hamano t-hamano added the [Block] Accordion Affects the Accordion Block label Sep 19, 2025
@t-hamano t-hamano marked this pull request as ready for review September 19, 2025 03:52
@github-actions
Copy link

github-actions bot commented Sep 19, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: t-hamano <[email protected]>
Co-authored-by: sirreal <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@t-hamano t-hamano self-assigned this Sep 19, 2025
@github-actions
Copy link

github-actions bot commented Sep 19, 2025

Flaky tests detected in 3c4f9b3.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/17857456722
📝 Reported issues:

);

wp_enqueue_script_module( '@wordpress/block-library/accordion' );
wp_enqueue_script_module( '@wordpress/block-library/accordion/view' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be in the viewScriptModule block.json instead of an enqueue here? That seems like the simplest and most direct approach:

"style": "wp-block-accordion"
}


Some blocks enqueue in their render callback because the enqueue is conditional.

I suppose this is conditional if $content is falsy, however when inserting an empty accordion block the module is enqueued, so that seems more like defensive coding than an optimization to conditionally enqueue the asset.


Aside: I noticed the field is not in the block.json handbook.

Copy link
Contributor Author

@t-hamano t-hamano Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simply defined the viewScriptModule field, but for some reason the src attribute is not correct:

src

I've looked into the code and can confirm that at least the path here is correct:

wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'], $args ); // The $args parameter is new as of WP 6.9 per <https://core.trac.wordpress.org/ticket/61734>.

Is there an error somewhere in this PR? By the way, there are no other core blocks that use the viewScriptModule field.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration should be handled elsewhere, rather than using local file: for the module, it can refer to the registered module ID (suggested here: #71742 (review)).

I believe the rest of the changes in this PR can then be reverted.

Sorry, I didn't mean to complicate this simplification 🙂

there are no other core blocks that use the viewScriptModule field.

I think that's because the conditionally enqueue, like in the image block that only enqueues the module if some attributes are present:

if (
isset( $lightbox_settings ) &&
'none' === $link_destination &&
isset( $lightbox_settings['enabled'] ) &&
true === $lightbox_settings['enabled']
) {
wp_enqueue_script_module( '@wordpress/block-library/image/view' );

@t-hamano t-hamano force-pushed the accordion/simplify-enqueue-script branch from 3d93db8 to 12b0304 Compare September 19, 2025 09:21
@t-hamano t-hamano force-pushed the accordion/simplify-enqueue-script branch from 12b0304 to 9a0aa26 Compare September 19, 2025 10:35
@t-hamano t-hamano force-pushed the accordion/simplify-enqueue-script branch from b1ad605 to 318be0a Compare September 19, 2025 11:04
@t-hamano t-hamano force-pushed the accordion/simplify-enqueue-script branch from 318be0a to 12b1034 Compare September 19, 2025 11:07
@t-hamano
Copy link
Contributor Author

@sirreal I suspect that the viewScriptModule field might not work in the Gutenberg plugin. This is because the script module file paths are different between core and Gutenberg.

It seems that 12b1034 fixed the problem commented here.

If this fix is ​​suitable, we can submit a separate PR to fix the script module itself. What do you think?

@t-hamano
Copy link
Contributor Author

The latest change doesn't output the script tag itself..

@t-hamano
Copy link
Contributor Author

Thank you, it works! However, the viewScriptModule field should support relative paths just like other properties, right?

@sirreal
Copy link
Member

sirreal commented Sep 19, 2025

Thank you, it works! However, the viewScriptModule field should support relative paths just like other properties, right?

It should (and I believe it does that correctly) however it's quite tricky in the case of the block library. The modules can exist in two different places and refer to a different file depending on whether Gutenberg has swapped its versions for the Core versions. In this case, with Core/Gutenberg modules, there are systems in place that handle registration and it's easier to refer to a registered module ID and not care about where it comes from.

Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and working for me. Nice simplification, thanks!

@t-hamano t-hamano merged commit 84e12bc into trunk Sep 19, 2025
77 of 78 checks passed
@t-hamano t-hamano deleted the accordion/simplify-enqueue-script branch September 19, 2025 12:53
@github-actions github-actions bot added this to the Gutenberg 21.8 milestone Sep 19, 2025
adamsilverstein pushed a commit to adamsilverstein/gutenberg that referenced this pull request Sep 22, 2025
* Accordion Block: Simplify script module enqueueing

* Use `viewScriptModule` field

* Replace build module path

* Change viewScriptModule field value and revert some changes

* Update viewScriptModule ID

---------

Co-authored-by: t-hamano <[email protected]>
Co-authored-by: sirreal <[email protected]>
adamsilverstein pushed a commit to adamsilverstein/gutenberg that referenced this pull request Sep 22, 2025
* Accordion Block: Simplify script module enqueueing

* Use `viewScriptModule` field

* Replace build module path

* Change viewScriptModule field value and revert some changes

* Update viewScriptModule ID

---------

Co-authored-by: t-hamano <[email protected]>
Co-authored-by: sirreal <[email protected]>
@sirreal sirreal mentioned this pull request Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Accordion Affects the Accordion Block [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants