Skip to content

Fix prefetched resources are not used by ReferenceField#10717

Closed
fzaninotto wants to merge 3 commits intomasterfrom
fix-prefetch-getmany
Closed

Fix prefetched resources are not used by ReferenceField#10717
fzaninotto wants to merge 3 commits intomasterfrom
fix-prefetch-getmany

Conversation

@fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Apr 30, 2025

Problem

The prefetch feature is implemented in the dataProvider proxy. 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.

  • useGetOne
  • useGetList
  • useGetManyReference
  • useGetMany
  • mutations hooks?

## 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 fzaninotto added RFR Ready For Review WIP Work In Progress and removed RFR Ready For Review labels Apr 30, 2025
const onSettledEvent = useEvent(onSettled);

const result = useQuery<RecordType, ErrorType>({
const result = useQuery<GetOneResult<RecordType>, ErrorType>({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately, this is a breaking change: any other hook writing into the getOne cache (e.g., useUpdate) would need to be updated...

@fzaninotto
Copy link
Member Author

This can't be fixed in a backward compatible way. It will have to wait for the next major version.

@fzaninotto fzaninotto closed this Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WIP Work In Progress

Development

Successfully merging this pull request may close these issues.

1 participant

Comments