Repository navigation
Command Palette: Make every admin screen faster by loading the palette only when it is opened - #15871
rene-hermenau wants to merge 7 commits into
Conversation
Every admin screen enqueued `wp-commands` and `wp-core-commands`, which pull in about 40 block editor scripts and the `wp-components` stylesheet before `DOMContentLoaded`, although the palette is only needed once it is opened. Admin screens now enqueue a small loader instead. The palette's scripts and styles are printed into an inert template in the footer and added to the page on the first Ctrl/Cmd+K or click on the toolbar button. Screens that already load the commands package, such as the block editor, keep loading the palette up front, as does `wp_enqueue_command_palette_assets()`. On `options-general.php` this drops the scripts on the page from 80 to 39 and the median DOMContentLoaded with a warm cache from 127 ms to 57 ms. See #66642.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
A side effect of this PR is that plugins like User Switching that register their commands behind checks like this will find their commands missing from the palette: The PR also exposes an existing bug in the Fresh admin color scheme, where styles don't look correct until the command palette is invoked. I'm not seeing this in Playground, but I see it locally using To reproduce:
I asked Claude to debug this, and it found the following: |
Plugins that add commands to the palette, such as User Switching, enqueue their script only when `wp_script_is( 'wp-commands', 'enqueued' )` is true. Since the palette is now loaded on demand on classic screens, that check is false there and their commands were missing. The new `enqueue_command_palette_assets` action fires wherever the palette's assets are enqueued: right away on screens that load the palette up front, and in the admin footer on screens that load it on demand, where whatever is enqueued on it is printed into the palette's template and loaded together with the palette. `wp_enqueue_command_palette_assets()` now enqueues `wp-commands` and lets `wp_enqueue_command_palette_loader()` take the up-front path, so the action fires for its direct callers too. See #66642.
|
Thanks for testing this so carefully! Plugin commands (User Switching): Good catch. 8b749f6 adds an add_action( 'enqueue_command_palette_assets', array( $this, 'action_enqueue_command_palette_script' ) );I checked it with a small mu-plugin that registers a command this way: on Settings > General the command shows up after Ctrl/Cmd+K and runs, and in the post editor it is there up front. New unit tests cover both paths. Plugins that keep the old check will still miss classic screens until they switch to the action; there is no way to keep that check true without loading the whole palette again. Fresh color scheme: I could not reproduce this on current trunk. The |
|
@rene-hermenau confirmed I had an outdated build. That part looks good after rebuilding. |
Adds command-palette-loader.js to the files checked by tsc and fixes the type errors it reports: promise resolvers wrapped for event handlers, the template element and style nodes typed, script URLs checked for null, and the click target checked to be an Element.
…while it loads. Opening the palette for the first time downloaded its scripts at that moment, which took about 1.8 seconds on a Fast 4G connection, with nothing on screen to show that anything was happening. The loader now prefetches the palette's scripts and styles once the page has loaded and gone idle, without running them, so the first open only has to run them. While the palette is being added to the page, the search icon in the toolbar button turns into a spinner, the button is marked busy, and screen readers hear that the palette is loading. Measured on Fast 4G, median of 8 rounds: the first open takes 248 ms instead of 1,807 ms (687 ms instead of 2,076 ms with a 4x CPU slowdown). Total Blocking Time stays at 0 ms on the Dashboard and on Settings > General. Follow-up to [61022]. See #66642.
|
@westonruter Updated after your review comment on the loader:
I kept it to a prefetch rather than inserting the scripts on idle, since running them on every screen would bring back the blocking time. Fast 4G, empty cache, nothing prefetched (worst case): 1-empty-cache.mp4Fast 4G, palette prefetched while the page was idle: 2-idle-prefetch.mp4 |
eb8420d to
6822400
Compare
But if it happens on idle, then blocking time is not going to be a problem, is it? |
The loader now remembers in the user settings that someone has opened the Command Palette. From then on, the palette's scripts are added to the page once it is idle, so it opens right away. Everyone else still only gets the scripts prefetched, so their pages do no extra work and hold no extra memory after load. A shortcut or toolbar click while the scripts are still being added waits for them and opens the palette once. On Fast 4G with a warm cache, the palette opens in 10 ms instead of 196 ms for someone who has used it (33 ms instead of 584 ms with a 4x CPU slowdown).
|
@westonruter Right, it does not touch the blocking time during page load. I measured what running it on idle costs instead, and it depends on the machine. Settings screens with a warm cache, Fast 4G, median of 6 runs, clicking the page heading every 200 ms for 6 s after
¹ What this PR did until now, and still does for anyone who has never opened the palette. So on a fast machine you are right: running it on idle costs almost nothing, and the palette opens instantly. On a slower one, every admin page view gets tasks of about 230 to 270 ms right after load, and a click that lands in one waits up to about 200 ms. Each tab also holds about 18 MiB more JS heap. Both sides have a point, so I combined them in the latest commit: the loader remembers in the user settings ( As targets I used the usual response time limits: about 100 ms for a reaction to feel instant, and about 1 s before it breaks someone's flow (Nielsen, RAIL). People who use the palette now get it in 10 to 33 ms, well under the first limit. Everyone else sees the spinner right away and gets the palette in about 0.2 to 0.6 s, under the second. That happens once per user: after the first open, the palette is ready on every screen. Compared to trunk, this takes nothing away from anyone. People who use the palette get it as quickly as today after one slower first open, and their pages no longer wait for it while loading. Everyone else, the majority of users, gets admin screens that are ready about twice as fast and hold about 18 MiB less memory per tab. Since it is a user setting, it also follows the user to other browsers. |
The loader calls getUserSetting() and setUserSetting() from utils.js, which the TypeScript dom lib does not know.
Good idea 👍 |
This makes wp-admin pages ready about twice as fast for everyone who works in the dashboard, by loading the Command Palette only when someone actually opens it instead of on every page.
On a Settings screen, the page becomes usable in 57 ms instead of 127 ms with a warm cache, and in 299 ms instead of 910 ms on a simulated slower device, with less than half the scripts. The palette works exactly as before: Ctrl/Cmd+K and the toolbar button still open it, and the editors are unchanged.
Why
Since 6.9, every admin screen enqueues
wp-commandsandwp-core-commands, which pull in about 40 block-editor scripts (React,wp-components,wp-block-editor,wp-core-data, …) and thewp-componentsstylesheet, although the palette is only needed after Ctrl/Cmd+K or a click on its toolbar button.How
wp_enqueue_command_palette_loader()replaceswp_enqueue_command_palette_assets()onadmin_enqueue_scripts. It builds the same settings and menu commands, attaches theinitializeCommandPalette()call towp-core-commands, and enqueues only the new 2.4 KB (minified)wp-command-palette-loaderscript.wp_print_command_palette_assets_template()prints the palette's not-yet-printed styles and scripts, with their inline scripts and translations, into an inert<template>in the footer, usingWP_Styles::do_items()/WP_Scripts::do_items().setUserSetting()) that someone has opened the palette. For them, it adds the palette to the page once it is idle instead of only prefetching it, so it opens right away. Everyone else keeps the prefetch, with no extra work or memory after load. A shortcut or click while the palette is still being added waits for it and opens it once.wp-commands(post editor, site editor) keep loading the palette up front.wp_enqueue_command_palette_assets()still loads everything up front for direct callers such as the Font Library and Connectors pages.enqueue_command_palette_assetsaction lets plugins add their commands without checkingwp_script_is( 'wp-commands', 'enqueued' ), which is now false on classic screens. It fires right away where the palette loads up front, and in the footer where it loads on demand; what is enqueued on it goes into the template and loads together with the palette.onclick(which needswp.data); the loader handles the click.wp-commandsstylesheet, since admin screens no longer load it up front.Measured on the local env,
wp-admin/options-general.php,SCRIPT_DEBUGoff, headless Chromium, 8 rounds, median DOMContentLoaded:<script src>on the pageThe first open of the palette takes about 200 ms on Fast 4G once the prefetch is done (about 600 ms with a 4× CPU slowdown). Without the prefetch it takes about 1.8 s, with the spinner showing. This slower first open happens once per user: after that, the palette is added while the page is idle and opens in 10 ms (33 ms with a 4× CPU slowdown). Full numbers and method are in the ticket and in the PR comments.
What the first open looks like
Fast 4G, empty cache, nothing prefetched (worst case):
1-empty-cache.mp4
Fast 4G, palette prefetched while the page was idle:
2-idle-prefetch.mp4
Tests: new
tests/phpunit/tests/dependencies/wpCommandPaletteLoader.php, updatedwpPrefetchAdminAssets.php, and a new Playwright spectests/e2e/specs/command-palette.test.js.Trac ticket: https://core.trac.wordpress.org/ticket/66642
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Implementation, tests, measurements and the ticket text, under my direction; I reviewed the change and take responsibility for it.
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.