Skip to content

Editor: Avoid unnecessary term re-fetches in FlatTermSelector - #80623

Merged
Mamaduka merged 1 commit into
trunkfrom
optimize/flat-term-selector-queries
Jul 23, 2026
Merged

Editor: Avoid unnecessary term re-fetches in FlatTermSelector#80623
Mamaduka merged 1 commit into
trunkfrom
optimize/flat-term-selector-queries

Conversation

@Mamaduka

Copy link
Copy Markdown
Member

What?

PR makes two small optimizations to the flat term selector's getEntityRecords queries:

  • Sort included term IDs in the query key, so reordering the same set of terms no longer produces a new query and an unnecessary re-fetch.
  • Clear search on change (setSearch('')), so a stale search value doesn't trigger an extra search request after selecting/creating a term.
  • No behavior change.

Note: There's one more unwanted query for stale termIds, but it requires too much bookkeeping to fix.

Testing Instructions

  1. Open a post.
  2. Assign a tag and save.
  3. Try creating a new tag by typing a non-existing name and pressing Enter.
  4. Save again.
  5. Observe network requests.

Testing Instructions for Keyboard

Same.

Screenshots or screencast

Before After
CleanShot 2026-07-23 at 14 19 57 CleanShot 2026-07-23 at 14 16 27

Use of AI Tools

None.

@Mamaduka Mamaduka self-assigned this Jul 23, 2026
@Mamaduka Mamaduka added [Type] Performance Related to performance efforts [Package] Editor /packages/editor labels Jul 23, 2026
Comment on lines +90 to +93
// Sort ids so reordering alone doesn't produce a new query key and re-fetch.
include: _termIds?.length
? [ ..._termIds ].sort( ( a, b ) => a - b ).join( ',' )
: undefined,

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.

The getEditedPostAttribute can return term IDs in a different order after the post is saved.

@github-actions

github-actions Bot commented Jul 23, 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.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>

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

@Mamaduka
Mamaduka requested a review from tyxla July 23, 2026 10:28
@github-actions

Copy link
Copy Markdown

Size Change: +22 B (0%)

Total Size: 7.75 MB

📦 View Changed
Filename Size Change
build/scripts/editor/index.min.js 498 kB +22 B (0%)

compressed-size-action

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

LGTM, nice optimizations there!

Just a few minor questions before we 🚢

Comment on lines +91 to +93
include: _termIds?.length
? [ ..._termIds ].sort( ( a, b ) => a - b ).join( ',' )
: undefined,

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.

Should we maybe not provide include to the query object at all if it's undefined? I've seen include: '' before, so which one is the right thing to do?

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.

This matches previous types. Query isn't performed when there are no itemIds.

// Optimistically update term values.
// The selector will always re-fetch terms later.
setValues( uniqueTerms );
setSearch( '' );

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.

Can we confirm that this won't have timing problems with the debounced search?

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.

Can't really think of any practical cases. In theory, maybe 🤷 😅

@Mamaduka

Copy link
Copy Markdown
Member Author

Thanks for the review, @tyxla!

@Mamaduka
Mamaduka merged commit 8c607b4 into trunk Jul 23, 2026
55 of 61 checks passed
@Mamaduka
Mamaduka deleted the optimize/flat-term-selector-queries branch July 23, 2026 13:24
@github-actions github-actions Bot added this to the Gutenberg 23.7 milestone Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Editor /packages/editor [Type] Performance Related to performance efforts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Sponsor
SponsoredKunjungi sekarang
Promo