Skip to content

pessimistic cache update with onQueryStarted interacts weirdly with invalidatesTags #5288

@pgsandstrom

Description

@pgsandstrom

I have prepared a repo that shows the weirdness: https://github.com/pgsandstrom/rtk-bug-maybe
A small warning that the example is vibe-coded, but I think it clearly shows the issue.

The original real life use case was like this: I do a DELETE on a resource. Once I've confirmed that the resource is deleted I need to immediately remove it from all caches to prevent any other requests to be made to it, because that would error out. So I use pessmistic cache update. But then I also want to do the "normal" cache invalidation with invalidatesTags.

When running the test repo this is logged:

[updateValue] started with: step-1
[component render] getValue: initial
[updateValue] queryFulfilled, server returned: step-1
[updateValue] applying optimistic update to cache
[updateValue] optimistic update dispatched
[component render] getValue: initial <--- This one is the weird one! Optimistic value has been done, but old value is retained until GET returns.
[getValue] fulfilled with: step-1
[component render] getValue: step-1

This feels like a normal use case, and the behaviour caused really subtle bugs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    RTK-QueryIssues related to Redux-Toolkit-Query

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions