Fix prefetched resources are not used by ReferenceField#10717
Closed
fzaninotto wants to merge 3 commits intomasterfrom
Closed
Fix prefetched resources are not used by ReferenceField#10717fzaninotto wants to merge 3 commits intomasterfrom
fzaninotto wants to merge 3 commits intomasterfrom
Conversation
## Problem `<ReferenceField>` relies on `getManyAggregate` to call the data provider. Each instance will call a `getMany(resource, [id])`, that react-admin will not pass to the data provider, -but aggregate into a single `getMany(resource, [id1, id2, id3])`. The prefetch feature pre-populates the `getMany(resource, [id1, id2, id3])` cache, but this isn't enough to prevent the actual data provider query. ## Solution Prefetched records should be used to populate the cache of *individual* getMany queries.
fzaninotto
commented
Apr 30, 2025
| const onSettledEvent = useEvent(onSettled); | ||
|
|
||
| const result = useQuery<RecordType, ErrorType>({ | ||
| const result = useQuery<GetOneResult<RecordType>, ErrorType>({ |
Member
Author
There was a problem hiding this comment.
unfortunately, this is a breaking change: any other hook writing into the getOne cache (e.g., useUpdate) would need to be updated...
Member
Author
|
This can't be fixed in a backward compatible way. It will have to wait for the next major version. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The prefetch feature is implemented in the
dataProviderproxy. This means it executes when the data provider is actually called.But when going back to a page, react-query starts by returning stale data. This does not trigger the cache populate for prefetched records as the data provider isn't yet called.
In a page with ReferenceField (e.g. the comments list in the simple example), this leads to a strange effect: prefetching prevents the first query for reference posts the first time the page is loaded, but not on subsequent renders.
Solution
Cache prepopulation for prefetched records should be done in query hooks rather than in useDataProvider. That way, it will be triggered even for stale data.