-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Bug: cache invalidation of queries that use serializeQueryArgs #3147
Bug: cache invalidation of queries that use serializeQueryArgs #3147
Conversation
|
✅ Deploy Preview for redux-starter-kit-docs ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
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 00f9261:
|
Debugging this a bit more, it seems like the following is happening:
|
I tried looking into adding something like if ('serializeQueryArgs' in endpointDefinitions[endpointName]) {
mwApi.dispatch(
removeQueryResult({
queryCacheKey: queryCacheKey as QueryCacheKey,
})
)
} inside the Maybe you can think of a way to solve this, but I also feel like maybe my use-case of having arguments that are only important enough to trigger a re-query after invalidation may be an abuse of API. Instead, it may make more sense to mention this behavior in the docs and recommend people to instead use WeakMap-based caching if you want to get a unique ID to put inside your |
// TODO: due to bug, the call to listItems3 still uses `1` instead of `2` as expected | ||
await storeRef.store.dispatch(api.endpoints.listItems3.initiate(2)) | ||
const updatedEntry = selectListItems(storeRef.store.getState()) | ||
expect(updatedEntry.data).toEqual(['a', 'b', 'c', 'd', 'e', 'f']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a copy-paste mistake and should have been 'd', 'e', 'f'
I'm just skimming over this, but couldn't this be solved here? redux-toolkit/packages/toolkit/src/query/core/buildSlice.ts Lines 161 to 163 in 0ce2ec0
|
I tried this and unfortunately this isn't an option either. This seems to be because if you have AKA Haven't found the cause for this though, so I could be wrong on my guess |
Alright, yet another attempt at this: use forceRefetch: ({ currentArg, previousArg }) => {
return currentArg !== previousArg
} This works in practice, but doesn't work in the test case because // Don't retry a request that's currently in-flight
if (requestState?.status === 'pending') {
return false
} |
@SebastienGllmt : thanks for the PR! I'll try to look at it "soon" (although I've got other things going on the next few days). That said, can you clarify what actual use cases you're trying to solve atm that led to this? |
We have an RTK Query where the input to the query is large enough that the serialization logic to create the cache key affects performance, so we want to remove it from the cache. The combination of When I created this PR initially, I didn't realize I could use We're still getting some performance hit because it looks like RTK Query does some JSON stringification of the arguments internally even if it's not part of the cache key, but this is manageable (about 50ms of our app startup is spent on this stringification of arguments) |
Gotcha. Hmm. I wonder if we could do some basic |
@SebastienGllmt : could you do me a favor and either generate a JS devtools CPU perf of your app (can send it to me privately on Discord if you want), or put together a quick example that shows the stringification taking up time? Also, could you try out the CodeSandbox CI build from #3193 and see if that helps improve perf for your app? |
If you specify serializeQueryArgs it seems the queries don't get invalidated properly when you call invalidateTags.
Notably, the endpoint does get invalidated and rerun as expected, but the arguments passed in are the old arguments when the cache key was created and not what they are after the invalidation
I added a test that shows this bug in this PR, but I don't have a fix for it.
Here is pseudo code if you prefer that: