Skip to content

Defer the editor script through the supported strategy arg - #190

Merged
roborourke merged 2 commits into
humanmade:developfrom
nlemoine:fix-enqueue-defer-arg
Oct 19, 2026
Merged

roborourke merged 2 commits into
humanmade:developfrom
nlemoine:fix-enqueue-defer-arg

Conversation

@nlemoine

@nlemoine nlemoine commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

enqueue_assets_for_post() passes 'defer' => true in the $args array of wp_enqueue_script(). That key has never been supported, so the script was never actually deferred. Since WordPress 7.0, unknown keys also trigger a _doing_it_wrong() notice on every block editor load:

wp_enqueue_script was called incorrectly. Unrecognized key(s) in the $args param: defer. Supported keys: strategy, in_footer, fetchpriority, module_dependencies (This message was added in version 7.0.0.)

This switches to 'strategy' => 'defer', available since WordPress 6.3. The script only calls registerPlugin(), and deferred scripts run before DOMContentLoaded, so before the editor is initialised.

Tests enqueue the editor assets and check that no incorrect usage notice is raised and that the printed script tag has the defer attribute. The PHPUnit workflow doesn't build the JS, so the test creates a minimal build/index.asset.php when it is missing and removes it afterwards. Both tests fail against the previous code on WP 7.1, and the deferred check fails on WP 6.9 if the key is removed.

@roborourke roborourke left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the intention was always to defer it (and note there might have been some other code in a project or elsewhere making use of this arg overload) then it'd be good to check it works with that fix and to defer through the proper WP system, rather than ignore it altogether.

A PHP test is probably overkill for this change, but it doesn't hurt :)

WordPress never recognised a 'defer' key in the $args array of wp_enqueue_script(), so the script was never deferred. Since 7.0 core also reports unknown keys with _doing_it_wrong(). Use 'strategy' => 'defer', available since WordPress 6.3.
@nlemoine
nlemoine force-pushed the fix-enqueue-defer-arg branch from 2cb0ab4 to fee0b4e Compare October 16, 2026 16:17
@nlemoine nlemoine changed the title Remove unsupported defer key from wp_enqueue_script args Defer the editor script through the supported strategy arg Oct 16, 2026
@nlemoine

Copy link
Copy Markdown
Contributor Author

Good point, the intention was clearly to defer it. Switched to 'strategy' => 'defer', and the test now checks that the printed script tag has the defer attribute (it fails on both 6.9 and 7.1 without the key). The script only calls registerPlugin(), so deferring it doesn't change anything for the editor init.

@nlemoine
nlemoine requested a review from roborourke October 16, 2026 16:22
@roborourke
roborourke merged commit 78ec391 into humanmade:develop Oct 19, 2026
10 checks passed
@roborourke roborourke mentioned this pull request Oct 19, 2026
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