Skip to content

Command Palette: Make every admin screen faster by loading the palette only when it is opened - #15871

Open
rene-hermenau wants to merge 7 commits into
WordPress:trunkfrom
rene-hermenau:66642-command-palette-on-demand
Open

rene-hermenau wants to merge 7 commits into
WordPress:trunkfrom
rene-hermenau:66642-command-palette-on-demand

Conversation

@rene-hermenau

@rene-hermenau rene-hermenau commented Nov 10, 2026 •

Copy link
Copy Markdown

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-commands and wp-core-commands, which pull in about 40 block-editor scripts (React, wp-components, wp-block-editor, wp-core-data, …) and the wp-components stylesheet, although the palette is only needed after Ctrl/Cmd+K or a click on its toolbar button.

How

  • wp_enqueue_command_palette_loader() replaces wp_enqueue_command_palette_assets() on admin_enqueue_scripts. It builds the same settings and menu commands, attaches the initializeCommandPalette() call to wp-core-commands, and enqueues only the new 2.4 KB (minified) wp-command-palette-loader script.
  • 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, using WP_Styles::do_items() / WP_Scripts::do_items().
  • On the shortcut or a toolbar click, the loader preloads the template's scripts, inserts them in order, inserts the styles, and opens the palette. After that the palette's own shortcut handler takes over.
  • Once the page has loaded and gone idle, the loader prefetches the template's scripts and styles without running them, so the first open does not wait for the download.
  • The loader remembers in the user settings (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.
  • While the palette loads, the search icon in the toolbar turns into a spinner and screen readers hear that it is loading.
  • Screens that already load 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.
  • A new enqueue_command_palette_assets action lets plugins add their commands without checking wp_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.
  • The toolbar button drops its inline onclick (which needs wp.data); the loader handles the click.
  • The login screen no longer prefetches the wp-commands stylesheet, since admin screens no longer load it up front.

Measured on the local env, wp-admin/options-general.php, SCRIPT_DEBUG off, headless Chromium, 8 rounds, median DOMContentLoaded:

trunk this PR
<script src> on the page 80 39
empty cache 251 ms 97 ms
warm cache 127 ms 57 ms
empty cache, 4× CPU slowdown 910 ms 299 ms
warm cache, 4× CPU slowdown 452 ms 135 ms

The 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, updated wpPrefetchAdminAssets.php, and a new Playwright spec tests/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.

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.
@github-actions

github-actions Bot commented Nov 10, 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.

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

Props renehermi, westonruter, mindctrl.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@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.

@mindctrl

Copy link
Copy Markdown
Member

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: wp_script_is( 'wp-commands', 'enqueued' ). See user_switching::action_enqueue_command_palette_script(). So if you're on a Settings page or somewhere like that, the command will be missing. I wonder if a new action inside wp_print_command_palette_assets_template() would help.

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 wordpress-develop with this PR.

To reproduce:

  • change your admin color scheme to Fresh
  • navigate to Settings > General (or just refresh the Users > Profile screen.
  • notice the radio buttons, checkboxes, and button colors are not displayed properly.
  • open the Command Palette and see all the above things be displayed as they should be

I asked Claude to debug this, and it found the following:

admin-schemes.css sets --wp-admin-theme-color on body.admin-color-* for every scheme except fresh. Under Fresh, the variable only comes from the :root blocks in the wp-components and wp-commands stylesheets, which this PR no longer loads up front. Meanwhile forms.css, common.css, edit.css and others use var(--wp-admin-theme-color) with no fallback.

On options-general.php with Fresh:

┌─────────────────────────────┬─────────────────────────────────────────────────┬──────────────────┐
│                             │           Before opening the palette            │ After opening it │
├─────────────────────────────┼─────────────────────────────────────────────────┼──────────────────┤
│ --wp-admin-theme-color      │ unset                                           │ #007cba          │
├─────────────────────────────┼─────────────────────────────────────────────────┼──────────────────┤
│ Checked checkbox background │ transparent (the white checkmark can't be seen) │ rgb(0,124,186)   │
├─────────────────────────────┼─────────────────────────────────────────────────┼──────────────────┤
│ .button-primary             │ #3858e9 (from its fallback)                     │ #007cba          │
└─────────────────────────────┴─────────────────────────────────────────────────┴──────────────────┘

So the checkboxes look broken, and the page's colors change when the palette opens. The default modern scheme is unaffected. The real cause is the missing Fresh rule in base-styles; this PR just exposes it. That should be fixed first or alongside, either with a Fresh block in @wordpress/base-styles or a :root default in an admin stylesheet that every screen loads.

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.
@rene-hermenau

Copy link
Copy Markdown
Author

Thanks for testing this so carefully!

Plugin commands (User Switching): Good catch. 8b749f6 adds an enqueue_command_palette_assets action. It fires right away on screens that load the palette up front (block editor, site editor, direct callers of wp_enqueue_command_palette_assets()), and in the admin footer on screens that load it on demand. Whatever is enqueued on it, with its dependencies, localized data and translations, goes into the palette's template and loads together with the palette. A plugin can then drop the wp_script_is( 'wp-commands', 'enqueued' ) check:

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 base-styles/admin-schemes.css built from the pinned Gutenberg build (c035c1a) has body.admin-color-fresh { --wp-admin-theme-color: #007cba; }, and with Fresh selected on Settings > General, --wp-admin-theme-color on body is #007cba and .button-primary is rgb(0, 124, 186) both before and after opening the palette. Since Playground looks fine for you as well, could your local src/wp-includes/css/dist be from an older Gutenberg build? Running npm run gutenberg:download and then npm run build:dev should refresh it. If it still happens afterwards, I'd be glad to look again.

@mindctrl

Copy link
Copy Markdown
Member

@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.
@rene-hermenau

Copy link
Copy Markdown
Author

@westonruter Updated after your review comment on the loader:

  • Once the page has loaded and gone idle, the loader now prefetches the palette's scripts and styles without running them. On Fast 4G the first open takes 248 ms instead of 1,807 ms (687 ms instead of 2,076 ms with a 4× CPU slowdown). Total Blocking Time stays at 0 ms on the Dashboard and on Settings > General.
  • While the palette loads, the search icon in the toolbar button turns into a spinner, and screen readers hear "Loading the command palette." The spinner stays still when reduced motion is preferred.

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.mp4

Fast 4G, palette prefetched while the page was idle:

2-idle-prefetch.mp4

@rene-hermenau
rene-hermenau force-pushed the 66642-command-palette-on-demand branch from eb8420d to 6822400 Compare November 13, 2026 23:44
@westonruter

Copy link
Copy Markdown
Member

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.

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).
@rene-hermenau

Copy link
Copy Markdown
Author

@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 load:

Full CPU speed, prefetch ¹ Full CPU speed, run on idle ² CPU slowed down 4×, prefetch ¹ CPU slowed down 4×, run on idle ²
Blocking time until load 0 ms 0 ms 0 ms 0 ms
Longest task after load none 67 ms 65 ms 267 ms
Blocking time after load 0 ms 19 ms 15 ms 397 ms
Worst click delay after load 1 ms 1 ms 1 ms 117 ms
JS heap after GC 7 MiB 25.6 MiB 7 MiB 25.6 MiB
First open of the palette 196 ms 10 ms 584 ms 33 ms

¹ What this PR did until now, and still does for anyone who has never opened the palette.
² What the latest commit of this PR adds for anyone who has opened the palette before.

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 (setUserSetting()) that someone has opened the palette. From then on, the palette is added to the page once it is idle, so it opens right away for the people who actually use it. Everyone else only gets the prefetch, with no extra heap and almost no work after load.

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.
@westonruter

Copy link
Copy Markdown
Member

the loader remembers in the user settings (setUserSetting()) that someone has opened the palette. From then on, the palette is added to the page once it is idle, so it opens right away for the people who actually use it. Everyone else only gets the prefetch, with no extra heap and almost no work after load.

Good idea 👍

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.

3 participants