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

getEntityRecord issues a HTTP request when there's a cached result #24308

Closed
adamziel opened this issue Jul 31, 2020 · 5 comments
Closed

getEntityRecord issues a HTTP request when there's a cached result #24308

adamziel opened this issue Jul 31, 2020 · 5 comments
Labels
[Package] Core data /packages/core-data [Type] Bug An existing feature does not function as intended

Comments

@adamziel
Copy link
Contributor

adamziel commented Jul 31, 2020

Describe the bug

Consider the following function:

export function* getEntityRecord( kind, name, key = '' ) {
const entities = yield getKindEntities( kind );
const entity = find( entities, { kind, name } );
if ( ! entity ) {
return;
}
const record = yield apiFetch( {
path: `${ entity.baseURL }/${ key }?context=edit`,
} );
yield receiveEntityRecords( kind, name, record );
}

I would expect it to short-circuit if the entity is found, but it does the opposite which results in extra http queries.

For example, calling:

getEntityRecords("root", "widgetArea");
// ... wait for resolution ...
getEntityRecord("root", "widgetArea", "sidebar-1");

Will issue two http queries even if the entity with id sidebar-1 is already available.

CC @youknowriad

@adamziel adamziel added [Type] Bug An existing feature does not function as intended [Package] Core data /packages/core-data labels Jul 31, 2020
@youknowriad
Copy link
Contributor

I wonder if this is solved with #19498 which is close to land.

@adamziel
Copy link
Contributor Author

adamziel commented Aug 6, 2020

I wonder if this is solved with #19498 which is close to land.

It seems like it will! Let's confirm once it lands and then hopefully close this issue.

Also, in reply to #24290 (comment):

this change is problematic for me. entity will always be defined aside on the very first call so getEntityRecord won't do anything for all calls except the first one?

I may be missing something, but it would make sense to me to use the entity we already have (until the cache is invalidated) instead of re-requesting a new one each time. I don't get how it's short-circuiting when the entity is not found - shouldn't that be exactly when the resolution needs to happen?.

@youknowriad
Copy link
Contributor

I may be missing something, but it would make sense to me to use the entity we already have (until the cache is invalidated) instead of re-requesting a new one each time. I don't get how it's short-circuiting when the entity is not found - shouldn't that be exactly when the resolution needs to happen?.

I think you're confusing "Entity" and "Entity Record". "Entity" is just the config of the entity, we're resolving the record at this point.

@adamziel
Copy link
Contributor Author

adamziel commented Aug 6, 2020

Ah I see, it all makes sense now! Awesome, so #19498 will solve this.

@kevin940726
Copy link
Member

Since #19498 has been merged, this issue should be resolved now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Core data /packages/core-data [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

3 participants