redux-toolkit icon indicating copy to clipboard operation
redux-toolkit copied to clipboard

RTK Query: Lazy query returns deprecated data while fetching the same data via tag invalidation

Open C3ntraX opened this issue 1 year ago • 14 comments

Describe the bug I found a bug regarding the combination of declarative and imperativ querying at the same time (Multiple subscribtions). If you execute a lazy query after an invalidation of the same data through which fetches the same data via data query, then the lazy query will not execute a request if "preferCached" is false.

This means that Lazyquery will ignore an ongoing fetch of the same data and returns deprecated data.

See the example, its hard to explain.

Isolated Example https://stackblitz.com/edit/react-ts-tkvbea?file=index.tsx,reduxRtk.tsx,customQuery.ts,App.tsx,package.json

To Reproduce Steps to reproduce the behavior:

  1. Go to the Sandbox and open App.tsx
  2. Open Console
  3. Click on Add button
  4. See: QueryData will get the new data; LazyQueryData hold the cached deprecated data and will ignore the running request. And this causes the LazyQuery to be logging the data before the query will finish.

What happens

  1. You click the "Add" button.
  2. This executes useAddDataMutation
  3. The Mutation invalidates all tags with type "Test"
  4. useGetDataQuery will request new data, because the tag got invalidated
  5. Imerative after step 2, the useLazyGetDataQuery will try to get the new data too.
  6. useLazyGetDataQuery won´t wait for the current query executed with useGetDataQuery and returns deprecated data; This happens if preferCachedData is false.

Expected behavior The lazy query should joining the ongoing request and wait for the request.

Additional context I need both ways to request data declaratively and imperatively. With the imerative way I do the printing of the data. So it's very important that it works somehow. I know to work around this, but to add an additional API that doesn't invalidate the "test" tag. This will prevent the problem, but yes.

C3ntraX avatar Oct 20 '22 19:10 C3ntraX