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

Rework infinite query forced checks #4854

Conversation

markerikson
Copy link
Collaborator

Previously in #4795 , I had made tweaks to the infinite query fetching logic to fix broken behavior (would not fetch next pages if the API definition had refetchOnMountOrArgChange: true). I did so by altering the logic to fall back to an empty infinite data set, using an internal arg.forceRefetch flag instead of isForcedQuery().

@agusterodin reported some more problems with similar scenarios in #4744 (comment). I did some investigating and concluded that this change resulted in a logic mismatch. We triggered an actual run of the thunk because condition used isForcedQuery, but we didn't use isForcedQuery to determine whether to use cached data or start fresh. That led to a refetch of the pages, but appended to the existing copy of the pages, resulting in duplicates.

After further tinkering, I ended up with isForcedQuery() && !arg.direction, as we don't pass in a direction flag when the hook switches to a different arg. That appears to handle both "can fetch with refetchOnMountOrArgChange active" and "does not duplicate the entries on refetch".

As for the other reported issue, where an arg change + refetch is quickly followed by a fetchNextPage() call: it looks like React Query intentionally cancels an in-flight query in that case. It seems like the refetch sequence continues, but I think that promise gets ignored. So, the additional page request goes out right away.

It might be possible to mimic that behavior with RTKQ, but our internal logic is different enough it's a bit trickier. Given that, I'm going to skip trying to match that edge case for now.

Copy link

codesandbox bot commented Feb 20, 2025

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit c9d6a4e:

Sandbox Source
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration
@examples-action-listener/counter Configuration
rtk-esm-cra Configuration

Copy link

size-limit report 📦

Path Size
1. entry point: @reduxjs/toolkit/query/react (modern.mjs) 14.42 KB (+0.22% 🔺)
1. entry point: @reduxjs/toolkit/query (cjs, production.min.cjs) 23.26 KB (-0.01% 🔽)
1. entry point: @reduxjs/toolkit/query/react (cjs, production.min.cjs) 25.64 KB (+0.02% 🔺)
2. entry point: @reduxjs/toolkit/query (without dependencies) (cjs, production.min.cjs) 10.04 KB (+0.05% 🔺)
3. createApi (.modern.mjs) 14.79 KB (+0.03% 🔺)
3. createApi (react) (.modern.mjs) 16.84 KB (+0.07% 🔺)

Copy link

netlify bot commented Feb 20, 2025

Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit c9d6a4e
🔍 Latest deploy log https://app.netlify.com/sites/redux-starter-kit-docs/deploys/67b7b1dff27fe70008d58235
😎 Deploy Preview https://deploy-preview-4854--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@markerikson markerikson merged commit 8a4ff3a into feature/infinite-query-integration Feb 20, 2025
88 checks passed
@aryaemami59 aryaemami59 deleted the feature/infinite-query-forcedquery-checks branch February 26, 2025 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant