Skip to content

Editor: Discard script modules enqueued during REST API preloading - #13507

Open
sirreal wants to merge 10 commits into
WordPress:trunkfrom
sirreal:fix/64484-preload-restore-script-modules
Open

sirreal wants to merge 10 commits into
WordPress:trunkfrom
sirreal:fix/64484-preload-restore-script-modules

Conversation

@sirreal

@sirreal sirreal commented Sep 14, 2026

Copy link
Copy Markdown
Member

block_editor_rest_api_preload() renders the post for the context=edit preload, and WP_Block::render() enqueues view assets. The function restores $wp_scripts and $wp_styles afterwards (r52733) but not $wp_script_modules, so a block's viewScriptModule is printed in the editor.

This swaps copies of $post, $wp_scripts, $wp_styles, and $wp_script_modules in for the duration of the preload and restores the original instances, rather than restoring copies as r52733 does. Hooks bound to the original instances stay valid, which is what the script module print callbacks need. _wp_get_iframed_editor_assets() already does it this way.

Test: a block with a view script, style, and module. After preloading, the script and style are not queued, admin_print_footer_scripts does not print the module, and a module enqueued before preloading is still printed.

Trac ticket: https://core.trac.wordpress.org/ticket/64484

Follow-up to r52733 (#2312).

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Fable 5.1, Claude Opus
Used for: diagnosis, code, tests, description.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment thread src/wp-includes/block-editor.php Outdated
…lbacks.

`WP_Script_Modules::add_hooks()` registered every print callback as
`array( $this, 'method' )`, binding the callbacks to the instance that
was the global when `wp-settings.php` ran `add_hooks()` on
`after_setup_theme`. Replacing the `$wp_script_modules` global after that
point changed where `wp_enqueue_script_module()` wrote, but not what was
printed. Classic scripts and styles do not have this problem because
their hooks are procedural functions that read the global when they run.

Add procedural print functions that delegate to `wp_script_modules()` and
register those instead.

See #66100, #64484.
@sirreal
sirreal force-pushed the fix/64484-preload-restore-script-modules branch from 84566ea to b461889 Compare September 14, 2026 07:32
Comment thread src/wp-includes/class-wp-script-modules.php Outdated
`block_editor_rest_api_preload()` runs REST requests in-process, including the post with `context=edit`. Rendering the post content enqueues each block's view assets. The function already restores `$wp_scripts` and `$wp_styles` after preloading, so classic view scripts and view styles are discarded. It did not restore `$wp_script_modules`, so a block's `viewScriptModule` stayed enqueued and was printed in the block editor.

Back up and restore `$wp_script_modules` the same way. Depends on the script module print hooks reading the global instance (#66100).

See #64484, #55151.
@sirreal
sirreal force-pushed the fix/64484-preload-restore-script-modules branch from 1c1e0de to 7931499 Compare September 14, 2026 08:01
@sirreal

sirreal commented Sep 14, 2026

Copy link
Copy Markdown
Member Author

There's a potential follow-up to do the same with Interactivity API state, which could also be populated undesirably on preload.

@sirreal

sirreal commented Sep 14, 2026

Copy link
Copy Markdown
Member Author

`block_editor_rest_api_preload()` cloned each global into a backup variable, ran the preload against the original object, then assigned the clone to the global. The global ended up pointing at a different object than before the call, so anything bound to the original instance kept reading an object nothing enqueues into any more.

Swap copies in for the duration of the preload and restore the originals afterwards, which is what `_wp_get_iframed_editor_assets()` already does. Hooks and other references bound to the original instances stay valid, so the script module print callbacks registered by `WP_Script_Modules::add_hooks()` read the restored instance.

This removes the dependency on the script module print hooks resolving the global instance (#66100).

Follow-up to [52733].

See #64484, #55151.
@sirreal
sirreal marked this pull request as ready for review September 14, 2026 10:28
@github-actions

github-actions Bot commented Sep 14, 2026

Copy link
Copy Markdown

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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jonsurrell, westonruter.

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

@sirreal
sirreal requested a review from luisherranz September 14, 2026 16:52
Comment thread tests/phpunit/tests/blocks/editor.php Outdated
Comment thread tests/phpunit/tests/blocks/editor.php
$wp_scripts = $backup_wp_scripts;
$wp_styles = $backup_wp_styles;
// Restore the original $post, $wp_scripts, $wp_styles, and $wp_script_modules instances.
$post = $original_post;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If $original_post is not empty, should this call setup_postdata( $post ) too, potentially?

Suggested change
$post = $original_post;
$post = $original_post;
if ( $post ) {
setup_postdata( $post );
}

There are endpoints which call setup_postdata(), including these methods:

  • WP_REST_Posts_Controller::prepare_item_for_response()
  • WP_REST_Block_Renderer_Controller::get_item()
  • WP_REST_Revisions_Controller::prepare_item_for_response()

Granted, this is not strictly in the scope of what is being fixed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You mean that more globals were susceptible to modification and calling setup_postdata() here is part of a more complete fix?

I'm happy to make that change, it's not behavior I've observed but it seems reasonable.

@westonruter westonruter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A couple suggestions/questions, but these can be addressed later and aren't blocking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants