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

Ensure upserted cache entries always get written #4768

Merged
merged 1 commit into from
Dec 14, 2024

Conversation

markerikson
Copy link
Collaborator

@markerikson markerikson commented Dec 14, 2024

This PR:

  • Fixes a bug with upsertQueryEntries where updates to an existing cache entry would not actually get completed, leaving the entry in an incorrect pending state indefinitely

Fixes #4749

Investigation

Made a Replay that shows the repro in action:

Looks like there was a small flaw in the logic. We already had a check for the fulfilled case that bails out of writing the result if substate.requestId !== meta.requestId, so that we don't end up with conflicting request results being written. However, it also had a check for the older forced upsert logic as well, and it wasn't accounting for this newer approach.

Meanwhile, the pending logic actually keeps around the existing request ID:

      substate.requestId =
        upserting && substate.requestId
          ? // for `upsertQuery` **updates**, keep the current `requestId`
            substate.requestId
          : // for normal queries or `upsertQuery` **inserts** always update the `requestId`
            meta.requestId

So, what was happening was roughly:

  • Actual request for ID 2 resolves
  • fulfilled action for ID 2 is written, with its request ID
  • upsertQueryEntries for ID 2 is dispatched
  • Inside the reducer:
    • pending write logic for the upsert sees there's an existing entry, and keeps the old request ID from the actual API response, instead of overwriting it with the fake request ID from the upsert
    • Immediately after that, the fulfilled write logic for the upsert tries to update the entry... but the existing and incoming request IDs don't match, and it wasn't being detected as a forced upsert because that logic only was checking for the older upsertQueryData approach

and so we're left with the existing entry, updated to be in a pending state, and without the upserted data written and marked as fulfilled.

Copy link

codesandbox bot commented Dec 14, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

netlify bot commented Dec 14, 2024

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

Name Link
🔨 Latest commit e101ec6
🔍 Latest deploy log https://app.netlify.com/sites/redux-starter-kit-docs/deploys/675d25de7e73310008f0d409
😎 Deploy Preview https://deploy-preview-4768--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.

Copy link

codesandbox-ci bot commented Dec 14, 2024

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 e101ec6:

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

Copy link

github-actions bot commented Dec 14, 2024

size-limit report 📦

Path Size
1. entry point: @reduxjs/toolkit/query/react (modern.mjs) 13.55 KB (-0.02% 🔽)
1. entry point: @reduxjs/toolkit/query (cjs, production.min.cjs) 22.43 KB (+0.19% 🔺)
1. entry point: @reduxjs/toolkit/query/react (cjs, production.min.cjs) 24.51 KB (+0.17% 🔺)
2. entry point: @reduxjs/toolkit/query (without dependencies) (cjs, production.min.cjs) 9.22 KB (+0.07% 🔺)
3. createApi (.modern.mjs) 13.95 KB (-0.01% 🔽)
3. createApi (react) (.modern.mjs) 15.67 KB (-0.05% 🔽)

@markerikson markerikson force-pushed the bugfix/4749-upsertQueryEntries branch from 0ef009e to e101ec6 Compare December 14, 2024 06:29
@markerikson markerikson merged commit 4f3bc9f into master Dec 14, 2024
88 checks passed
@markerikson markerikson deleted the bugfix/4749-upsertQueryEntries branch December 14, 2024 20:51
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.

UpsertQueryEntries breaks query invalidation when the invalidationBehavior is set to delayed
1 participant