Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce requests in post picker #575

Open
mboynes opened this issue Feb 21, 2024 · 1 comment
Open

Reduce requests in post picker #575

mboynes opened this issue Feb 21, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@mboynes
Copy link
Contributor

mboynes commented Feb 21, 2024

Description

In the normal flow of the post picker,

  1. The user presses "Select"
  2. The post picker modal opens and runs a search, which fires a REST request to the search endpoint (e.g. /wp-json/wp/v2/search?type=post&subtype%5B0%5D=post&subtype%5B1%5D=page&page=1&_embed=1&exclude=&_locale=user
  3. The user selects a post
  4. The post picker uses usePostById() which will then take the post ID, and run another REST API search to get the post type (e.g. /wp-json/wp/v2/search?include=150&_locale=user)
  5. usePostById() uses usePost(), which calls getEntityRecord(), which will then make a REST API request to get the post object (e.g. /wp-json/wp/v2/posts/150?context=edit&_locale=user).
  6. The preview of the post is rendered, by default with the thumbnail, title, and post type. However, the previewRender prop could be passed to render more data.

In most cases, the data we get back from the search in step 2 provides everything we need throughout the rest of the process, and the requests in step 4 and 5 are unnecessary. At the absolute least, the request in step 5 should be unnecessary during this flow, as the post type is included in the response in step 2.

What can we do to refactor this code to make this process more efficient?

Use Case

Normal use of the post picker component.

@mboynes mboynes added the enhancement New feature or request label Feb 21, 2024
@kevinfodness
Copy link
Member

We should be able to cache data returned by the picker modal so that it doesn't need to follow up for more info. usePostById is necessary when rendering the field without first picking it from the modal, because we're only storing the post ID, and we need to get info about the post based on the ID without knowing its type (which the API requires in order to fetch a full object).

Other thoughts:

  1. Can we use just what's returned from the search endpoint generally? That could potentially work for both a situation where the post is selected from the picker and for where we only have the ID - maybe we don't need to fetch the full entity record for a post at all.
  2. If we're using a hook like usePostById we need to obey the rules of hooks, so we may need to get creative with how we're storing information to prevent additional requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants