Opened 16 months ago
Last modified 3 weeks ago
#63693 new defect (bug)
wp_should_load_block_assets_on_demand doing it wrong too early
| Reported by: | kkmuffme | Owned by: | |
|---|---|---|---|
| Priority: | highest omg bbq | Milestone: | Awaiting Review |
| Component: | Script Loader | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
wp_should_load_block_assets_on_demand() calls wp_is_rest_endpoint() which generally only works after parse_request hook (since that's where the constant gets defined) https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/script-loader.php#L2644 since it will always return false before that hook.
The same thing also for wp_should_load_separate_core_block_assets() https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/script-loader.php#L2609
However it's often called way too early, e.g. on "init" hook, e.g. in WooCommerce https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/src/Blocks/BlockTypesController.php#L632 and https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/src/Blocks/BlockTypesController.php#L633
Or in WP core itself, e.g. when calling wp_register_style( on "init", it will subsequently call wp_default_styles() and then the load block assets on demand function.
The same also in WP core register_block_type_from_metadata(, essentially all the stuff on init in "blocks" wp-includes, e.g. register_block_core_archives() on init
Or for example even in the file that's declaring those: https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/script-loader.php#L1798
I guess that all those "init" registered block things should be moved to run later on "parse_request" (or "wp") hook? Or alternatively, remove the "wp_is_rest_endpoint()" check since at the point in time it's called on "init", it's not ready yet and always returns false.
Possibly add a "doing_it_wrong" in wp_is_serving_rest_request for ! did_action( 'parse_request' )
Change History (4)
#2
@
9 months ago
Since 6.9.1 this now also happens for things that are not running on the wp_default_styles hook, e.g. register_block_core_video() from wp-includes/blocks/video.php
Should block registration/asset registration be moved later, e.g. on parse_request or wp, so that the rest context is actually known?
Yes
#3
in reply to: ↑ 1
@
7 weeks ago
- Priority normal → highest omg bbq
Replying to stevenoks:
Should block registration/asset registration be moved later, e.g. on parse_request or wp, so that the rest context is actually known?
Or instead, should the wp_is_rest_endpoint() check be adjusted/removed in these functions, since it can’t return meaningful results that early anyway?
Spamming Links
This ticket was mentioned in PR #14551 on WordPress/wordpress-develop by @shreya0shrivastava.
3 weeks ago
#4
- Keywords has-patch has-unit-tests added
Summary
wp_should_load_separate_core_block_assets() and wp_should_load_block_assets_on_demand() run early (init, wp_default_styles), before the main query exists, so their is_feed() check can't return a meaningful result.
This adds a private helper, _wp_is_non_frontend_request_for_block_assets(), used by both functions. It checks is_admin() and wp_is_rest_endpoint() as before, and only calls is_feed() once the wp action has fired.
REST requests still can't be detected at init, since REST_REQUEST isn't defined until parse_request. Fixing that would mean moving block registration later, which could break code that expects it on init.
Use of AI Tools
AI assistance: Yes
Tool(s): Clade code
Model(s): Sonnet 5.5
Used for: Initial code skeleton and test suggestions; final implementation and tests were reviewed and edited by me.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Should block registration/asset registration be moved later, e.g. on parse_request or wp, so that the rest context is actually known?
Or instead, should the wp_is_rest_endpoint() check be adjusted/removed in these functions, since it can’t return meaningful results that early anyway?